2024-04-29 19:09:13 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from os.path import dirname as directory_name
|
|
|
|
from os.path import abspath as path_absolute
|
|
|
|
from os.path import exists as path_exists
|
|
|
|
from re import compile as RECompile
|
|
|
|
from json import loads as json_decode
|
|
|
|
from time import time as timestamp
|
|
|
|
from traceback import print_exc as print_trace_exception
|
|
|
|
from inspect import stack as get_stack
|
|
|
|
import datetime
|
|
|
|
|
|
|
|
re_slashes = RECompile(r'[\/\\\\]+')
|
|
|
|
re_python_file = RECompile(r'^\/(.+)\.py$')
|
|
|
|
re_string_variables = RECompile(r'\{([^\{\}]+)\}')
|
|
|
|
|
|
|
|
root_path = path_absolute(directory_name(__file__))
|
|
|
|
slash = '/' if '/' in root_path else '\\'
|
|
|
|
default_language = ""
|
|
|
|
|
|
|
|
class DefaultData:
|
|
|
|
settings = {
|
|
|
|
"print_format" : "[{type}] {yyyy}{mm}{dd} {hh}{ii}{dd} [{line}]{file}({method}): {message}"
|
|
|
|
}
|
|
|
|
i18n = {}
|
|
|
|
language = None
|
|
|
|
default_language = None
|
|
|
|
|
|
|
|
def get_path(file, i = 1):
|
|
|
|
|
|
|
|
error = (
|
|
|
|
1 << 0 if file == None else
|
|
|
|
1 << 1 if not isinstance(file, str) else
|
|
|
|
1 << 2 if not file else
|
|
|
|
0)
|
|
|
|
path = None
|
|
|
|
|
|
|
|
if not error:
|
|
|
|
path = re_slashes.sub(slash, root_path + file)
|
|
|
|
if not path_exists(path):
|
|
|
|
error |= 1 << 3
|
|
|
|
|
|
|
|
return (path, error << i)
|
|
|
|
|
|
|
|
def load_json(file):
|
|
|
|
|
|
|
|
path, error = get_path(file)
|
|
|
|
json = None
|
|
|
|
|
|
|
|
if not error:
|
|
|
|
try:
|
|
|
|
with open(path, "r") as data:
|
|
|
|
json = json_decode(data.read())
|
|
|
|
error |= (
|
|
|
|
1 << 0 if json == None else
|
|
|
|
1 << 1 if not isinstance(json, dict) else
|
|
|
|
0) << 5
|
|
|
|
except:
|
|
|
|
print_trace_exception()
|
|
|
|
error |= 1 << 0
|
|
|
|
|
|
|
|
return json, error
|
|
|
|
|
|
|
|
def get_action_data():
|
|
|
|
|
|
|
|
stack = get_stack()[2]
|
|
|
|
|
|
|
|
return {
|
|
|
|
"file" : stack.filename,
|
|
|
|
"method" : stack.function,
|
|
|
|
"line" : stack.lineno
|
|
|
|
}
|
|
|
|
|
|
|
|
def _print(_type, string, variables):
|
|
|
|
|
|
|
|
own = {
|
|
|
|
"type" : _type.upper(),
|
|
|
|
**get_action_data(),
|
|
|
|
**variables
|
|
|
|
}
|
|
|
|
date = datetime.datetime.now()
|
|
|
|
|
|
|
|
for key in ("year", "month", "day", "hour", "minute", "second"):
|
|
|
|
|
|
|
|
k = "i" if key == "minute" else key[0]
|
|
|
|
|
|
|
|
own[key] = getattr(date, key)
|
|
|
|
own[k] = own[key] % 100
|
|
|
|
own[k + k] = ("00" + str(own[k]))[-2:]
|
|
|
|
|
|
|
|
own["yyyy"] = own["year"]
|
|
|
|
|
|
|
|
def callback(matches):
|
|
|
|
key = matches.group(1)
|
|
|
|
return str(own[key]) if key in own else matches.group(0)
|
|
|
|
|
|
|
|
own["message"] = re_string_variables.sub(callback, DefaultData.i18n[DefaultData.language][string] if DefaultData.language and string in DefaultData.i18n[DefaultData.language] else string)
|
|
|
|
|
|
|
|
print(re_string_variables.sub(callback, DefaultData.settings["print_format"]))
|
|
|
|
|
|
|
|
def load_default_settings(file):
|
|
|
|
|
|
|
|
json, error = load_json(file)
|
|
|
|
|
|
|
|
if not error:
|
|
|
|
for key, value in json.items():
|
|
|
|
DefaultData.settings[key] = value
|
|
|
|
if key == "language":
|
|
|
|
DefaultData.language = value
|
|
|
|
|
|
|
|
if error:
|
2024-05-01 13:11:54 +00:00
|
|
|
_print("erro", "load_default_settings_error", {"code" : error, "path" : file})
|
2024-04-29 19:09:13 +00:00
|
|
|
else:
|
|
|
|
_print(" ok ", "load_default_settings_ok", {"path" : file})
|
|
|
|
|
|
|
|
return error
|
|
|
|
|
|
|
|
def load_default_i18n(file):
|
|
|
|
|
|
|
|
json, error = load_json(file)
|
|
|
|
|
|
|
|
if not error:
|
|
|
|
for language, sentences in json.items():
|
|
|
|
if isinstance(sentences, dict):
|
|
|
|
if language not in DefaultData.i18n:
|
|
|
|
DefaultData.i18n[language] = {}
|
|
|
|
if DefaultData.language == None:
|
|
|
|
DefaultData.language = language
|
|
|
|
for key, sentence in sentences.items():
|
|
|
|
DefaultData.i18n[language][key] = sentence
|
|
|
|
|
|
|
|
if error:
|
2024-05-01 13:11:54 +00:00
|
|
|
_print("erro", "load_default_i18n_error", {"code" : error, "path" : file})
|
2024-04-29 19:09:13 +00:00
|
|
|
else:
|
|
|
|
_print(" ok ", "load_default_i18n_ok", {"path" : file})
|
|
|
|
|
|
|
|
return error
|
|
|
|
|
|
|
|
def compile_file(file):
|
|
|
|
|
|
|
|
path, error = get_path(file)
|
|
|
|
|
|
|
|
if not error:
|
|
|
|
try:
|
|
|
|
with open(path, "rb") as data:
|
|
|
|
exec(compile(data.read(), re_python_file.sub(r'\1', file), "exec"), globals())
|
|
|
|
except:
|
|
|
|
print_trace_exception()
|
|
|
|
error |= 1 << 0
|
|
|
|
|
|
|
|
if error:
|
2024-05-01 13:11:54 +00:00
|
|
|
_print("erro", "compile_file_error", {"code" : error, "path" : file})
|
2024-04-29 19:09:13 +00:00
|
|
|
else:
|
|
|
|
_print(" ok ", "compile_file_ok", {"path" : file})
|
|
|
|
|
|
|
|
return error
|
|
|
|
|
|
|
|
def get_time(_from):
|
|
|
|
return round((timestamp() - _from) * 1000, 4)
|
|
|
|
|
|
|
|
for language in ("english", "espanol", "galego", "nihongo", "russkiy"):
|
|
|
|
load_default_i18n("/../JSON/I18N/MemWeb.py.i18n." + language + ".json")
|
|
|
|
for fragment in ("", ".secrets"):
|
|
|
|
load_default_settings("/../JSON/MemWeb.py.settings" + fragment + ".json")
|