forked from FoldingCommunity/Translate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate_yaml.py
executable file
·59 lines (44 loc) · 1.46 KB
/
validate_yaml.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
#!/usr/bin/env python3
import fnmatch
import yaml
import traceback
import os
import sys
def find(pattern, path):
result = []
for root, dirs, files in os.walk(path):
for name in files:
if fnmatch.fnmatch(name, pattern):
result.append(os.path.join(root, name))
return result
# eg: Localization/en-US/statistics.yaml
def path_to_language(file):
(head, tail) = os.path.split(file)
(head, tail) = os.path.split(head)
return tail
def filename_is(path, filename):
(head, tail) = os.path.split(path)
return tail == filename
def ensure_schema(translation, path):
keys = ["url", "language", "text", "title"]
for key in keys:
if key not in translation:
raise Exception("Missing '%s' key in %s" % (key, path))
def ensure_language(translation, path):
expected_language = path_to_language(path)
if translation["language"] != expected_language:
raise Exception(
"%s was expected to be in language %s, but instead was %s" %
(path, expected_language, translation["language"]))
exit_code = 0
for path in find("*.yaml", "Localization"):
with open(path) as f:
print("Validating", path)
try:
translation = yaml.safe_load(f)
ensure_schema(translation, path)
ensure_language(translation, path)
except Exception as e:
exit_code = 1
traceback.print_exc(file=sys.stdout)
exit(exit_code)