-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate_config_docs.py
executable file
·55 lines (40 loc) · 1.79 KB
/
generate_config_docs.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
#!/usr/bin/env python
import os
from pathlib import Path
from sparglim.config.configer import SparkEnvConfiger
_HERE = Path(os.path.abspath(__file__)).parent
TEMPLATE_PATH = _HERE / "config-template.md"
OUTPUT_PATH = _HERE / "config.md"
def _generate_env_config_docs(config: dict) -> str:
docs = ""
for spark_config, (env, default) in config.items():
annotations = ""
if isinstance(env, list):
env = "` or `".join(env)
if env.endswith("_LIST"):
spark_config = spark_config.replace("list", "*")
annotations = " A string seperated by `,` will be converted"
docs += f"- `{env}`: `{spark_config}`, default: `{default}`.{annotations}\n"
return docs
def generate_docs(target_configer_cls) -> str:
docs = ""
source_code_path = target_configer_cls.__module__.replace(".", "/") + ".py"
docs += f"Source code: {source_code_path}\n\n"
docs += f"Avaliable environment variables for {target_configer_cls.__name__}:\n\n"
docs += f"Default config:\n\n"
docs += _generate_env_config_docs(target_configer_cls.default_config_mapper)
items = target_configer_cls.__dict__.items()
config_map = {k: v for k, v in items if k.startswith("_") and isinstance(v, dict)}
for config_suffix, config in config_map.items():
docs += f"\n`config{config_suffix}()` can config following:\n\n"
docs += _generate_env_config_docs(config)
return docs
def generate_from_template(docs: str):
print(f"Generating docs... {TEMPLATE_PATH.as_posix()} -> {OUTPUT_PATH.as_posix()}")
print(docs)
template = TEMPLATE_PATH.read_text()
template = template.format(docs=docs)
OUTPUT_PATH.write_text(template)
if __name__ == "__main__":
docs = generate_docs(SparkEnvConfiger)
generate_from_template(docs)