-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
286 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -160,4 +160,5 @@ cython_debug/ | |
#.idea/ | ||
venv | ||
cache.json | ||
metadata.json | ||
/tests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import os | ||
import shutil | ||
import json | ||
|
||
from .. import model as file_model | ||
|
||
|
||
class JSONConfigFile(file_model.ConfigFile): | ||
"""JSON配置文件""" | ||
|
||
def __init__( | ||
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 | ||
self.template_data = template_data | ||
|
||
def exists(self) -> bool: | ||
return os.path.exists(self.config_file_name) | ||
|
||
async def create(self): | ||
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: | ||
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: | ||
|
||
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: | ||
self.template_data = json.load(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}") | ||
|
||
if completion: | ||
|
||
for key in self.template_data: | ||
if key not in cfg: | ||
cfg[key] = self.template_data[key] | ||
|
||
return cfg | ||
|
||
async def save(self, cfg: dict): | ||
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: | ||
json.dump(cfg, f, indent=4, ensure_ascii=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import os | ||
import shutil | ||
import importlib | ||
import logging | ||
|
||
from .. import model as file_model | ||
|
||
|
||
class PythonModuleConfigFile(file_model.ConfigFile): | ||
"""Python模块配置文件""" | ||
|
||
config_file_name: str = None | ||
"""配置文件名""" | ||
|
||
template_file_name: str = None | ||
"""模板文件名""" | ||
|
||
def __init__(self, config_file_name: str, template_file_name: str) -> None: | ||
self.config_file_name = config_file_name | ||
self.template_file_name = template_file_name | ||
|
||
def exists(self) -> bool: | ||
return os.path.exists(self.config_file_name) | ||
|
||
async def create(self): | ||
shutil.copyfile(self.template_file_name, self.config_file_name) | ||
|
||
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) | ||
|
||
for key in dir(module): | ||
if key.startswith('__'): | ||
continue | ||
|
||
if not isinstance(getattr(module, key), allowed_types): | ||
continue | ||
|
||
cfg[key] = getattr(module, key) | ||
|
||
# 从模板模块文件中进行补全 | ||
if completion: | ||
module_name = os.path.splitext(os.path.basename(self.template_file_name))[0] | ||
module = importlib.import_module(module_name) | ||
|
||
for key in dir(module): | ||
if key.startswith('__'): | ||
continue | ||
|
||
if not isinstance(getattr(module, key), allowed_types): | ||
continue | ||
|
||
if key not in cfg: | ||
cfg[key] = getattr(module, key) | ||
|
||
return cfg | ||
|
||
async def save(self, data: dict): | ||
logging.warning('Python模块配置文件不支持保存') | ||
|
||
def save_sync(self, data: dict): | ||
logging.warning('Python模块配置文件不支持保存') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from __future__ import annotations | ||
|
||
from . import model as file_model | ||
from .impls import pymodule, json as json_file | ||
|
||
|
||
managers: ConfigManager = [] | ||
|
||
|
||
class ConfigManager: | ||
"""配置文件管理器""" | ||
|
||
file: file_model.ConfigFile = None | ||
"""配置文件实例""" | ||
|
||
data: dict = None | ||
"""配置数据""" | ||
|
||
def __init__(self, cfg_file: file_model.ConfigFile) -> None: | ||
self.file = cfg_file | ||
self.data = {} | ||
|
||
async def load_config(self, completion: bool=True): | ||
self.data = await self.file.load(completion=completion) | ||
|
||
async def dump_config(self): | ||
await self.file.save(self.data) | ||
|
||
def dump_config_sync(self): | ||
self.file.save_sync(self.data) | ||
|
||
|
||
async def load_python_module_config(config_name: str, template_name: str, completion: bool=True) -> ConfigManager: | ||
"""加载Python模块配置文件""" | ||
cfg_inst = pymodule.PythonModuleConfigFile( | ||
config_name, | ||
template_name | ||
) | ||
|
||
cfg_mgr = ConfigManager(cfg_inst) | ||
await cfg_mgr.load_config(completion=completion) | ||
|
||
return cfg_mgr | ||
|
||
|
||
async def load_json_config(config_name: str, template_name: str=None, template_data: dict=None, completion: bool=True) -> ConfigManager: | ||
"""加载JSON配置文件""" | ||
cfg_inst = json_file.JSONConfigFile( | ||
config_name, | ||
template_name, | ||
template_data | ||
) | ||
|
||
cfg_mgr = ConfigManager(cfg_inst) | ||
await cfg_mgr.load_config(completion=completion) | ||
|
||
return cfg_mgr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import abc | ||
|
||
|
||
class ConfigFile(metaclass=abc.ABCMeta): | ||
"""配置文件抽象类""" | ||
|
||
config_file_name: str = None | ||
"""配置文件名""" | ||
|
||
template_file_name: str = None | ||
"""模板文件名""" | ||
|
||
template_data: dict = None | ||
"""模板数据""" | ||
|
||
@abc.abstractmethod | ||
def exists(self) -> bool: | ||
pass | ||
|
||
@abc.abstractmethod | ||
async def create(self): | ||
pass | ||
|
||
@abc.abstractmethod | ||
async def load(self, completion: bool=True) -> dict: | ||
pass | ||
|
||
@abc.abstractmethod | ||
async def save(self, data: dict): | ||
pass | ||
|
||
@abc.abstractmethod | ||
def save_sync(self, data: dict): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from __future__ import annotations | ||
|
||
import traceback | ||
import re | ||
|
||
from ..core import app | ||
from ..common import entity | ||
|
||
|
||
class PostPreprocessor: | ||
|
||
def __init__(self, ap: app.Application): | ||
self.ap = ap | ||
|
||
async def preprocess_post(self, post: entity.Post) -> entity.Post: | ||
"""预处理稿件""" | ||
try: | ||
post.extra_text = await self.emotion_text_preprocess(post) | ||
except Exception as e: | ||
traceback.print_exc() | ||
post.extra_text = '' | ||
|
||
return post | ||
|
||
async def emotion_text_preprocess(self, post: entity.Post) -> str: | ||
"""发表时的附带文本预处理""" | ||
|
||
text = post.text | ||
post_id = post.id | ||
uin = str(post.uin) | ||
|
||
def at(user_id): | ||
return f"@{{uin:{user_id},nick:,who:1}}" | ||
|
||
def links(): | ||
# 从 text 中提取链接 | ||
return re.findall(r'https?://[^\s]+', text) | ||
|
||
extra_raw_text = self.ap.meta.data['post_publish_text'] | ||
|
||
extra_text = eval(extra_raw_text) | ||
|
||
# exec(extra_raw_text, locals()) | ||
|
||
return extra_text |