-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathreindex.py
78 lines (64 loc) · 2.56 KB
/
reindex.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import yaml
import os
import logging
import sys
import json
from validation import validate_fittings
LEVELS = {'debug': logging.DEBUG,
'info': logging.INFO,
'warning': logging.WARNING,
'error': logging.ERROR,
'critical': logging.CRITICAL}
if len(sys.argv) > 1:
level_name = sys.argv[1]
level = LEVELS.get(level_name, logging.NOTSET)
logging.basicConfig(level=level)
complete_index = {}
def read_fitting(file_path):
with open(file_path) as fitting_f:
plan = fitting_f.read()
documents = plan.split('\n---')
index = 0
for document in documents:
index += 1
if '\n' in document and index == 1:
settings = yaml.load(document)
logging.info("Validated fittings %s", file_path)
return settings
def get_fittings(directory, name, description):
fittings = []
# Check and load directory.
for subdir, dirs, files in os.walk("fittings/"+directory):
logging.info("Sub-directory %s", subdir)
for dir in dirs:
logging.info("Found directory %s", dir)
for file in files:
if file == "fittings.yaml":
# check the yaml
file_path = os.path.join(subdir, file)
fitting = read_fitting(file_path)
fitting_info = fitting.get('information', '')
validate_fittings(file_path)
fittings.append({"name": os.path.basename(subdir),
"directory": subdir.replace('\\','/'),
"icon": os.path.join(subdir, "icon.png").replace('\\','/'),
"information": fitting_info})
return fittings
with open("fittings/categories.yaml", "r") as categories_f:
docs = yaml.load(categories_f)
complete_index['Categories'] = [{
'name': category['name'],
'description': category['description'],
'fittings': [
fitting for fitting in get_fittings(category['directory'],
category['name'],
category['description'])
]
} for category in docs['categories']]
for category in docs['categories']:
get_fittings(
category['directory'],
category['name'],
category['description'])
with open("index.json", "w") as index_f:
index_f.write(json.dumps(complete_index))