style: introduce ruff as linter and formatter (#1356)

* style: remove necessary imports

* style: fix F841

* style: fix F401

* style: fix F811

* style: fix E402

* style: fix E721

* style: fix E722

* style: fix E722

* style: fix F541

* style: ruff format

* style: all passed

* style: add ruff in deps

* style: more ignores in ruff.toml

* style: add pre-commit
This commit is contained in:
Junyan Qin (Chin)
2025-04-29 17:24:07 +08:00
committed by GitHub
parent 09e70d70e9
commit 209f16af76
240 changed files with 5307 additions and 4689 deletions

View File

@@ -9,7 +9,10 @@ class JSONConfigFile(file_model.ConfigFile):
"""JSON配置文件"""
def __init__(
self, config_file_name: str, template_file_name: str = None, template_data: dict = None
self,
config_file_name: str,
template_file_name: str = None,
template_data: dict = None,
) -> None:
self.config_file_name = config_file_name
self.template_file_name = template_file_name
@@ -22,28 +25,26 @@ class JSONConfigFile(file_model.ConfigFile):
if self.template_file_name is not None:
shutil.copyfile(self.template_file_name, self.config_file_name)
elif self.template_data is not None:
with open(self.config_file_name, "w", encoding="utf-8") as f:
with open(self.config_file_name, 'w', encoding='utf-8') as f:
json.dump(self.template_data, f, indent=4, ensure_ascii=False)
else:
raise ValueError("template_file_name or template_data must be provided")
async def load(self, completion: bool=True) -> dict:
raise ValueError('template_file_name or template_data must be provided')
async def load(self, completion: bool = True) -> dict:
if not self.exists():
await self.create()
if self.template_file_name is not None:
with open(self.template_file_name, "r", encoding="utf-8") as f:
with open(self.template_file_name, 'r', encoding='utf-8') as f:
self.template_data = json.load(f)
with open(self.config_file_name, "r", encoding="utf-8") as f:
with open(self.config_file_name, 'r', encoding='utf-8') as f:
try:
cfg = json.load(f)
except json.JSONDecodeError as e:
raise Exception(f"配置文件 {self.config_file_name} 语法错误: {e}")
raise Exception(f'配置文件 {self.config_file_name} 语法错误: {e}')
if completion:
for key in self.template_data:
if key not in cfg:
cfg[key] = self.template_data[key]
@@ -51,9 +52,9 @@ class JSONConfigFile(file_model.ConfigFile):
return cfg
async def save(self, cfg: dict):
with open(self.config_file_name, "w", encoding="utf-8") as f:
with open(self.config_file_name, 'w', encoding='utf-8') as f:
json.dump(cfg, f, indent=4, ensure_ascii=False)
def save_sync(self, cfg: dict):
with open(self.config_file_name, "w", encoding="utf-8") as f:
with open(self.config_file_name, 'w', encoding='utf-8') as f:
json.dump(cfg, f, indent=4, ensure_ascii=False)

View File

@@ -25,10 +25,10 @@ class PythonModuleConfigFile(file_model.ConfigFile):
async def create(self):
shutil.copyfile(self.template_file_name, self.config_file_name)
async def load(self, completion: bool=True) -> dict:
async def load(self, completion: bool = True) -> dict:
module_name = os.path.splitext(os.path.basename(self.config_file_name))[0]
module = importlib.import_module(module_name)
cfg = {}
allowed_types = (int, float, str, bool, list, dict)
@@ -63,4 +63,4 @@ class PythonModuleConfigFile(file_model.ConfigFile):
logging.warning('Python模块配置文件不支持保存')
def save_sync(self, data: dict):
logging.warning('Python模块配置文件不支持保存')
logging.warning('Python模块配置文件不支持保存')

View File

@@ -9,7 +9,10 @@ class YAMLConfigFile(file_model.ConfigFile):
"""YAML配置文件"""
def __init__(
self, config_file_name: str, template_file_name: str = None, template_data: dict = None
self,
config_file_name: str,
template_file_name: str = None,
template_data: dict = None,
) -> None:
self.config_file_name = config_file_name
self.template_file_name = template_file_name
@@ -22,28 +25,26 @@ class YAMLConfigFile(file_model.ConfigFile):
if self.template_file_name is not None:
shutil.copyfile(self.template_file_name, self.config_file_name)
elif self.template_data is not None:
with open(self.config_file_name, "w", encoding="utf-8") as f:
with open(self.config_file_name, 'w', encoding='utf-8') as f:
yaml.dump(self.template_data, f, indent=4, allow_unicode=True)
else:
raise ValueError("template_file_name or template_data must be provided")
async def load(self, completion: bool=True) -> dict:
raise ValueError('template_file_name or template_data must be provided')
async def load(self, completion: bool = True) -> dict:
if not self.exists():
await self.create()
if self.template_file_name is not None:
with open(self.template_file_name, "r", encoding="utf-8") as f:
with open(self.template_file_name, 'r', encoding='utf-8') as f:
self.template_data = yaml.load(f, Loader=yaml.FullLoader)
with open(self.config_file_name, "r", encoding="utf-8") as f:
with open(self.config_file_name, 'r', encoding='utf-8') as f:
try:
cfg = yaml.load(f, Loader=yaml.FullLoader)
except yaml.YAMLError as e:
raise Exception(f"配置文件 {self.config_file_name} 语法错误: {e}")
raise Exception(f'配置文件 {self.config_file_name} 语法错误: {e}')
if completion:
for key in self.template_data:
if key not in cfg:
cfg[key] = self.template_data[key]
@@ -51,9 +52,9 @@ class YAMLConfigFile(file_model.ConfigFile):
return cfg
async def save(self, cfg: dict):
with open(self.config_file_name, "w", encoding="utf-8") as f:
with open(self.config_file_name, 'w', encoding='utf-8') as f:
yaml.dump(cfg, f, indent=4, allow_unicode=True)
def save_sync(self, cfg: dict):
with open(self.config_file_name, "w", encoding="utf-8") as f:
yaml.dump(cfg, f, indent=4, allow_unicode=True)
with open(self.config_file_name, 'w', encoding='utf-8') as f:
yaml.dump(cfg, f, indent=4, allow_unicode=True)

View File

@@ -6,7 +6,7 @@ from .impls import pymodule, json as json_file, yaml as yaml_file
class ConfigManager:
"""配置文件管理器"""
name: str = None
"""配置管理器名"""
@@ -31,7 +31,7 @@ class ConfigManager:
self.file = cfg_file
self.data = {}
async def load_config(self, completion: bool=True):
async def load_config(self, completion: bool = True):
self.data = await self.file.load(completion=completion)
async def dump_config(self):
@@ -41,9 +41,11 @@ class ConfigManager:
self.file.save_sync(self.data)
async def load_python_module_config(config_name: str, template_name: str, completion: bool=True) -> ConfigManager:
async def load_python_module_config(
config_name: str, template_name: str, completion: bool = True
) -> ConfigManager:
"""加载Python模块配置文件
Args:
config_name (str): 配置文件名
template_name (str): 模板文件名
@@ -52,10 +54,7 @@ async def load_python_module_config(config_name: str, template_name: str, comple
Returns:
ConfigManager: 配置文件管理器
"""
cfg_inst = pymodule.PythonModuleConfigFile(
config_name,
template_name
)
cfg_inst = pymodule.PythonModuleConfigFile(config_name, template_name)
cfg_mgr = ConfigManager(cfg_inst)
await cfg_mgr.load_config(completion=completion)
@@ -63,20 +62,21 @@ async def load_python_module_config(config_name: str, template_name: str, comple
return cfg_mgr
async def load_json_config(config_name: str, template_name: str=None, template_data: dict=None, completion: bool=True) -> ConfigManager:
async def load_json_config(
config_name: str,
template_name: str = None,
template_data: dict = None,
completion: bool = True,
) -> ConfigManager:
"""加载JSON配置文件
Args:
config_name (str): 配置文件名
template_name (str): 模板文件名
template_data (dict): 模板数据
completion (bool): 是否自动补全内存中的配置文件
"""
cfg_inst = json_file.JSONConfigFile(
config_name,
template_name,
template_data
)
cfg_inst = json_file.JSONConfigFile(config_name, template_name, template_data)
cfg_mgr = ConfigManager(cfg_inst)
await cfg_mgr.load_config(completion=completion)
@@ -84,9 +84,14 @@ async def load_json_config(config_name: str, template_name: str=None, template_d
return cfg_mgr
async def load_yaml_config(config_name: str, template_name: str=None, template_data: dict=None, completion: bool=True) -> ConfigManager:
async def load_yaml_config(
config_name: str,
template_name: str = None,
template_data: dict = None,
completion: bool = True,
) -> ConfigManager:
"""加载YAML配置文件
Args:
config_name (str): 配置文件名
template_name (str): 模板文件名
@@ -96,11 +101,7 @@ async def load_yaml_config(config_name: str, template_name: str=None, template_d
Returns:
ConfigManager: 配置文件管理器
"""
cfg_inst = yaml_file.YAMLConfigFile(
config_name,
template_name,
template_data
)
cfg_inst = yaml_file.YAMLConfigFile(config_name, template_name, template_data)
cfg_mgr = ConfigManager(cfg_inst)
await cfg_mgr.load_config(completion=completion)

View File

@@ -22,7 +22,7 @@ class ConfigFile(metaclass=abc.ABCMeta):
pass
@abc.abstractmethod
async def load(self, completion: bool=True) -> dict:
async def load(self, completion: bool = True) -> dict:
pass
@abc.abstractmethod