From 4b99d6d87d3c465610e02f7b74af5329bff279d4 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 26 Nov 2022 22:45:30 +0800 Subject: [PATCH] update --- Difficult_Rocket/utils/translate.py | 61 +++++++++++++++++++++++++++-- README.md | 23 ++++++----- requirement.txt | 1 - 3 files changed, 68 insertions(+), 17 deletions(-) diff --git a/Difficult_Rocket/utils/translate.py b/Difficult_Rocket/utils/translate.py index 8ab9ccc..96ae22a 100644 --- a/Difficult_Rocket/utils/translate.py +++ b/Difficult_Rocket/utils/translate.py @@ -20,10 +20,63 @@ from Difficult_Rocket import DR_runtime, DR_option from Difficult_Rocket.utils import tools from Difficult_Rocket.exception.language import * -""" -这部分代码使用了中文编程,why? -你觉得呢? -""" + +class Translated: + def __init__(self, value: Union[list, tuple, dict, str], raise_error: bool = False): + self.value = value + self.raise_error = raise_error + self.item_names = [] + + def __getattr__(self, item): + if hasattr(object, item): + return getattr(object, item) + self.item_names.append(item) + return self + + def __getitem__(self, item): + self.item_names.append(item) + return self + + def __str__(self): + if not self.item_names: + return self.value + else: + return f'{self.value}.{".".join(self.item_names)}' + + +class Translating: + def __init__(self, value: Union[Dict[str, Any], list, tuple], raise_error: bool = False, get_list: list = None): + self.value: Union[Dict[str, Any], list, tuple] = value + self.raise_error = raise_error + self.get_list = get_list or [] + + def __getitem__(self, item: Union[str, Hashable]) -> Union["Translating", Translated]: + cache_get_list = self.get_list.copy() + cache_get_list.append(item) + try: + cache = self.value[item] + except (KeyError, TypeError): + if DR_option.report_translate_no_found: + frame = inspect.currentframe() + last_frame = frame.f_back + if last_frame.f_code == self.__getattr__.__code__: + last_frame = last_frame.f_back + call_info = f'Translate Not Found at {last_frame.f_code.co_name} by {".".join(cache_get_list)} at:' \ + f'{last_frame.f_code.co_filename}:{last_frame.f_lineno}' + print(call_info) + if not self.raise_error: + return Translated('.'.join(cache_get_list), raise_error=False) + else: + raise TranslateKeyNotFound(item_names=cache_get_list) + return Translating(value=cache, raise_error=self.raise_error, get_list=cache_get_list) + + def __getattr__(self, item: Union[str, Hashable]) -> Union["Translating", Translated]: + if hasattr(object, item): + return getattr(object, item) + return self.__getitem__(item) + + def __str__(self): + return str(self.value) class Translated: def __init__(self, value: Union[list, tuple, dict, str], raise_error: bool = False): diff --git a/README.md b/README.md index 2ee5b80..d51cccc 100644 --- a/README.md +++ b/README.md @@ -39,26 +39,26 @@ - `开发平台 1 - Windows 10` - `Python 3.8.10` - `Windows10 x64` - - `json5 0.9.9` - `pillow 9.2.0` - `pyperclip 1.8.2` - - `pyglet 2.0b2` + - `pyglet 2.0` - `xmltodict 0.12.0` - `tomlkit 0.11.5` - `AMD R5 5600X` - `AMD RX 550 4G` -- `开发平台 2 - macOS Big Sur` - - `Python 3.8.10` - - `macOS Big Sur 11.2.1` - - `Pyglet 1.5.15` - - `Json5 0.9.5` - - `pillow 8.1.2` - - `Intel I5 8279U` + +[//]: # (- `开发平台 2 - macOS Big Sur`) +[//]: # ( - `Python 3.8.10`) +[//]: # ( - `macOS Big Sur 11.2.1`) +[//]: # ( - `Pyglet 1.5.15`) +[//]: # ( - `Json5 0.9.5`) +[//]: # ( - `pillow 8.1.2`) +[//]: # ( - `Intel I5 8279U`) ## 需要的Python模块 -- `json5` (已经内置 V0.9.6 路径:`./libs/json5`) - `tomlkit` +- `rtoml` - `pyglet` (已经内置 V2.0b2 路径:`./libs/pyglet`) - `xmltodict` (已经内置 V0.12.0 路径:`./libs/xmltodict`) - `pyperclip` (已经内置 V1.8.2 路径: `./libs/pyperclip`) @@ -68,8 +68,7 @@ ## 感谢 - [pyglet](https://github.com/pyglet/pyglet) : GUI 和画面渲染 -- `json5`: json5 解析器 -- `tomlkit`: toml 解析器 +- `tomlkit` / `rtoml` : toml 解析器 - `xmltodict`: xml 与 dict 转换器 - `pyperclip`: 剪贴板! - [@Rayawa](https://github.com/Rayawa) : 文档矫正 & 翻译部分 lang diff --git a/requirement.txt b/requirement.txt index 78a5747..3033524 100644 --- a/requirement.txt +++ b/requirement.txt @@ -8,5 +8,4 @@ pymunk rtoml setuptools Cython -colorama viztracer \ No newline at end of file