Skip to content

Commit 2633855

Browse files
authored
Merge pull request #22 from JacobCallahan/scripts
Add initial helper scripts
2 parents d39ea9f + 84dfbc8 commit 2633855

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

scripts/clean_csv.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import sys
2+
3+
HELP_TEXT = """
4+
This script cleans a CSV file by removing lines that contain consecutive commas (,,,)
5+
and formats the output by aligning the first two columns of each line.
6+
7+
Usage:
8+
python clean_csv.py <input_file> [output_file]
9+
10+
Arguments:
11+
<input_file> - The path to the CSV file to be cleaned.
12+
[output_file] - Optional. The path where the cleaned CSV file will be saved.
13+
If not provided, the output will be saved as 'cleaned.report'.
14+
Note: The output report will not be a valid csv file.
15+
16+
Example:
17+
python clean_csv.py data.csv cleaned_data.report
18+
"""
19+
20+
21+
def strip_extra(line):
22+
split_line = line.split(",")
23+
if len(split_line) == 1:
24+
return line
25+
return f"{split_line[0]:<50} | {split_line[1]}\n"
26+
27+
28+
def clean_empty(file_handler):
29+
out_lines = []
30+
for line in file_handler:
31+
if ",,," not in line:
32+
out_lines.append(strip_extra(line))
33+
return out_lines
34+
35+
36+
if sys.argv[1] in ("-h", "--help", "help"):
37+
print(HELP_TEXT)
38+
sys.exit()
39+
40+
to_clean = sys.argv[1]
41+
with open(to_clean) as fh:
42+
cleaned = clean_empty(fh)
43+
44+
if len(sys.argv) > 2:
45+
as_name = sys.argv[2]
46+
else:
47+
as_name = "cleaned.report"
48+
with open(as_name, "w") as fh:
49+
fh.writelines(cleaned)
50+
51+
print(f"Cleaned file saved to {as_name}")

scripts/gen_constants.py

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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

Comments
 (0)