forked from xrpscanner/xrpl-project-directory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
59 lines (48 loc) · 1.98 KB
/
parser.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
import datetime
import os
import sys
import toml
data_path = "data"
mandatory_fields = ["title", "url", "modified_date", "category"]
def parse_files(data_path):
failed_files = []
successful_files = []
for root, dirs, files in os.walk(data_path):
for file in files:
if(file.endswith(".toml")):
path = os.path.join(root, file)
print("parsing file", path)
try:
data = toml.load(path)
except toml.decoder.TomlDecodeError as e:
print(f"ERROR: {path} failed to decode: {e}")
failed_files.append(path)
continue
# Check mandatory fields
missing_fields = []
for field in mandatory_fields:
if field not in data:
missing_fields.append(field)
if missing_fields:
print(f"ERROR: {path} is missing fields {', '.join(missing_fields)}")
failed_files.append(path)
# skip rest of processing for this file
continue
# Check that the modified_date is a valid date
try:
last_modified = datetime.datetime.fromisoformat(data['modified_date'])
except ValueError:
print(f"ERROR: {path} invalid modified_date field")
failed_files.append(path)
continue
# If we reach here our file has been validated successfully
successful_files.append(path)
return successful_files, failed_files
if __name__ == "__main__":
successful_files, failed_files = parse_files(data_path)
print()
print(f"Sucessfully parsed {len(successful_files)} files.")
print(f"Failed files: {len(failed_files)}.")
# Return non-zero error code to signal to other processes
if failed_files:
sys.exit(1)