-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.py
More file actions
68 lines (60 loc) · 2.6 KB
/
Copy pathprocess.py
File metadata and controls
68 lines (60 loc) · 2.6 KB
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
63
64
65
66
67
68
from od2validation import Package, ValidationError
import sys
import logging
from typing import List, Optional
# Set up logging (level is set to INFO, format tells log how to display messages)
# format uses a weird syntax because logging uses older string format style
logging.basicConfig(
level=logging.INFO,
# Display the level of the log name (like INFO or DEBUG) and then the log message after whitespace
format='%(levelname)s: %(message)s'
)
# process.py is the entry point for the script, so we set the logging level for the whole program here
logger = logging.getLogger(__name__)
def count_header_errors(errors, headers) -> dict[str, int]:
"""Return dict with error totals per header"""
d = {}
for h in headers:
d[h] = 0
for e in errors:
if e.error_header in d:
d[e.error_header] += 1
else:
print(f"PROGRAM ERROR: Header '{e.error_header}' present in errors but not in header list")
return d
def print_error_summary(processing, errors, collection_name) -> None:
"""Print each header with corresponding error total (don't print if 0)"""
# Derive error variables
error_count = len(errors)
headers_with_errors = set(e.error_header for e in errors)
validated_headers = processing.get_headers()
error_totals = count_header_errors(errors, processing.get_headers())
# Print summary
print("\n" + "="*80)
print("-- Validation complete --")
print(f"Checked {len(validated_headers)} headers")
if error_count == 0:
print("NO ERRORS FOUND")
else:
print(f"Found {error_count} error(s) in {len(headers_with_errors)}/{len(validated_headers)} headers")
for header in error_totals:
if error_totals[header] != 0:
print(f"{header}: {error_totals[header]}")
print("\nTo automatically fix common issues:")
print(f" python fixcsv.py {collection_name}")
print("\nNote: this file is not created by default, you will have to make it manually")
print("="*80)
def main():
try:
# Run checks and print errors
collection_name = sys.argv[1]
processing = Package(collection_name)
processing.print_filepaths()
processing.check_headers()
errors = processing.get_headers_instructions()
print_error_summary(processing, errors, collection_name)
except IndexError:
print("Missing config file name (do not include file extension)")
print("EXAMPLE: python process.py uo-athletics")
if __name__ == "__main__":
main()