-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_example_usage.py
59 lines (40 loc) · 1.22 KB
/
update_example_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
"""Script to update the README sections:
- Example usage from `docs/example_usage.py`
- main script cli help from `python -m datatools help-all`
"""
import re
from datatools.__main__ import _recursive_help
path_py = "docs/example_usage.py"
path_readme = "README.md"
encoding = "utf-8"
section_readme = "## Example usage"
section_cli = "## Command line"
def replace_md_section(md_template: str, md_section: str, md_data: str) -> str:
# very simple implementation
pmatch = rf"{md_section}\s+```[^`]+```" # readme_section
psub = md_data
assert re.match(".*" + pmatch, md_template, re.DOTALL | re.MULTILINE), pmatch
return re.sub(pmatch, psub, md_template, re.DOTALL | re.MULTILINE)
# get CLI --help text
cli_help = _recursive_help()
with open(path_py, "r", encoding=encoding) as file:
py_code = file.read()
with open(path_readme, "r", encoding=encoding) as file:
md_readme = file.read()
py_md_readme = f"""
{section_readme}
```python
{py_code}
```
"""
cli_md_readme = f"""
{section_cli}
```bash
{cli_help}
```
"""
md_readme = replace_md_section(
md_template=md_readme, md_section=section_readme, md_data=py_md_readme
)
with open(path_readme, "w", encoding=encoding) as file:
file.write(md_readme)