add config table generator script
This commit is contained in:
parent
79ae291ed1
commit
a6aae704c6
|
@ -1,93 +1,80 @@
|
||||||
|
import re
|
||||||
import requests
|
import requests
|
||||||
|
from typing import Tuple, Dict, Any
|
||||||
|
|
||||||
|
def extract_config_param(line: str, param: str) -> str:
|
||||||
|
return re.split(f'{param}: ', line)[1].strip()[1:].split('"')[0].strip()
|
||||||
|
|
||||||
def extract_config_param(line, param):
|
def remove_extra_char(string: str, char: str) -> str:
|
||||||
return line.split(f"{param}: ")[1].strip()[1:].split('"')[0].strip()
|
return string[:-1] if string[-1] == char else string
|
||||||
|
|
||||||
|
def parse_table_heading(line: str) -> Tuple[str, bool]:
|
||||||
def remove_extra_char(string, char):
|
table_heading = re.split('##', line)[1].strip()
|
||||||
if string[-1] == char:
|
if 'config' not in table_heading or 'Application-level' in table_heading:
|
||||||
string = string[:-1]
|
|
||||||
return string
|
|
||||||
|
|
||||||
|
|
||||||
def parse_table_heading(line):
|
|
||||||
table_heading = line.split("##")[1].strip()
|
|
||||||
# ensure only config blocks are captured
|
|
||||||
if "config" not in table_heading or "Application-level" in table_heading:
|
|
||||||
return None, False
|
return None, False
|
||||||
else:
|
|
||||||
table_heading = table_heading.title()
|
table_heading = table_heading.title()
|
||||||
|
|
||||||
# ensuring heading consistency
|
word_replace_re = re.compile('|'.join([
|
||||||
words = [
|
r'(Configuration)', r'(And)', r'(Lightpush)',
|
||||||
("Configuration", "Config"),
|
r'(Json-Rpc)', r'(Rest Http)', r'(Dns)',
|
||||||
("And", "and"),
|
r'(V5)', r'(Websocket)'
|
||||||
("Lightpush", "Light Push"),
|
]))
|
||||||
("Json-Rpc", "JSON-RPC"),
|
word_replace_dict = {
|
||||||
("Rest Http", "REST HTTP"),
|
'Configuration': 'Config', 'And': 'and', 'Lightpush': 'Light Push',
|
||||||
("Dns", "DNS"),
|
'Json-Rpc': 'JSON-RPC', 'Rest Http': 'REST HTTP', 'Dns': 'DNS',
|
||||||
("V5", "v5"),
|
'V5': 'v5', 'Websocket': 'WebSocket'
|
||||||
("Websocket", "WebSocket")
|
}
|
||||||
]
|
table_heading = word_replace_re.sub(lambda match: word_replace_dict[match.group(0)], table_heading)
|
||||||
for word in words:
|
return '## ' + table_heading, True
|
||||||
table_heading = table_heading.replace(word[0], word[1])
|
|
||||||
return "## " + table_heading, True
|
|
||||||
|
|
||||||
|
def fetch_config_file(config_path: str) -> str:
|
||||||
def extract_config(config_path):
|
config_file = requests.get(config_path)
|
||||||
# fetch the config file
|
if config_file.status_code == 200:
|
||||||
config_data = requests.get(config_path)
|
return config_file.text.split("\n")
|
||||||
if config_data.status_code == 200:
|
|
||||||
config_data = config_data.text.split("\n")
|
|
||||||
else:
|
else:
|
||||||
exit("An error occurred while fetching the config file")
|
exit("An error occurred while fetching the config file")
|
||||||
|
|
||||||
|
def extract_config(config_path: str) -> str:
|
||||||
|
config_data = fetch_config_file(config_path)
|
||||||
|
|
||||||
config_table = "## Application-Level Config\n\n| Name | Default Value | Description |\n| - | - | - |\n"
|
config_table = "## Application-Level Config\n\n| Name | Default Value | Description |\n| - | - | - |\n"
|
||||||
row = {"name": None, "default": "", "description": None}
|
row = {"name": None, "default": "", "description": None}
|
||||||
for line in config_data:
|
for line in config_data:
|
||||||
line = line.strip()
|
line = line.strip()
|
||||||
|
|
||||||
# we've left a config block
|
|
||||||
if line == "":
|
if line == "":
|
||||||
# check if there's a config
|
|
||||||
if row["description"] is not None and row["name"] != "topics":
|
if row["description"] is not None and row["name"] != "topics":
|
||||||
# patch since this config executes Nim
|
|
||||||
if row["name"] == "store-message-retention-policy":
|
if row["name"] == "store-message-retention-policy":
|
||||||
row["default"] = "time:172800"
|
row["default"] = "time:172800"
|
||||||
# patch as nat config name is missing
|
|
||||||
if row["name"] is None:
|
if row["name"] is None:
|
||||||
row["name"], row["default"] = "nat", "any"
|
row["name"], row["default"] = "nat", "any"
|
||||||
row["description"] += ". Must be one of: any, none, upnp, pmp, extip:<IP>"
|
row["description"] += ". Must be one of: any, none, upnp, pmp, extip:<IP>"
|
||||||
config_table += f"| `{row['name']}` | {row['default']} | {row['description']} |\n"
|
config_table += f"| `{row['name']}` | {row['default']} | {row['description']} |\n"
|
||||||
row = {"name": None, "default": "", "description": None}
|
row = {"name": None, "default": "", "description": None}
|
||||||
|
|
||||||
# create a new config config_table
|
|
||||||
if line.startswith("## "):
|
if line.startswith("## "):
|
||||||
table_heading, is_valid_heading = parse_table_heading(line)
|
table_heading, is_valid_heading = parse_table_heading(line)
|
||||||
if is_valid_heading:
|
if is_valid_heading:
|
||||||
config_table += f"\n{table_heading}\n\n| Name | Default Value | Description |\n| - | - | - |\n"
|
config_table += f"\n{table_heading}\n\n| Name | Default Value | Description |\n| - | - | - |\n"
|
||||||
|
|
||||||
# extract the config name
|
|
||||||
if line.startswith("name:"):
|
if line.startswith("name:"):
|
||||||
row["name"] = extract_config_param(line, "name")
|
row["name"] = extract_config_param(line, "name")
|
||||||
|
|
||||||
# extract the config default value
|
|
||||||
if line.startswith("defaultValue:"):
|
if line.startswith("defaultValue:"):
|
||||||
default_value = line.split("defaultValue: ")[1].strip()
|
default_value = re.split("defaultValue: ", line)[1].strip()
|
||||||
if '""' not in default_value:
|
if '""' not in default_value:
|
||||||
default_value = f"`{remove_extra_char(default_value, ',')}`".replace('"', "")
|
default_value = f"`{remove_extra_char(default_value, ',')}`".replace('"', "")
|
||||||
if "ValidIpAddress.init" in default_value:
|
if "ValidIpAddress.init" in default_value:
|
||||||
default_value = default_value.replace("ValidIpAddress.init(", "").replace(")", "")
|
default_value = default_value.replace("ValidIpAddress.init(", "").replace(")", "")
|
||||||
row["default"] = default_value
|
row["default"] = default_value
|
||||||
|
|
||||||
# extract the config description
|
|
||||||
if line.startswith("desc:"):
|
if line.startswith("desc:"):
|
||||||
description = remove_extra_char(extract_config_param(line, "desc"), ".").replace("|", "\|")
|
description = remove_extra_char(extract_config_param(line, "desc"), ".").replace("|", "\|")
|
||||||
row["description"] = description[0].upper() + description[1:]
|
row["description"] = description[0].upper() + description[1:]
|
||||||
|
|
||||||
return config_table.replace(">", "\>")
|
return config_table.replace(">", "\>")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
config_path = "https://raw.githubusercontent.com/waku-org/nwaku/master/apps/wakunode2/config.nim"
|
config_path = "https://raw.githubusercontent.com/waku-org/nwaku/master/apps/wakunode2/config.nim"
|
||||||
table_data = extract_config(config_path)
|
table_data = extract_config(config_path)
|
||||||
|
|
Loading…
Reference in New Issue