forked from fsi-tue/eei
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_yml_integrity.py
More file actions
205 lines (175 loc) · 5.89 KB
/
check_yml_integrity.py
File metadata and controls
205 lines (175 loc) · 5.89 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
"""
This script checks the ingegrity of the structure and (partially) the data of the eventy.yml YAML file and the translations keys (de.json / en.json).
@author: Jules Kreuer
@contact: contact@juleskreuer.eu
"""
from datetime import datetime
from schema import Schema, And, Use, Optional, SchemaError
import yaml
import os
import json
FILE_NAME = "events.yml"
LANG_FILES = ["i18n/de.json", "i18n/en.json"]
UNIQUE_VALUES = {}
DATE_FORMAT = "%d.%m.%Y %H:%M"
def is_date(s: str) -> bool:
"""
Must follow %d.%m.%Y %H:%M format
Eg: 07.03.2024 01:02
"""
try:
datetime.strptime(s, DATE_FORMAT)
except ValueError:
print(f"The date {s} does not follow the format {DATE_FORMAT}")
return False
return True
def is_unique(key, data) -> bool:
"""
Check if `data` for a given `key` is unique across all events.
"""
if key not in UNIQUE_VALUES:
UNIQUE_VALUES[key] = [data]
return True
if data not in UNIQUE_VALUES[key]:
UNIQUE_VALUES[key].append(data)
return True
return False
def is_icon(s: str) -> bool:
valid_icon_names = [
"beer",
"cap",
"clock",
"cocktail",
"dice",
"film",
"food",
"grill",
"hiking",
"home",
"marker",
"route",
"signs",
"snowflake",
"information",
"party",
"google-maps",
"openstreetmap"
]
return s in valid_icon_names
def is_valid_location_maps(location_maps: dict) -> bool:
"""
Validate location_maps dictionary.
Only allow 'google' and 'osm' as keys.
"""
valid_providers = ['google', 'osm']
for key in location_maps.keys():
if key not in valid_providers:
print(f"Invalid location_maps provider: {key}. Only 'google' and 'osm' are allowed.")
return False
return True
# Schema of a single event
# To check the value for:
# a string use: str
# a non-empty string use: And(str, len),
# a date use: And(str, is_date),
# a unique field use: lambda s: is_unique("KEY NAME", s)
# a list of str use: [str]
#
# To check for optional keys use: Optionale("KEY NAME"): VALUE
REQUIRED_SCHEMA = Schema(And(
{
Optional("name"): And(str, len),
Optional("registration_enabled"): bool,
Optional("cancelled"): bool,
Optional("text"): str,
Optional("info"): str,
"location": str,
Optional("location_maps"): And(dict, is_valid_location_maps),
Optional("opentoall"): bool,
Optional("max_participants"): int,
Optional("dinos"): bool,
"event_date": {
"start": And(str, is_date),
Optional("end"): And(str, is_date),
"onTime": bool,
},
Optional("registration_date"): {
Optional("start"): And(str, is_date),
"end": And(str, is_date),
},
Optional("csv_path"): And(str, len, lambda s: is_unique("csv_path", s)),
Optional("form"): {
Optional("breakfast"): bool,
Optional("food"): bool,
Optional("gender"): bool,
Optional("course_required"): bool,
Optional("no_alcohol"): bool,
},
"icon": And(str, is_icon),
Optional("metas"): [str],
},
lambda e: (not e.get("registration_enabled", True)) or ("csv_path" in e),
lambda e: (not e.get("registration_enabled", True)) or ("max_participants" in e),
lambda e: (not e.get("registration_enabled", True)) or ("registration_date" in e),
))
class UniqueKeyLoader(yaml.SafeLoader):
"""
Load the YAML file, raises an error if a duplicate key is found.
Adapted from https://gist.github.com/pypt/94d747fe5180851196eb
"""
def construct_mapping(self, node, deep=False):
mapping = set()
for key_node, value_node in node.value:
if ":merge" in key_node.tag:
continue
key = self.construct_object(key_node, deep=deep)
if key in mapping:
raise ValueError(f"Duplicate {key!r} key found in YAML.")
mapping.add(key)
return super().construct_mapping(node, deep)
def check_language_files() -> int:
"""
Check the integrity of the language JSON files.
Ensures:
- Files exist.
- Files are valid JSON.
- Keys in both files match.
"""
errors = 0
lang_data = {}
# Check if files exist and are valid JSON
for lang_file in LANG_FILES:
if not os.path.exists(lang_file):
print(f"Error: Language file {lang_file} does not exist.")
errors += 1
continue
try:
with open(lang_file, "r", encoding="utf-8") as f:
lang_data[lang_file] = json.load(f)
except json.JSONDecodeError as e:
print(f"Error: Language file {lang_file} is not valid JSON. {e}")
errors += 1
# Compare keys between language files
if len(lang_data) == len(LANG_FILES):
keys = [set(data.keys()) for data in lang_data.values()]
if keys[0] != keys[1]:
print("Error: Keys in language files do not match.")
print(f"Keys in {LANG_FILES[0]} but not in {LANG_FILES[1]}: {keys[0] - keys[1]}")
print(f"Keys in {LANG_FILES[1]} but not in {LANG_FILES[0]}: {keys[1] - keys[0]}")
errors += 1
return errors
with open(FILE_NAME, "r") as f:
yaml_data = yaml.load(f, Loader=UniqueKeyLoader)
max_key_len = max(len(k) for k in yaml_data["events"].keys()) + 1
num_errors = 0
for key, event in yaml_data["events"].items():
try:
REQUIRED_SCHEMA.validate(event)
print(f" {key.ljust(max_key_len)} valid.")
except SchemaError as e:
num_errors += 1
e = str(e).replace("\n", " ")
print(f" {key.ljust(max_key_len)} Invalid schema. {e}")
# Add the language file check to the main script
num_errors += check_language_files()
exit(num_errors)