mirror of
https://github.com/Faraphel/Atlas-Install.git
synced 2025-07-01 18:28:27 +02:00
module variable are now using the global keyword instead of a module-self object
This commit is contained in:
parent
e90e8bb8e6
commit
d6ebb37253
4 changed files with 20 additions and 19 deletions
10
main.pyw
10
main.pyw
|
@ -4,17 +4,17 @@ from source.option import Options
|
||||||
from source.translation import load_language
|
from source.translation import load_language
|
||||||
|
|
||||||
|
|
||||||
# this allows every variable to be accessible from other files, useful for the plugins
|
|
||||||
self = __import__(__name__)
|
|
||||||
|
|
||||||
options = Options.from_file("./option.json")
|
options = Options.from_file("./option.json")
|
||||||
translater = load_language(options.language.get())
|
translater = load_language(options.language.get())
|
||||||
|
window = None
|
||||||
|
|
||||||
|
|
||||||
def main_gui():
|
def main_gui():
|
||||||
|
global window
|
||||||
|
|
||||||
from source.interface.gui import install
|
from source.interface.gui import install
|
||||||
self.window = install.Window(options)
|
window = install.Window(options)
|
||||||
self.window.run()
|
window.run()
|
||||||
|
|
||||||
|
|
||||||
def main_cli(argparser: argparse.ArgumentParser):
|
def main_cli(argparser: argparse.ArgumentParser):
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
|
import os
|
||||||
|
|
||||||
# metadata
|
# metadata
|
||||||
__version__ = (0, 12, 0)
|
__version__ = (0, 12, 0)
|
||||||
__author__ = 'Faraphel'
|
__author__ = 'Faraphel'
|
||||||
|
|
||||||
|
|
||||||
# external links
|
# external links
|
||||||
import os
|
|
||||||
|
|
||||||
discord_url = "https://discord.gg/HEYW5v8ZCd"
|
discord_url = "https://discord.gg/HEYW5v8ZCd"
|
||||||
github_wiki_url = "https://github.com/Faraphel/MKWF-Install/wiki/help"
|
github_wiki_url = "https://github.com/Faraphel/MKWF-Install/wiki/help"
|
||||||
readthedocs_url = "https://mkwf-install.readthedocs.io/"
|
readthedocs_url = "https://mkwf-install.readthedocs.io/"
|
||||||
|
|
|
@ -11,8 +11,7 @@ if TYPE_CHECKING:
|
||||||
from source import TemplateSafeEval, Env
|
from source import TemplateSafeEval, Env
|
||||||
|
|
||||||
|
|
||||||
self = __import__(__name__)
|
safe_eval_cache: dict[hash, Callable] = {}
|
||||||
self.safe_eval_cache = {}
|
|
||||||
|
|
||||||
|
|
||||||
class SafeEvalException(Exception):
|
class SafeEvalException(Exception):
|
||||||
|
@ -38,6 +37,7 @@ def safe_eval(template: "TemplateSafeEval", env: "Env" = None, macros: dict[str,
|
||||||
|
|
||||||
:return: the lambda expression
|
:return: the lambda expression
|
||||||
"""
|
"""
|
||||||
|
global safe_eval_cache
|
||||||
|
|
||||||
if len(template) == 0: return lambda *_, **__: ""
|
if len(template) == 0: return lambda *_, **__: ""
|
||||||
if env is None: env = {}
|
if env is None: env = {}
|
||||||
|
@ -46,7 +46,7 @@ def safe_eval(template: "TemplateSafeEval", env: "Env" = None, macros: dict[str,
|
||||||
|
|
||||||
template_key: hash = hash((template, args, tuple(env.items()))) # unique identifiant for every template
|
template_key: hash = hash((template, args, tuple(env.items()))) # unique identifiant for every template
|
||||||
# if the safe_eval return a callable and have already been called, return the cached callable
|
# if the safe_eval return a callable and have already been called, return the cached callable
|
||||||
if template_key in self.safe_eval_cache: return self.safe_eval_cache[template_key]
|
if template_key in safe_eval_cache: return safe_eval_cache[template_key]
|
||||||
|
|
||||||
# replace the macro in the template
|
# replace the macro in the template
|
||||||
template = replace_macro(template=template, macros=macros)
|
template = replace_macro(template=template, macros=macros)
|
||||||
|
@ -138,5 +138,5 @@ def safe_eval(template: "TemplateSafeEval", env: "Env" = None, macros: dict[str,
|
||||||
|
|
||||||
# return the evaluated formula
|
# return the evaluated formula
|
||||||
lambda_template = eval(compile(expression, "<safe_eval>", "eval"), globals_, locals_)
|
lambda_template = eval(compile(expression, "<safe_eval>", "eval"), globals_, locals_)
|
||||||
self.safe_eval_cache[template_key] = lambda_template # cache the callable for potential latter call
|
safe_eval_cache[template_key] = lambda_template # cache the callable for potential latter call
|
||||||
return better_safe_eval_error(lambda_template, template=template)
|
return better_safe_eval_error(lambda_template, template=template)
|
||||||
|
|
|
@ -6,9 +6,8 @@ if TYPE_CHECKING:
|
||||||
from source.mkw.ModConfig import ModConfig
|
from source.mkw.ModConfig import ModConfig
|
||||||
|
|
||||||
|
|
||||||
self = __import__(__name__)
|
_language: str | None = None
|
||||||
self._language = None
|
_language_data: dict = {}
|
||||||
self._language_data = {}
|
|
||||||
|
|
||||||
|
|
||||||
def load_language(language: str):
|
def load_language(language: str):
|
||||||
|
@ -17,8 +16,10 @@ def load_language(language: str):
|
||||||
:param language: language code to load
|
:param language: language code to load
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
self._language = language
|
global _language, _language_data
|
||||||
self._language_data = json.loads(Path(f"./assets/language/{language}.json").read_text(encoding="utf8"))
|
|
||||||
|
_language = language
|
||||||
|
_language_data = json.loads(Path(f"./assets/language/{language}.json").read_text(encoding="utf8"))
|
||||||
|
|
||||||
|
|
||||||
def translate(text) -> str:
|
def translate(text) -> str:
|
||||||
|
@ -27,7 +28,7 @@ def translate(text) -> str:
|
||||||
:param text: list of text to translate
|
:param text: list of text to translate
|
||||||
:return: translated text
|
:return: translated text
|
||||||
"""
|
"""
|
||||||
return self._language_data.get("translation", {}).get(text, text)
|
return _language_data.get("translation", {}).get(text, text)
|
||||||
|
|
||||||
|
|
||||||
def translate_external(mod_config: "ModConfig", message_texts: dict[str, str], default: str = "") -> str:
|
def translate_external(mod_config: "ModConfig", message_texts: dict[str, str], default: str = "") -> str:
|
||||||
|
@ -38,9 +39,9 @@ def translate_external(mod_config: "ModConfig", message_texts: dict[str, str], d
|
||||||
:param default: the default message if no translation are found
|
:param default: the default message if no translation are found
|
||||||
:return: the translated message
|
:return: the translated message
|
||||||
"""
|
"""
|
||||||
message = message_texts.get(self._language)
|
message = message_texts.get(_language)
|
||||||
if message is None: message = message_texts.get("*")
|
if message is None: message = message_texts.get("*")
|
||||||
if message is None: message = default
|
if message is None: message = default
|
||||||
return mod_config.multiple_safe_eval(message, args=["language"])(language=self._language)
|
return mod_config.multiple_safe_eval(message, args=["language"])(language=_language)
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue