Skip to content

Commit 5bf92a2

Browse files
committed
build(docs): 重构开源声明生成逻辑
- 更新 os_notices.toml 配置文件,增加模板和输出路径 - 重写 generate_os_notices.py 脚本,使用模板生成文档 - 修改 .vscode/settings.json,将 os_notices.md 添加到文件嵌套模式
1 parent 8143bd8 commit 5bf92a2

File tree

5 files changed

+79
-49
lines changed

5 files changed

+79
-49
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"explorer.fileNesting.enabled": true,
33
"explorer.fileNesting.patterns": {
44
"pyproject.toml": "pdm.lock, .editorconfig, .markdownlint.json",
5-
"vcf_generator_lite.spec": "setup.iss, metadata.yml, versionfile.txt",
5+
"vcf_generator_lite.spec": "setup.iss, metadata.yml, versionfile.txt, os_notices.*",
66
"*": "${basename}.*.${extname}",
77
}
88
}

docs/legal/os_notices.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
<!-- Generated by scripts/generate_os_notice.py, DO NOT DIRECTLY EDIT THIS FILE. -->
1+
<!-- Generated by 'pdm run docs_generate_os_notices', DO NOT DIRECTLY EDIT THIS FILE. -->
2+
<!-- The template is located at 'docs/os_notices_template.py' -->
23
<!-- markdownlint-disable -->
34
<!-- @formatter:off -->
45

56
# 开源声明
67

