-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportaldata.py
217 lines (145 loc) · 6.48 KB
/
portaldata.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
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
206
207
208
209
210
211
212
213
214
215
216
217
from interface import (
ProgrammingLanguage,
Licenses,
Organization,
ToolCategory,
SoftwareTool,
)
from pathlib import Path, PurePosixPath
from pydantic import ValidationError
from util import read_file, write_file
def normalize(x: str):
return x.lower().replace(" ", "")
class PortalData():
def __init__(self, datapath: str):
datapath = Path(datapath)
self.errors = []
self.load_licenses(datapath)
self.load_organizations(datapath)
self.load_categories(datapath)
self.load_languages(datapath)
self.load_tools(datapath)
def load_licenses(self, datapath: Path):
self.licenses = {}
for file in (datapath / "licenses").iterdir():
try:
licen = Licenses.model_validate(read_file(file))
except ValidationError as err:
self.errors.append(("licenses", file.name, str(err)))
continue
licen.id = file.name.replace(file.suffix, "")
licen.path = PurePosixPath("licenses") / licen.id
licen.tools = []
self.licenses[licen.id] = licen
def load_languages(self, datapath: Path):
self.languages = {}
for file in (datapath / "languages").iterdir():
try:
lang = ProgrammingLanguage.model_validate(read_file(file))
except ValidationError as err:
self.errors.append(("languages", file.name, str(err)))
continue
lang.id = file.name.replace(file.suffix, "")
lang.path = PurePosixPath("programming-languages") / lang.id
lang.tools = []
for i, licen_id in enumerate(lang.licenses):
licen_id = normalize(licen_id)
if licen_id not in self.licenses.keys():
self.errors.append(("languages", file.name,
f"'{licen_id}' is not a valid licence id"))
continue
lang.licenses[i] = self.licenses[licen_id]
self.languages[lang.id] = lang
def load_organizations(self, datapath: Path):
self.organizations = {}
for file in (datapath / "organizations").iterdir():
try:
org = Organization.model_validate(read_file(file))
except ValidationError as err:
self.errors.append(("organizations", file.name, str(err)))
continue
org.id = file.name.replace(file.suffix, "")
org.path = PurePosixPath("organizations") / org.id
org.tools = []
self.organizations[org.id] = org
def load_categories(self, datapath: Path):
self.categories = {}
for file in (datapath / "categories").iterdir():
try:
cat = ToolCategory.model_validate(read_file(file))
except ValidationError as err:
self.errors.append(("categories", file.name, str(err)))
continue
cat.id = file.name.replace(file.suffix, "")
cat.path = PurePosixPath("categories") / cat.id
cat.children = []
cat.tools = []
self.categories[cat.id] = cat
for cat in self.categories.values():
if cat.parent:
parent_id = normalize(cat.parent)
if parent_id not in self.categories.keys():
self.errors.append(("categories", cat.id,
f"'{parent_id}' is not a valid parent category id"))
continue
cat.parent = self.categories[parent_id]
cat.parent.children.append(cat)
def load_tools(self, datapath: Path):
self.tools = {}
for file in (datapath /"software").iterdir():
try:
tool = SoftwareTool.model_validate(read_file(file))
except ValidationError as err:
self.errors.append(("software", file.name, str(err)))
continue
tool.id = file.name.replace(file.suffix, "")
tool.path = PurePosixPath("tools") / tool.id
for i, lang_id in enumerate(tool.languages):
lang_id = normalize(lang_id)
if lang_id not in self.languages.keys():
self.errors.append(("software", file.name,
f"'{lang_id}' is not a valid programming language id"))
continue
lang = self.languages[lang_id]
tool.languages[i] = lang
lang.tools.append(tool)
for i, org_id in enumerate(tool.organizations):
org_id = normalize(org_id)
if org_id not in self.organizations.keys():
self.errors.append(("software", file.name,
f"'{org_id}' is not a valid organization id"))
continue
org = self.organizations[org_id]
tool.organizations[i] = org
org.tools.append(tool)
for i, licen_id in enumerate(tool.licenses):
licen_id = normalize(licen_id)
if licen_id not in self.licenses.keys():
self.errors.append(("software", file.name,
f"'{licen_id}' is not a valid license id"))
continue
licen = self.licenses[licen_id]
tool.licenses[i] = licen
licen.tools.append(tool)
for i, cat_id in enumerate(tool.categories):
cat_id = normalize(cat_id)
if cat_id not in self.categories.keys():
self.errors.append(("software", file.name,
f"'{cat_id}' is not a valid category id"))
continue
cat = self.categories[cat_id]
tool.categories[i] = cat
cat.tools.append(tool)
self.tools[tool.id] = tool
def has_errors(self):
return len(self.errors) > 0
def write_errormessage(self, errorpath: Path):
with open(errorpath, 'w') as f:
f.write("Thank you for your submission! " +
"Unfortunately, we could not successfully parse the dataset " +
"after applying your changes. Here are the errors we encountered:" +
"\n\n```\n")
for error in self.errors:
f.write(str(error) + "\n")
f.write("```\n\n" +
"If you can, try fixing these and update your submission.\n")