-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
62 lines (47 loc) · 1.82 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import sys
import os
import json
from read_files import read_specs
from read_files import read_data
from validate import validate_file
# Usage statment can be printed to user in case of an error in passing arguments
usage_statement = "Usage\n\tmain.py <DataFile Name>.txt(or.csv) <SpecFile Name>.json\n"
# Check that the correct number of arguments was provided
if len(sys.argv) != 3:
print(usage_statement)
exit(0)
# Read filenames from stdin arguments
data_file = sys.argv[1]
spec_file = sys.argv[2]
# print("files:", len(sys.argv)-1)
# print(f"data file: {data_file}")
# print(f"spec file: {spec_file}\n")
# Check that input files exist and are of the correct types
if (not os.path.exists(data_file)) or (not os.path.exists(spec_file)) :
print("One of the files doesn't exist----hint: could be a typo in the file name.\n\n", usage_statement)
exit(0)
if not spec_file.endswith(".json"):
print("Spec file needs to be a json file.\n\n", usage_statement)
exit(0)
# Read the spec file
meta_data, specs = read_specs(spec_file)
delimiter = meta_data[0]
data_file_type = meta_data[1]
# print("delimiter:", delimiter, "\n")
if not data_file.endswith(f".{data_file_type}"):
print(f"Data file needs to be a {data_file_type} file.\n\n{usage_statement}")
exit(0)
# Read the data file
data = read_data(data_file, delimiter)
# Validate data
errors = validate_file(meta_data, specs, data)
print("Validation complete!\n")
if len(errors) > 0:
outformatted_content = ",\n".join(["\t" + json.dumps({k: v}) for k, v in errors])
# Create a new output file
with open("out.json", 'w') as output:
if len(errors) > 0:
output.write("[\n")
output.write(outformatted_content)
output.write("\n]")
print("output file created.")