闲的没事是真开心啊
This commit is contained in:
parent
32f485c8ea
commit
c05c502ca8
@ -4,7 +4,7 @@
|
|||||||
# All rights reserved
|
# All rights reserved
|
||||||
# -------------------------------
|
# -------------------------------
|
||||||
|
|
||||||
from typing import Dict, Union
|
from typing import Dict, Union, List, Optional
|
||||||
from dataclasses import dataclass
|
from dataclasses import dataclass
|
||||||
|
|
||||||
# pyglet
|
# pyglet
|
||||||
@ -28,6 +28,7 @@ class SR1PartData:
|
|||||||
flip_y: bool
|
flip_y: bool
|
||||||
explode: bool
|
explode: bool
|
||||||
textures: str
|
textures: str
|
||||||
|
connections: Optional[List[int]] = None
|
||||||
|
|
||||||
|
|
||||||
class SR1Textures(Options):
|
class SR1Textures(Options):
|
||||||
|
@ -22,11 +22,12 @@ class TranslateError(BaseError):
|
|||||||
|
|
||||||
class TranslateKeyNotFound(TranslateError):
|
class TranslateKeyNotFound(TranslateError):
|
||||||
"""语言文件某项缺失"""
|
"""语言文件某项缺失"""
|
||||||
def __init__(self, item_names: list):
|
def __init__(self, value: dict = None, item_names: list = None):
|
||||||
self.item_names: list = item_names
|
self.item_names: list = item_names
|
||||||
|
self.value: dict = value
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"{self.__class__.__name__}: Can't get item {'. '.join(self.item_names)}"
|
return f"{self.__class__.__name__}: Can't get item {'. '.join(self.item_names)} from: {self.value}"
|
||||||
|
|
||||||
|
|
||||||
class TranslateFileNotFound(TranslateError):
|
class TranslateFileNotFound(TranslateError):
|
||||||
|
@ -23,16 +23,12 @@ from Difficult_Rocket.exception.language import *
|
|||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class TranslateConfig:
|
class TranslateConfig:
|
||||||
raise_error: bool = False
|
raise_error: bool = False # 引用错误时抛出错误
|
||||||
# 引用错误时抛出错误
|
crack_normal: bool = False # 出现错误引用后 将引用到的正确内容替换为引用路径
|
||||||
crack_normal: bool = False
|
insert_crack: bool = True # 加入引用的错误内容
|
||||||
# 出现错误引用后 将引用到的正确内容替换为引用路径
|
is_final: bool = False # 是否为最终内容
|
||||||
insert_crack: bool = True
|
keep_get: bool = True # 引用错误后是否继续引用
|
||||||
# 加入引用的错误内容
|
always_copy: bool = False # 是否一直新建 Translate (为 True 会降低性能)
|
||||||
is_final: bool = False
|
|
||||||
# 是否为最终内容
|
|
||||||
keep_get: bool = True
|
|
||||||
# 引用错误后是否继续引用
|
|
||||||
|
|
||||||
def set(self, item: str, value: bool) -> 'TranslateConfig':
|
def set(self, item: str, value: bool) -> 'TranslateConfig':
|
||||||
assert getattr(self, item, None) is not None, f'Config {item} is not in TranslateConfig'
|
assert getattr(self, item, None) is not None, f'Config {item} is not in TranslateConfig'
|
||||||
@ -45,12 +41,16 @@ class TranslateConfig:
|
|||||||
crack_normal=self.crack_normal,
|
crack_normal=self.crack_normal,
|
||||||
insert_crack=self.insert_crack,
|
insert_crack=self.insert_crack,
|
||||||
is_final=self.is_final,
|
is_final=self.is_final,
|
||||||
keep_get=self.keep_get)
|
keep_get=self.keep_get,
|
||||||
|
always_copy=self.always_copy)
|
||||||
|
|
||||||
def copy(self) -> 'TranslateConfig':
|
def copy(self) -> 'TranslateConfig':
|
||||||
return self.__copy__()
|
return self.__copy__()
|
||||||
|
|
||||||
|
|
||||||
|
key_type = Union[str, int, Hashable]
|
||||||
|
|
||||||
|
|
||||||
class Translates:
|
class Translates:
|
||||||
name = 'Translate'
|
name = 'Translate'
|
||||||
|
|
||||||
@ -68,45 +68,77 @@ class Translates:
|
|||||||
self.config = config or TranslateConfig()
|
self.config = config or TranslateConfig()
|
||||||
self.get_list = get_list or []
|
self.get_list = get_list or []
|
||||||
|
|
||||||
def set_option(self, option: Union[str, TranslateConfig],
|
def set_conf_(self, option: Union[str, TranslateConfig],
|
||||||
value: Optional[Union[bool, List[str]]] = None) -> 'Translates':
|
value: Optional[Union[bool, List[str]]] = None) -> 'Translates':
|
||||||
|
"""
|
||||||
|
设置翻译设置
|
||||||
|
:param option: 设置名称 / 新设置
|
||||||
|
:param value:
|
||||||
|
:return:
|
||||||
|
"""
|
||||||
assert type(option) is str or isinstance(option, TranslateConfig)
|
assert type(option) is str or isinstance(option, TranslateConfig)
|
||||||
if isinstance(option, TranslateConfig):
|
if isinstance(option, TranslateConfig):
|
||||||
self.config = option
|
self.config = option
|
||||||
return self
|
return self
|
||||||
self.config.set(option, value)
|
self.config.set(option, value)
|
||||||
|
return self
|
||||||
|
|
||||||
def __getitem__(self, item: Union[str, int, Hashable]) -> Union["Translates"]:
|
def _raise_no_value(self, e: Exception, item: key_type):
|
||||||
|
if self.config.raise_error:
|
||||||
|
raise TranslateKeyNotFound(self.value, [x[1] for x in self.get_list]) from None
|
||||||
|
elif DR_option.report_translate_no_found:
|
||||||
|
frame = inspect.currentframe()
|
||||||
|
if frame is not None:
|
||||||
|
frame = frame.f_back
|
||||||
|
if frame.f_back.f_code is not self.__getattr__.__code__:
|
||||||
|
frame = frame.f_back
|
||||||
|
frame = f'call at {frame.f_code.co_filename}:{frame.f_lineno}'
|
||||||
|
else:
|
||||||
|
frame = 'but No Frame environment'
|
||||||
|
raise_info = f"{self.name} Cause a error when getting {item} {frame}"
|
||||||
|
print(raise_info)
|
||||||
|
|
||||||
|
def __getitem__(self, item: key_type) -> Union["Translates"]:
|
||||||
"""
|
"""
|
||||||
:param item: 取用的内容/小天才
|
:param item: 取用的内容/小天才
|
||||||
:return:
|
:return:
|
||||||
"""
|
"""
|
||||||
cache_get_list = self.get_list.copy()
|
|
||||||
try:
|
try:
|
||||||
cache = self.value[item]
|
cache_value = self.value[item]
|
||||||
cache_get_list.append((True, item))
|
self.get_list.append((True, item))
|
||||||
except (KeyError, TypeError):
|
if self.config.always_copy:
|
||||||
cache_get_list.append((False, item))
|
return self.copy
|
||||||
# 出现问题
|
except (KeyError, TypeError, AttributeError) as e:
|
||||||
if DR_option.report_translate_no_found:
|
self.get_list.append((False, item))
|
||||||
frame = inspect.currentframe()
|
self._raise_no_value(e, item)
|
||||||
last_frame = frame.f_back
|
|
||||||
if last_frame.f_code == self.__getattr__.__code__:
|
# except (KeyError, TypeError):s
|
||||||
last_frame = last_frame.f_back
|
# cache_get_list.append((False, item))
|
||||||
call_info = f'{self.name} Not Found at {last_frame.f_code.co_name} by ' \
|
# # 出现问题
|
||||||
f'{".".join([x[1] for x in cache_get_list])} at:' \
|
# if DR_option.report_translate_no_found:
|
||||||
f'{last_frame.f_code.co_filename}:{last_frame.f_lineno}'
|
# frame = inspect.currentframe()
|
||||||
print(call_info)
|
# last_frame = frame.f_back
|
||||||
# 如果不抛出错误
|
# if last_frame.f_code == self.__getattr__.__code__:
|
||||||
if self.config.raise_error:
|
# last_frame = last_frame.f_back
|
||||||
raise TranslateKeyNotFound(item_names=cache_get_list) from None
|
# call_info = f'{self.name} Not Found at {last_frame.f_code.co_name} by ' \
|
||||||
if self.final: # 如果已经是翻译结果
|
# f'{".".join([x[1] for x in cache_get_list])} at:' \
|
||||||
return Translates(value='.'.join(cache_get_list))
|
# f'{last_frame.f_code.co_filename}:{last_frame.f_lineno}'
|
||||||
else:
|
# print(call_info)
|
||||||
if self.final:
|
# # 如果不抛出错误
|
||||||
return self
|
# if self.config.raise_error:
|
||||||
else:
|
# raise TranslateKeyNotFound(item_names=cache_get_list) from None
|
||||||
return Translates(value=cache, get_list=cache_get_list)
|
# if self.final: # 如果已经是翻译结果
|
||||||
|
# return Translates(value='.'.join(cache_get_list))
|
||||||
|
# else:
|
||||||
|
# if self.final:
|
||||||
|
# return self
|
||||||
|
# else:
|
||||||
|
# return Translates(value=cache, get_list=cache_get_list)
|
||||||
|
|
||||||
|
def __copy__(self) -> 'Translates':
|
||||||
|
return Translates(value=self.value,
|
||||||
|
config=self.config,
|
||||||
|
get_list=self.get_lists)
|
||||||
|
|
||||||
def __getattr__(self, item: Union[str, Hashable]) -> Union["Translates"]:
|
def __getattr__(self, item: Union[str, Hashable]) -> Union["Translates"]:
|
||||||
# 实际上我这里完全不需要处理正常需求,因为 __getattribute__ 已经帮我处理过了
|
# 实际上我这里完全不需要处理正常需求,因为 __getattribute__ 已经帮我处理过了
|
||||||
@ -120,7 +152,7 @@ class Translates:
|
|||||||
|
|
||||||
class Tr:
|
class Tr:
|
||||||
"""
|
"""
|
||||||
我不装了,我就抄了tr
|
我不装了,我就抄了tr(实际上没啥关系)
|
||||||
GOOD
|
GOOD
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
0
docs/src/howto/translate.md
Normal file
0
docs/src/howto/translate.md
Normal file
@ -1,6 +0,0 @@
|
|||||||
|
|
||||||
import random
|
|
||||||
|
|
||||||
test_list = [random.randint(0, 1_0000) for x in range(1_10000)]
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user