78
| 开源项目 | 许可证 | 版权声明 |
8-
| -------- | ------ | -------- |
9-
| [CPython](https://github.com/python/cpython) | [`Python license`](https://docs.python.org/3/license.html) | Copyright © 2001 Python Software Foundation. All rights reserved.<br />Copyright © 2000 BeOpen.com. All rights reserved.<br />Copyright © 1995-2001 Corporation for National Research Initiatives. All rights reserved.<br />Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved. |
10-
| [tkhtmlview](https://github.com/bauripalash/tkhtmlview) | [`MIT License`](https://github.com/bauripalash/tkhtmlview/blob/f7ded5584773f3470a98cf078f10cafb8eca14ba/LICENSE) | Copyright (c) 2020 Palash Bauri<br />Copyright (c) 2018 paolo-gurisatti |
11-
| [TtkText](https://github.com/Jesse205/TtkText) | [`MIT License`](https://github.com/Jesse205/TtkText/blob/v0.1.3/LICENSE) | Copyright (c) 2025 Jesse205 |
9+
| --- | --- | --- |
10+
| [CPython](https://github.com/python/cpython) | [Python license](https://docs.python.org/3/license.html) | Copyright © 2001 Python Software Foundation. All rights reserved.<br />Copyright © 2000 BeOpen.com. All rights reserved.<br />Copyright © 1995-2001 Corporation for National Research Initiatives. All rights reserved.<br />Copyright © 1991-1995 Stichting Mathematisch Centrum. All rights reserved. |
11+
| [tkhtmlview](https://github.com/bauripalash/tkhtmlview) | [MIT License](https://github.com/bauripalash/tkhtmlview/blob/f7ded5584773f3470a98cf078f10cafb8eca14ba/LICENSE) | Copyright (c) 2020 Palash Bauri<br />Copyright (c) 2018 paolo-gurisatti |
12+
| [TtkText](https://github.com/Jesse205/TtkText) | [MIT License](https://github.com/Jesse205/TtkText/blob/v0.1.3/LICENSE) | Copyright (c) 2025 Jesse205 |

docs/legal/os_notices_template.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from typing import Any
2+
3+
4+
def make_table(headers: list[Any], rows: list[list[Any]]):
5+
return "\n".join(
6+
[
7+
f"| {" | ".join(headers)} |",
8+
f"| {" | ".join(["---"] * len(headers))} |",
9+
*[f"| {" | ".join(row)} |" for row in rows],
10+
]
11+
)
12+
13+
14+
def make_url(text: str, url: str) -> str:
15+
return f"[{text}]({url})"
16+
17+
18+
output = f"""
19+
<!-- Generated by 'pdm run docs_generate_os_notices', DO NOT DIRECTLY EDIT THIS FILE. -->
20+
<!-- The template is located at 'docs/os_notices_template.py' -->
21+
<!-- markdownlint-disable -->
22+
<!-- @formatter:off -->
23+
24+
# 开源声明
25+
26+
{make_table(
27+
["开源项目", "许可证", "版权声明"],
28+
[
29+
[
30+
make_url(notice["name"], notice["repository"]) if "repository" in notice else notice["name"],
31+
make_url(notice["license"], notice["license-url"]),
32+
"<br />".join(notice["copyrights"]),
33+
]
34+
# noinspection PyUnresolvedReferences
35+
for notice in notices # type: ignore
36+
],
37+
)}
38+
""".lstrip()

os_notices.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
template = "./docs/legal/os_notices_template.py"
2+
output = "./docs/legal/os_notices.md"
3+
14
[[notices]]
25
name = "CPython"
36
repository = "https://github.com/python/cpython"
Lines changed: 31 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,48 @@
11
import importlib.metadata
2+
import runpy
23
import tomllib
34
from typing import TypedDict
45

5-
PATH_OS_NOTICE_DOC = "docs/legal/os_notices.md"
66
PATH_OS_NOTICE_DATA = "os_notices.toml"
77

8-
TEMPLATE_OS_NOTICE_DOC = """<!-- Generated by scripts/generate_os_notice.py, DO NOT DIRECTLY EDIT THIS FILE. -->
9-
<!-- markdownlint-disable -->
10-
<!-- @formatter:off -->
8+
Notice = TypedDict(
9+
"Notice",
10+
{
11+
"name": str,
12+
"dependency": str,
13+
"repository": str,
14+
"license": str,
15+
"license-url": str,
16+
"copyrights": list[str],
17+
},
18+
)
1119

12-
# 开源声明
13-
14-
| 开源项目 | 许可证 | 版权声明 |
15-
| -------- | ------ | -------- |
16-
{notices}
17-
"""
18-
TEMPLATE_OS_NOTICE_DOC_NOTICE = """| {name} | {license} | {copyrights} |"""
19-
TEMPLATE_OS_NOTICE_DOC_NOTICE_LICENSE = """[`{license}`]({license_url})"""
20-
21-
Notice = TypedDict("Notice", {
22-
"name": str,
23-
"dependency": str,
24-
"repository": str,
25-
"license": str,
26-
"license-url": str,
27-
"copyrights": list[str]
28-
})
20+
NoticesConfig = TypedDict(
21+
"NoticesConfig", {"template": str, "output": str, "notices": list[Notice]}
22+
)
2923

3024

3125
def format_url(url: str, notice: Notice) -> str:
3226
if "dependency" not in notice:
3327
return url
34-
return url.format(
35-
version=importlib.metadata.version(notice["dependency"])
36-
)
37-
38-
39-
def generate_os_notice(notices: list[Notice]) -> str:
40-
return TEMPLATE_OS_NOTICE_DOC.format(
41-
notices="\n".join([
42-
TEMPLATE_OS_NOTICE_DOC_NOTICE.format(
43-
name=f"[{notice["name"]}]({notice["repository"]})" if "repository" in notice else notice["name"],
44-
license=TEMPLATE_OS_NOTICE_DOC_NOTICE_LICENSE.format(
45-
license=notice["license"],
46-
license_url=format_url(notice["license-url"], notice)
47-
),
48-
copyrights="<br />".join(notice["copyrights"])
49-
)
50-
for notice in notices
51-
])
52-
)
28+
return url.format(version=importlib.metadata.version(notice["dependency"]))
29+
30+
31+
def process_notices(notices: list[Notice]):
32+
return [
33+
{**notice, "license-url": format_url(notice["license-url"], notice)}
34+
for notice in notices
35+
]
5336

5437

5538
def main() -> int:
5639
with open(PATH_OS_NOTICE_DATA, "rb") as f:
57-
data = tomllib.load(f)
58-
with open(PATH_OS_NOTICE_DOC, "w", encoding="utf-8") as f:
59-
f.write(generate_os_notice(notices=data["notices"]))
40+
config = NoticesConfig(**tomllib.load(f))
41+
42+
with open(config["output"], "w", encoding="utf-8") as f:
43+
output = runpy.run_path(
44+
config["template"],
45+
init_globals={"notices": process_notices(notices=config["notices"])},
46+
)["output"]
47+
f.write(output)
6048
return 0

0 commit comments

Comments
 (0)