refactor(config): 重构配置类以支持动态元数据字段

配置类 ThesisConfig 现在使用 metadata 字典直接透传 TOML 配置,
无需为每个变量单独声明字段。新增模板变量只需修改 TOML 文件,
无需修改 Python 代码。

BREAKING CHANGE: 配置文件结构发生改变,从单独字段改为统一的
metadata 节点。
This commit is contained in:
zzy
2026-05-08 23:07:23 +08:00
parent 74d28ea2d8
commit fc6afdea9d
3 changed files with 24 additions and 43 deletions

4
.gitignore vendored
View File

@@ -20,3 +20,7 @@ wheels/
*.docx *.docx
*.doc *.doc
*.txt *.txt
.vscode/
.tmp/
*.toml

View File

@@ -6,17 +6,15 @@ import tomllib
@dataclass @dataclass
class ThesisConfig: class ThesisConfig:
"""论文配置数据(学生信息、元数据等,不包含正文内容)。""" """论文配置数据
student_name: str = "<None>" ``metadata`` 直接透传 TOML 的 ``[metadata]`` 节,不再为每个变量声明字段。
student_id: str = "<None>" 新增模板变量只需改 TOML无需修改 Python。
college: str = "<None>" """
major: str = "<None>"
class_: str = "<None>"
advisor: str = "<None>"
advisor_title: str = "<None>"
title: str = "<None>"
metadata: dict = field(default_factory=dict)
# 以下字段仍有业务逻辑,保留为显式属性
title_from_md: bool = True title_from_md: bool = True
body_start_keywords: list[str] = field(default_factory=lambda: ["绪论", "引言"]) body_start_keywords: list[str] = field(default_factory=lambda: ["绪论", "引言"])
body_end_keywords: list[str] = field( body_end_keywords: list[str] = field(
@@ -27,17 +25,8 @@ class ThesisConfig:
reference_style: str = "列出段落1" reference_style: str = "列出段落1"
def to_dict(self) -> dict: def to_dict(self) -> dict:
"""转成模板渲染用的扁平字典,排除 options 命名空间""" """透传 metadata模板变量来源"""
return { return self.metadata
"student_name": self.student_name,
"student_id": self.student_id,
"college": self.college,
"major": self.major,
"class": self.class_,
"advisor": self.advisor,
"advisor_title": self.advisor_title,
"title": self.title,
}
def load_config(path: str | Path) -> ThesisConfig: def load_config(path: str | Path) -> ThesisConfig:
@@ -50,14 +39,7 @@ def load_config(path: str | Path) -> ThesisConfig:
opts = raw.get("options", {}) opts = raw.get("options", {})
return ThesisConfig( return ThesisConfig(
student_name=meta.get("student_name", "<None>"), metadata=meta,
student_id=meta.get("student_id", "<None>"),
college=meta.get("college", "<None>"),
major=meta.get("major", "<None>"),
class_=meta.get("class", "<None>"),
advisor=meta.get("advisor", "<None>"),
advisor_title=meta.get("advisor_title", "<None>"),
title=meta.get("title", "<None>"),
title_from_md=opts.get("title_from_md", True), title_from_md=opts.get("title_from_md", True),
body_start_keywords=opts.get("body_start_keywords", ["绪论", "引言"]), body_start_keywords=opts.get("body_start_keywords", ["绪论", "引言"]),
body_end_keywords=opts.get( body_end_keywords=opts.get(

View File

@@ -15,7 +15,8 @@ from .body import body_to_paragraphs, replace_placeholder
from .references import references_to_paragraphs from .references import references_to_paragraphs
_TEXT_FIELDS = [ # 解析器可能产生的字段(用于填充报告)
_PARSER_FIELDS = [
"title", "title",
"abstact_cn_context", "abstact_cn_context",
"abstract_cn_keywords", "abstract_cn_keywords",
@@ -24,13 +25,7 @@ _TEXT_FIELDS = [
"acknowledgement", "acknowledgement",
"reference", "reference",
"appendix", "appendix",
"student_name", "body_md",
"student_id",
"college",
"major",
"class",
"advisor",
"advisor_title",
] ]
@@ -81,12 +76,11 @@ def generate_thesis(
body_end_kw=config.body_end_keywords, body_end_kw=config.body_end_keywords,
) )
# 3. 合并配置 → 上下文(配置优先 # 3. 合并配置 → 上下文(配置填充解析器未产生的空白
for k, v in config.to_dict().items(): for k, v in config.to_dict().items():
if k == "title" and config.title_from_md and context.get("title"): if k == "title" and config.title_from_md and context.get("title"):
continue # 以 markdown 标题为准 continue # 以 markdown 标题为准
if v != "<None>": context.setdefault(k, v)
context[k] = v
# 4. 用 defaultdict 兜底缺失键 # 4. 用 defaultdict 兜底缺失键
ctx = defaultdict(lambda: "<None>", context) ctx = defaultdict(lambda: "<None>", context)
@@ -129,17 +123,18 @@ def generate_thesis(
print(f"[完成] 论文生成完成: {output_path}") print(f"[完成] 论文生成完成: {output_path}")
# 10. 字段填充报告 # 10. 字段填充报告(动态收集所有模板与解析字段)
report_fields = list(dict.fromkeys([*config.metadata.keys(), *_PARSER_FIELDS]))
print("\n--- 字段填充情况 ---") print("\n--- 字段填充情况 ---")
for key in _TEXT_FIELDS: for key in report_fields:
val = ctx[key] val = ctx.get(key, "<None>")
if val == "<None>": if val == "<None>":
print(f" [缺失] {key}") print(f" [缺失] {key}")
else: else:
preview = str(val)[:60].replace("\n", " ") preview = str(val)[:60].replace("\n", " ")
print(f" [OK] {key}: {preview}...") print(f" [OK] {key}: {preview}...")
missing = [k for k in _TEXT_FIELDS if ctx[k] == "<None>"] missing = [k for k in report_fields if ctx.get(k, "<None>") == "<None>"]
if missing: if missing:
print("\n[警告] 以下字段缺失,已填充 '<None>'") print("\n[警告] 以下字段缺失,已填充 '<None>'")
for f in missing: for f in missing: