|
| 1 | +import csv |
| 2 | +import sys |
| 3 | + |
| 4 | +import yaml |
| 5 | + |
| 6 | +KEEP_FIELDS = ["name", "label", "title", "url", "description", "path"] |
| 7 | +SKIP_DICT = {} |
| 8 | +HELP_TEXT = """ |
| 9 | +This script processes a comparison report, in the form of a csv file, and outputs a constants file. |
| 10 | +This constants file can then be used to run another comparison report with more filtered results. |
| 11 | +
|
| 12 | +Usage: |
| 13 | + python gen_constants.py <input_csv_file> [output_yaml_file] |
| 14 | +
|
| 15 | +Arguments: |
| 16 | + <input_csv_file> - The path to the CSV file to be processed. |
| 17 | + [output_yaml_file] - Optional. The path where the resulting YAML file will be saved. |
| 18 | + If not provided, the output will be saved as 'constants.yaml'. |
| 19 | +
|
| 20 | +Example: |
| 21 | + python gen_constants.py my_results.csv my_constants.yaml |
| 22 | +""" |
| 23 | + |
| 24 | +csv.field_size_limit(sys.maxsize) |
| 25 | + |
| 26 | + |
| 27 | +def filter_parts(parts): |
| 28 | + for check in KEEP_FIELDS: |
| 29 | + if check in parts[-1]: |
| 30 | + return True |
| 31 | + |
| 32 | + |
| 33 | +def add_path_original(path, dest=None): |
| 34 | + if dest is None: |
| 35 | + dest = SKIP_DICT |
| 36 | + curr = path.pop(0) |
| 37 | + if not (c_val := dest.get(curr, {})): |
| 38 | + dest[curr] = c_val |
| 39 | + if len(path) >= 2: |
| 40 | + add_path_original(path, c_val) |
| 41 | + elif path: |
| 42 | + dest[curr].update({path.pop(0): {}}) |
| 43 | + else: |
| 44 | + dest[curr] = c_val |
| 45 | + |
| 46 | + |
| 47 | +def simplify(indict): |
| 48 | + as_list = False |
| 49 | + for key in indict: |
| 50 | + if isinstance(indict[key], dict): |
| 51 | + indict[key] = simplify(indict[key]) |
| 52 | + if not indict[key]: |
| 53 | + as_list = True |
| 54 | + if as_list: # convert the dictionary into a list |
| 55 | + res = [] |
| 56 | + for key, val in indict.items(): |
| 57 | + if not val: |
| 58 | + res.append(key) |
| 59 | + else: |
| 60 | + res.append({key: val}) |
| 61 | + return res |
| 62 | + if len(indict.items()) == 1: |
| 63 | + if not next(iter(indict.values())): |
| 64 | + return next(iter(indict.keys())) |
| 65 | + if any([isinstance(i, str) for i in indict.items()]): |
| 66 | + indict[key] = list(indict[key]) |
| 67 | + return indict or {} |
| 68 | + |
| 69 | + |
| 70 | +if sys.argv[1] in ("-h", "--help", "help"): |
| 71 | + print(HELP_TEXT) |
| 72 | + sys.exit() |
| 73 | + |
| 74 | + |
| 75 | +input_file = sys.argv[1] |
| 76 | +print(f"Getting baseline from {input_file}.") |
| 77 | +with open(input_file) as fh: |
| 78 | + reader = csv.reader(fh) |
| 79 | + next(reader) # pulse for headers |
| 80 | + for row in reader: |
| 81 | + parts = [p for p in row[0].split("/") if not p.isdigit()] |
| 82 | + if not filter_parts(parts): |
| 83 | + add_path_original(parts) |
| 84 | + |
| 85 | +SKIP_DICT = simplify(SKIP_DICT) |
| 86 | + |
| 87 | +if len(sys.argv) > 2: |
| 88 | + as_name = sys.argv[2] |
| 89 | +else: |
| 90 | + as_name = "constants.yaml" |
| 91 | +with open(as_name, "w") as of: |
| 92 | + export = {"expected_constants": SKIP_DICT, "skipped_constants": []} |
| 93 | + yaml.dump(export, of) |
| 94 | + |
| 95 | +print(f"Constants file saved to {as_name}.") |
0 commit comments