-
Notifications
You must be signed in to change notification settings - Fork 0
/
upgrade_readme_usage.py
134 lines (108 loc) · 3.21 KB
/
upgrade_readme_usage.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf-8 -*-
import inspect
import itertools
import logging
import os
import re
import pymince.algorithm
import pymince.boolean
import pymince.dates
import pymince.dictionary
import pymince.file
import pymince.functional
import pymince.iterator
import pymince.json
import pymince.jsonlines
import pymince.logging
import pymince.std
import pymince.text
import pymince.warnings
import pymince.xml
modules = (
pymince.algorithm,
pymince.boolean,
pymince.dates,
pymince.dictionary,
pymince.file,
pymince.functional,
pymince.iterator,
pymince.json,
pymince.jsonlines,
pymince.logging,
pymince.std,
pymince.text,
pymince.warnings,
pymince.xml,
)
def cleandoc(obj):
docstring = obj.__doc__
docstring = inspect.cleandoc(docstring) if docstring else ""
return docstring
def member2markdown(member):
name = member.__name__
docstring = cleandoc(member)
try:
signature = inspect.signature(member)
except ValueError:
signature = ()
lines = (
f"##### {member.__name__}",
"```",
f"{name}{signature}",
"",
f"{docstring}",
"```",
)
return "\n".join(lines)
def getmembers(module):
def filtering(n):
return inspect.isclass(n) or inspect.isfunction(n)
yield from inspect.getmembers(module, filtering)
def module2markdown(module):
def members2markdown():
for _name, member in getmembers(module):
yield member2markdown(member)
module_name = os.path.basename(module.__file__)
module_desc = cleandoc(module)
lines = itertools.chain((f"\n#### {module_name}\n{module_desc}",), members2markdown())
return "\n".join(lines)
def make_table():
"""
| Modules | Tools |
| -----: | ----: |
| dictionary.py | [from_objects](#from_objects), [frozendict](#frozendict) |
| file.py | [ensure_directory](#ensure_directory) |
"""
header = "| PyModules | Tools |"
border = "| :-------- | :----- |"
table = [header, border]
for module in modules:
row = ""
module_name = os.path.basename(module.__file__)
row += f"| **{module_name}** |"
row += ", ".join((f"[*{m.__name__}*](#{m.__name__})" for _, m in getmembers(module)))
row += "|"
table.append(row)
table = "\n".join(table)
return table
if __name__ == "__main__":
base_dir = os.path.dirname(os.path.basename(__file__))
readme_path = os.path.join(base_dir, "README.md")
pattern = re.compile(r"### Usage.*(?=###)", flags=re.DOTALL)
content_title = "### Usage"
content_table = make_table()
content_items = "".join(module2markdown(module) for module in modules)
content_lines = (
content_title,
content_table,
content_items,
"", # ensure last \n
)
content = "\n".join(content_lines)
with open(readme_path, encoding="utf-8") as f:
old_string = f.read()
new_string = pattern.sub(content, old_string)
if new_string != old_string:
logging.basicConfig(level=logging.DEBUG)
with pymince.logging.timed_block("upgrade_readme_usage"), open(readme_path, mode="w", encoding="utf-8") as f:
f.write(new_string)