#!/bin/python3
######################################################################################################
# #
# DO NOT CHANGE THE INDENTATION IN THIS FILE! #
# #
######################################################################################################
import json, re
CONFIG_FILE = "../components/Config.qml"
JSON_FILE = "options.json"
MD_FILE = "options.md"
HELP_FILE = "help.html"
def parse_config():
with open(CONFIG_FILE, "r") as file:
json_output = {}
for line in file:
line = line.strip()
if line.startswith("property"):
spaced_line = line.split(" ")
type = spaced_line[1]
property = spaced_line[2].replace(":", "")
quoted_line = re.findall('["|\'](.+?)["|\']', line)[0]
split_quoted_line = quoted_line.split("/")
category = split_quoted_line[0] if "/" in quoted_line else "General"
option = split_quoted_line[1] if "/" in quoted_line else split_quoted_line[0]
default = ""
if "@default:" in line:
default = line.split("@default:")[1].split("@")[0].strip()
elif "||" in line:
default = line.split("||")[1].strip().replace('"', "").split("//")[0].strip()
elif type == "bool" and "config[" in line:
default = "true"
elif type == "bool":
default = "false"
elif type == "int":
default = "0"
elif type == "real":
default = "0.0"
possible = ""
if "@possible:" in line:
possible = line.split("@possible:")[1].split("@")[0].strip()
elif type == "color":
possible = "QColor"
elif type == "bool":
possible = "'true' | 'false'"
elif type == "string" and "clock" in property.lower() and "format" in property.lower():
possible = "QTime"
elif type == "string" and "date" in property.lower() and "format" in property.lower():
possible = "QDate"
desc = line.split("@desc:")[1].split("@")[0].strip() if "@desc:" in line else ""
if category not in json_output:
json_output[category] = []
json_output[category].append({
"category": category,
"option": option,
"type": type,
"default": default,
"possible": possible,
"desc": desc,
"property": property
})
return json_output
def generate_json_file(json_output):
jsonf = json.dumps(json_output)
with open(JSON_FILE, "w") as file:
file.write(jsonf)
print(f"Generated `{JSON_FILE}`")
def generate_md_file(json_output):
table = ['
| Category | Option | Type | Default value | Possible values |
']
ref = ["\n-- --"]
for i, (category, options) in enumerate(json_output.items()):
# category title
ref.append(f'\n## [{category}]')
for j, option in enumerate(options):
# TABLE
table.append("")
if j == 0:
table.append(f'| [{category}] | ')
formated_possible = ""
if option["possible"] == "QColor":
formated_possible = 'QColor'
elif option["possible"] == "QTime":
formated_possible = 'QTime format string'
elif option["possible"] == "QDate":
formated_possible = 'QDate format string'
else:
formated_possible = option["possible"]
table.append(f'''
{option["option"]}
|
{option["type"]} |
{option["default"]} |
{formated_possible.replace("'", "`").replace("|", " ").strip()}
| ''')
table.append("
")
# REFERENCE
# option reference:
ref.append(f'''
{option["option"]}
{option["desc"]}
| Type |
{option["type"]} |
| Default value |
{option["default"]} |
| Possible values |
{formated_possible.replace("'", "`").replace("|", " ").strip()}
|
''')
if i != len(json_output) - 1:
table.append('| |
')
table.append('| Category | Option | Type | Default value | Possible values |
')
ref.append('\n-- --')
table.append('
\n')
with open(MD_FILE, "w") as file:
file.write("".join(table) + "\n".join(ref))
print(f"Generated `{MD_FILE}`")
if __name__ == "__main__":
json_output = parse_config()
generate_json_file(json_output)
generate_md_file(json_output)