2020-08-01 20:20:02 +00:00
|
|
|
# Generates config file
|
|
|
|
import configparser
|
|
|
|
import json
|
|
|
|
import argparse
|
|
|
|
from pathlib import Path
|
|
|
|
|
2021-05-16 22:00:37 +00:00
|
|
|
def fix_description(desc):
|
|
|
|
return "; " + desc.replace("\n", "\n; ")
|
2020-08-01 20:20:02 +00:00
|
|
|
|
2020-09-29 19:51:15 +00:00
|
|
|
def generate_ini(base_file, ini_file):
|
2020-08-01 20:20:02 +00:00
|
|
|
"""
|
|
|
|
Generates .ini file from config-base file.
|
|
|
|
"""
|
|
|
|
with open(Path(base_file), "r") as f:
|
|
|
|
config_base = json.load(f)
|
|
|
|
|
|
|
|
ini = configparser.RawConfigParser(allow_no_value=True)
|
|
|
|
|
2021-01-05 18:16:23 +00:00
|
|
|
for section in config_base["sections"]:
|
2020-08-01 20:20:02 +00:00
|
|
|
ini.add_section(section)
|
2021-01-05 18:16:23 +00:00
|
|
|
if "meta" in config_base["sections"][section]:
|
2021-05-16 22:00:37 +00:00
|
|
|
ini.set(section, fix_description(config_base["sections"][section]["meta"]["description"]))
|
2021-01-05 18:16:23 +00:00
|
|
|
for entry in config_base["sections"][section]["settings"]:
|
2023-06-21 11:28:52 +00:00
|
|
|
if config_base["sections"][section]["settings"][entry]["type"] == "note":
|
|
|
|
continue
|
2021-01-05 18:16:23 +00:00
|
|
|
if "description" in config_base["sections"][section]["settings"][entry]:
|
2021-05-16 22:00:37 +00:00
|
|
|
ini.set(section, fix_description(config_base["sections"][section]["settings"][entry]["description"]))
|
2021-01-05 18:16:23 +00:00
|
|
|
value = config_base["sections"][section]["settings"][entry]["value"]
|
|
|
|
if isinstance(value, bool):
|
|
|
|
value = str(value).lower()
|
|
|
|
else:
|
|
|
|
value = str(value)
|
|
|
|
ini.set(section, entry, value)
|
2020-08-01 20:20:02 +00:00
|
|
|
|
|
|
|
with open(Path(ini_file), "w") as config_file:
|
|
|
|
ini.write(config_file)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2020-10-17 23:57:53 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument("-i", "--input", help="input config base from jf-accounts")
|
|
|
|
parser.add_argument("-o", "--output", help="output ini")
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
2020-08-01 20:20:02 +00:00
|
|
|
|
2020-10-17 23:57:53 +00:00
|
|
|
print(generate_ini(base_file=args.input, ini_file=args.output))
|