config-table-generator.py
1 import re 2 import requests 3 from typing import Tuple, Dict, Any 4 5 def extract_config_param(line: str, param: str) -> str: 6 return re.split(f'{param}: ', line)[1].strip()[1:].split('"')[0].strip() 7 8 def remove_extra_char(string: str, char: str) -> str: 9 return string[:-1] if string[-1] == char else string 10 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: 14 return None, False 15 table_heading = table_heading.capitalize() 16 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'(Discovery 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 'Discovery V5': 'Discv5', '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 29 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") 34 else: 35 exit("An error occurred while fetching the config file") 36 37 def extract_config(config_path: str) -> str: 38 config_data = fetch_config_file(config_path) 39 40 config_table = "## Application-level config\n\n| Name | Default Value | Description |\n| - | - | - |\n" 41 row = {"name": None, "default": "", "description": None} 42 for line in config_data: 43 line = line.strip() 44 45 if line == "": 46 if row["description"] is not None and row["name"] != "topics": 47 if row["name"] == "store-message-retention-policy": 48 row["default"] = "time:172800" 49 if row["name"] is None: 50 row["name"], row["default"] = "nat", "any" 51 row["description"] += ". Must be one of: any, none, upnp, pmp, extip:<IP>" 52 config_table += f"| `{row['name']}` | {row['default']} | {row['description']} |\n" 53 row = {"name": None, "default": "", "description": None} 54 55 if line.startswith("## "): 56 table_heading, is_valid_heading = parse_table_heading(line) 57 if is_valid_heading: 58 config_table += f"\n{table_heading}\n\n| Name | Default Value | Description |\n| - | - | - |\n" 59 60 if line.startswith("name:"): 61 row["name"] = extract_config_param(line, "name") 62 63 if line.startswith("defaultValue:"): 64 default_value = re.split("defaultValue: ", line)[1].strip() 65 if '""' not in default_value: 66 default_value = f"`{remove_extra_char(default_value, ',')}`".replace("@", "") 67 if "[" not in default_value: 68 default_value = default_value.replace('"', "") 69 if "ValidIpAddress.init" in default_value: 70 default_value = default_value.replace("ValidIpAddress.init(", "").replace(")", "") 71 row["default"] = default_value 72 73 if line.startswith("desc:"): 74 description = remove_extra_char(extract_config_param(line, "desc"), ".").replace("|", "\|") 75 row["description"] = description[0].upper() + description[1:] 76 77 return config_table.replace(">", "\>") 78 79 80 if __name__ == "__main__": 81 config_path = "https://raw.githubusercontent.com/waku-org/nwaku/master/apps/wakunode2/external_config.nim" 82 table_data = extract_config(config_path) 83 print(table_data)