|
| 1 | +import re |
1 | 2 | import requests
|
| 3 | +from typing import Tuple, Dict, Any |
2 | 4 |
|
| 5 | +def extract_config_param(line: str, param: str) -> str: |
| 6 | + return re.split(f'{param}: ', line)[1].strip()[1:].split('"')[0].strip() |
3 | 7 |
|
4 |
| -def extract_config_param(line, param): |
5 |
| - return line.split(f"{param}: ")[1].strip()[1:].split('"')[0].strip() |
| 8 | +def remove_extra_char(string: str, char: str) -> str: |
| 9 | + return string[:-1] if string[-1] == char else string |
6 | 10 |
|
7 |
| - |
8 |
| -def remove_extra_char(string, char): |
9 |
| - if string[-1] == char: |
10 |
| - string = string[:-1] |
11 |
| - return string |
12 |
| - |
13 |
| - |
14 |
| -def parse_table_heading(line): |
15 |
| - table_heading = line.split("##")[1].strip() |
16 |
| - # ensure only config blocks are captured |
17 |
| - if "config" not in table_heading or "Application-level" in table_heading: |
| 11 | +def parse_table_heading(line: str) -> Tuple[str, bool]: |
| 12 | + table_heading = re.split('##', line)[1].strip() |
| 13 | + if 'config' not in table_heading or 'Application-level' in table_heading: |
18 | 14 | return None, False
|
19 |
| - else: |
20 |
| - table_heading = table_heading.title() |
| 15 | + table_heading = table_heading.title() |
21 | 16 |
|
22 |
| - # ensuring heading consistency |
23 |
| - words = [ |
24 |
| - ("Configuration", "Config"), |
25 |
| - ("And", "and"), |
26 |
| - ("Lightpush", "Light Push"), |
27 |
| - ("Json-Rpc", "JSON-RPC"), |
28 |
| - ("Rest Http", "REST HTTP"), |
29 |
| - ("Dns", "DNS"), |
30 |
| - ("V5", "v5"), |
31 |
| - ("Websocket", "WebSocket") |
32 |
| - ] |
33 |
| - for word in words: |
34 |
| - table_heading = table_heading.replace(word[0], word[1]) |
35 |
| - return "## " + table_heading, True |
| 17 | + word_replace_re = re.compile('|'.join([ |
| 18 | + r'(Configuration)', r'(And)', r'(Lightpush)', |
| 19 | + r'(Json-Rpc)', r'(Rest Http)', r'(Dns)', |
| 20 | + r'(V5)', r'(Websocket)' |
| 21 | + ])) |
| 22 | + word_replace_dict = { |
| 23 | + 'Configuration': 'Config', 'And': 'and', 'Lightpush': 'Light Push', |
| 24 | + 'Json-Rpc': 'JSON-RPC', 'Rest Http': 'REST HTTP', 'Dns': 'DNS', |
| 25 | + 'V5': 'v5', 'Websocket': 'WebSocket' |
| 26 | + } |
| 27 | + table_heading = word_replace_re.sub(lambda match: word_replace_dict[match.group(0)], table_heading) |
| 28 | + return '## ' + table_heading, True |
36 | 29 |
|
37 |
| - |
38 |
| -def extract_config(config_path): |
39 |
| - # fetch the config file |
40 |
| - config_data = requests.get(config_path) |
41 |
| - if config_data.status_code == 200: |
42 |
| - config_data = config_data.text.split("\n") |
| 30 | +def fetch_config_file(config_path: str) -> str: |
| 31 | + config_file = requests.get(config_path) |
| 32 | + if config_file.status_code == 200: |
| 33 | + return config_file.text.split("\n") |
43 | 34 | else:
|
44 | 35 | exit("An error occurred while fetching the config file")
|
45 | 36 |
|
| 37 | +def extract_config(config_path: str) -> str: |
| 38 | + config_data = fetch_config_file(config_path) |
| 39 | + |
46 | 40 | config_table = "## Application-Level Config\n\n| Name | Default Value | Description |\n| - | - | - |\n"
|
47 | 41 | row = {"name": None, "default": "", "description": None}
|
48 | 42 | for line in config_data:
|
49 | 43 | line = line.strip()
|
50 | 44 |
|
51 |
| - # we've left a config block |
52 | 45 | if line == "":
|
53 |
| - # check if there's a config |
54 | 46 | if row["description"] is not None and row["name"] != "topics":
|
55 |
| - # patch since this config executes Nim |
56 | 47 | if row["name"] == "store-message-retention-policy":
|
57 | 48 | row["default"] = "time:172800"
|
58 |
| - # patch as nat config name is missing |
59 | 49 | if row["name"] is None:
|
60 | 50 | row["name"], row["default"] = "nat", "any"
|
61 | 51 | row["description"] += ". Must be one of: any, none, upnp, pmp, extip:<IP>"
|
62 | 52 | config_table += f"| `{row['name']}` | {row['default']} | {row['description']} |\n"
|
63 | 53 | row = {"name": None, "default": "", "description": None}
|
64 | 54 |
|
65 |
| - # create a new config config_table |
66 | 55 | if line.startswith("## "):
|
67 | 56 | table_heading, is_valid_heading = parse_table_heading(line)
|
68 | 57 | if is_valid_heading:
|
69 | 58 | config_table += f"\n{table_heading}\n\n| Name | Default Value | Description |\n| - | - | - |\n"
|
70 | 59 |
|
71 |
| - # extract the config name |
72 | 60 | if line.startswith("name:"):
|
73 | 61 | row["name"] = extract_config_param(line, "name")
|
74 | 62 |
|
75 |
| - # extract the config default value |
76 | 63 | if line.startswith("defaultValue:"):
|
77 |
| - default_value = line.split("defaultValue: ")[1].strip() |
| 64 | + default_value = re.split("defaultValue: ", line)[1].strip() |
78 | 65 | if '""' not in default_value:
|
79 | 66 | default_value = f"`{remove_extra_char(default_value, ',')}`".replace('"', "")
|
80 | 67 | if "ValidIpAddress.init" in default_value:
|
81 | 68 | default_value = default_value.replace("ValidIpAddress.init(", "").replace(")", "")
|
82 | 69 | row["default"] = default_value
|
83 | 70 |
|
84 |
| - # extract the config description |
85 | 71 | if line.startswith("desc:"):
|
86 | 72 | description = remove_extra_char(extract_config_param(line, "desc"), ".").replace("|", "\|")
|
87 | 73 | row["description"] = description[0].upper() + description[1:]
|
88 |
| - |
| 74 | + |
89 | 75 | return config_table.replace(">", "\>")
|
90 | 76 |
|
| 77 | + |
91 | 78 | if __name__ == "__main__":
|
92 | 79 | config_path = "https://raw.githubusercontent.com/waku-org/nwaku/master/apps/wakunode2/config.nim"
|
93 | 80 | table_data = extract_config(config_path)
|
|
0 commit comments