|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +import argparse |
| 4 | +import os |
| 5 | +import pathlib |
| 6 | + |
| 7 | +import yaml |
| 8 | + |
| 9 | +USERVER_ROOT = pathlib.Path(__file__).parent.parent.parent |
| 10 | + |
| 11 | + |
| 12 | +def normalize_default_value(data) -> str: |
| 13 | + if data is True: |
| 14 | + return 'true' |
| 15 | + elif data is False: |
| 16 | + return 'false' |
| 17 | + else: |
| 18 | + return data |
| 19 | + |
| 20 | + |
| 21 | +def format_schema(yaml) -> str: |
| 22 | + result = '' |
| 23 | + properties = yaml.get('properties', {}) |
| 24 | + if not properties: |
| 25 | + result += 'No options' |
| 26 | + return result |
| 27 | + |
| 28 | + max_key_size = max(len(key) for key in properties.keys()) |
| 29 | + max_value_size = max(len(value['description']) for value in properties.values()) |
| 30 | + |
| 31 | + result += f'{"Name":{max_key_size}} | {"Description":{max_value_size}} | Default value\n' |
| 32 | + result += f'{"":-<{max_key_size}} | {"":-<{max_value_size}} | {"":-<16}\n' |
| 33 | + |
| 34 | + for key, value in properties.items(): |
| 35 | + key = key.replace('\n', ' ') |
| 36 | + description = value['description'].replace('\n', ' ') |
| 37 | + default = normalize_default_value(value.get('defaultDescription', '--')) |
| 38 | + result += f'{key:{max_key_size}} | {description:{max_value_size}} | {default}\n' |
| 39 | + |
| 40 | + return result |
| 41 | + |
| 42 | + |
| 43 | +def parse_args(): |
| 44 | + parser = argparse.ArgumentParser() |
| 45 | + parser.add_argument('-o') |
| 46 | + parser.add_argument('yaml', nargs='+') |
| 47 | + return parser.parse_args() |
| 48 | + |
| 49 | + |
| 50 | +def handle_file(ifname: pathlib.Path, output_dir: pathlib.Path): |
| 51 | + with open(ifname) as ifile: |
| 52 | + content = yaml.load(ifile.read(), yaml.Loader) |
| 53 | + |
| 54 | + md = format_schema(content) |
| 55 | + |
| 56 | + output_path = output_dir / ifname.parent.relative_to(USERVER_ROOT) |
| 57 | + os.makedirs(str(output_path), exist_ok=True) |
| 58 | + |
| 59 | + output_file = output_path / (pathlib.Path(ifname).stem + '.md') |
| 60 | + print(output_file) |
| 61 | + |
| 62 | + with open(output_file, 'w') as ofile: |
| 63 | + ofile.write(md) |
| 64 | + |
| 65 | + |
| 66 | +def main(): |
| 67 | + args = parse_args() |
| 68 | + for fname in args.yaml: |
| 69 | + try: |
| 70 | + handle_file(pathlib.Path(fname), pathlib.Path(args.o)) |
| 71 | + except yaml.scanner.ScannerError as err: |
| 72 | + print(f'Failed to parse YAML from file {fname}: {err}') |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == '__main__': |
| 76 | + main() |
0 commit comments