Skip to content

Commit a6aae70

Browse files
committed
add config table generator script
1 parent 79ae291 commit a6aae70

File tree

1 file changed

+32
-45
lines changed

1 file changed

+32
-45
lines changed

scripts/config-table-generator.py

+32-45
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,80 @@
1+
import re
12
import requests
3+
from typing import Tuple, Dict, Any
24

5+
def extract_config_param(line: str, param: str) -> str:
6+
return re.split(f'{param}: ', line)[1].strip()[1:].split('"')[0].strip()
37

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
610

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:
1814
return None, False
19-
else:
20-
table_heading = table_heading.title()
15+
table_heading = table_heading.title()
2116

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
3629

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")
4334
else:
4435
exit("An error occurred while fetching the config file")
4536

37+
def extract_config(config_path: str) -> str:
38+
config_data = fetch_config_file(config_path)
39+
4640
config_table = "## Application-Level Config\n\n| Name | Default Value | Description |\n| - | - | - |\n"
4741
row = {"name": None, "default": "", "description": None}
4842
for line in config_data:
4943
line = line.strip()
5044

51-
# we've left a config block
5245
if line == "":
53-
# check if there's a config
5446
if row["description"] is not None and row["name"] != "topics":
55-
# patch since this config executes Nim
5647
if row["name"] == "store-message-retention-policy":
5748
row["default"] = "time:172800"
58-
# patch as nat config name is missing
5949
if row["name"] is None:
6050
row["name"], row["default"] = "nat", "any"
6151
row["description"] += ". Must be one of: any, none, upnp, pmp, extip:<IP>"
6252
config_table += f"| `{row['name']}` | {row['default']} | {row['description']} |\n"
6353
row = {"name": None, "default": "", "description": None}
6454

65-
# create a new config config_table
6655
if line.startswith("## "):
6756
table_heading, is_valid_heading = parse_table_heading(line)
6857
if is_valid_heading:
6958
config_table += f"\n{table_heading}\n\n| Name | Default Value | Description |\n| - | - | - |\n"
7059

71-
# extract the config name
7260
if line.startswith("name:"):
7361
row["name"] = extract_config_param(line, "name")
7462

75-
# extract the config default value
7663
if line.startswith("defaultValue:"):
77-
default_value = line.split("defaultValue: ")[1].strip()
64+
default_value = re.split("defaultValue: ", line)[1].strip()
7865
if '""' not in default_value:
7966
default_value = f"`{remove_extra_char(default_value, ',')}`".replace('"', "")
8067
if "ValidIpAddress.init" in default_value:
8168
default_value = default_value.replace("ValidIpAddress.init(", "").replace(")", "")
8269
row["default"] = default_value
8370

84-
# extract the config description
8571
if line.startswith("desc:"):
8672
description = remove_extra_char(extract_config_param(line, "desc"), ".").replace("|", "\|")
8773
row["description"] = description[0].upper() + description[1:]
88-
74+
8975
return config_table.replace(">", "\>")
9076

77+
9178
if __name__ == "__main__":
9279
config_path = "https://raw.githubusercontent.com/waku-org/nwaku/master/apps/wakunode2/config.nim"
9380
table_data = extract_config(config_path)

0 commit comments

Comments
 (0)