-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvalidate.py
More file actions
419 lines (371 loc) · 15.2 KB
/
Copy pathvalidate.py
File metadata and controls
419 lines (371 loc) · 15.2 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
"""Validate seed JSON against the schema and conventions (§9.3, §15.3).
Checks: required fields, slug convention (§14.1), value ranges/units (§14.3),
and foreign-key integrity by slug. Run with ``python -m scripts.validate``;
exits non-zero on the first failure set (used by CI ``validate-data.yml``).
"""
from __future__ import annotations
import json
import re
import sys
from pathlib import Path
from typing import Any
DATA_DIR = Path(__file__).resolve().parent.parent / "data"
SLUG_RE = re.compile(r"^[a-z0-9]+(?:-[a-z0-9]+)*$")
BRAND_REQUIRED = {"slug", "name", "country", "categories", "source_urls"}
BRAND_CATEGORIES = {
"smartphone-oem",
"soc-designer",
"cpu-designer",
"gpu-designer",
"ip-licensor",
"aib-partner",
"pc-oem",
"chipset-maker",
"sub-brand",
"defunct",
}
COUNTRY_RE = re.compile(r"^[A-Z]{2}$")
SOC_REQUIRED = {"slug", "name", "manufacturer", "release_date", "process_nm", "gpu_name"}
PHONE_REQUIRED = {
"slug",
"name",
"brand",
"soc",
"release_date",
"ram_gb",
"battery_mah",
"weight_g",
"os",
}
MOBILE_DEVICE_REQUIRED = {
"slug",
"name",
"brand",
"release_date",
"ram_gb",
"battery_mah",
"weight_g",
"os",
"source_urls",
"verified",
}
GPU_REQUIRED = {
"slug",
"name",
"manufacturer",
"architecture",
"release_date",
"memory_gb",
"memory_type",
"memory_bus_bit",
"base_clock_mhz",
"boost_clock_mhz",
"tdp_w",
"pcie_version",
}
CPU_REQUIRED = {
"slug",
"name",
"manufacturer",
"release_date",
"segment",
"architecture",
"cores",
"threads",
}
LAPTOP_REQUIRED = {
"slug",
"name",
"brand",
"release_date",
"ram_gb",
"os",
"source_urls",
"verified",
}
MONITOR_REQUIRED = {
"slug",
"name",
"brand",
"release_date",
"size_inch",
"resolution",
"source_urls",
"verified",
}
DATE_RE = re.compile(r"^\d{4}-\d{2}-\d{2}$")
def _load(subdir: str) -> list[tuple[str, dict[str, Any]]]:
path = DATA_DIR / subdir
if not path.exists():
return []
return [
(str(f.relative_to(DATA_DIR)), json.loads(f.read_text(encoding="utf-8-sig")))
for f in sorted(path.rglob("*.json")) # recurse into brand subfolders
]
def _check_required(
name: str, record: dict[str, Any], required: set[str], errors: list[str]
) -> None:
missing = required - record.keys()
if missing:
errors.append(f"{name}: missing required fields {sorted(missing)}")
def _check_slug(name: str, slug: object, errors: list[str]) -> None:
if not isinstance(slug, str) or not SLUG_RE.match(slug):
errors.append(f"{name}: invalid slug '{slug}' (must be kebab-case, §14.1)")
def _check_range(
name: str, field: str, value: object, lo: float, hi: float, errors: list[str]
) -> None:
if value is None:
return
if not isinstance(value, (int, float)) or not (lo <= value <= hi):
errors.append(f"{name}: {field}={value} out of range [{lo}, {hi}]")
def _check_date(name: str, value: object, errors: list[str]) -> None:
if not isinstance(value, str) or not DATE_RE.match(value):
errors.append(f"{name}: release_date '{value}' must be ISO 8601 YYYY-MM-DD (§14.2)")
def _check_unique_slugs(
category: str, records: list[tuple[str, dict[str, Any]]], errors: list[str]
) -> None:
"""Each category's `slug` must be unique — seed/dump load into a UNIQUE column."""
seen: dict[str, str] = {}
for fname, rec in records:
slug = rec.get("slug")
if not isinstance(slug, str):
continue
if slug in seen:
errors.append(
f"{fname}: duplicate {category} slug '{slug}' (also in {seen[slug]})"
)
else:
seen[slug] = fname
def _check_source_urls(name: str, record: dict[str, Any], errors: list[str]) -> None:
urls = record.get("source_urls")
if not isinstance(urls, list) or not urls or not all(
isinstance(url, str) and url.startswith(("http://", "https://")) for url in urls
):
errors.append(f"{name}: source_urls must be a non-empty list of http(s) URL strings")
def _check_storage_options_gb(name: str, record: dict[str, Any], errors: list[str]) -> None:
values = record.get("storage_options_gb")
if values is None:
return
if not isinstance(values, list):
errors.append(f"{name}: storage_options_gb must be a list of integer GB values")
return
bad = [value for value in values if not isinstance(value, int) or value < 1]
if bad:
errors.append(f"{name}: storage_options_gb contains invalid integer GB values {bad}")
def _check_variant_path(
fname: str,
rec: dict[str, Any],
category: str,
errors: list[str],
*,
allow_flat: bool = False,
) -> None:
parts = Path(fname).parts
if allow_flat and len(parts) == 4:
return
if len(parts) != 5:
errors.append(
f"{fname}: {category} variants must live at "
f"'{category}/<brand>/<year>/<base_model_slug>/<slug>.json'"
)
return
_, brand, year, base_model_slug, filename = parts
if rec.get("brand") != brand:
errors.append(f"{fname}: lives in brand '{brand}' but brand='{rec.get('brand')}'")
release_year = str(rec.get("release_date", ""))[:4]
if release_year and year != release_year:
errors.append(
f"{fname}: lives in year '{year}' but release_date starts with '{release_year}'"
)
if rec.get("base_model_slug") and rec.get("base_model_slug") != base_model_slug:
errors.append(
f"{fname}: lives under base '{base_model_slug}' but "
f"base_model_slug='{rec.get('base_model_slug')}'"
)
if filename != f"{rec.get('slug')}.json":
errors.append(f"{fname}: filename must match slug '{rec.get('slug')}'")
def validate() -> list[str]:
errors: list[str] = []
brands = _load("brand")
socs = _load("soc")
phones = _load("smartphone")
tablets = _load("tablet")
watches = _load("watch")
pdas = _load("pda")
gpus = _load("gpu")
cpus = _load("cpu")
laptops = _load("laptop")
monitors = _load("monitor")
brand_slugs = {rec["slug"] for _, rec in brands if "slug" in rec}
soc_slugs = {rec["slug"] for _, rec in socs if "slug" in rec}
cpu_slugs = {rec["slug"] for _, rec in cpus if "slug" in rec}
gpu_slugs = {rec["slug"] for _, rec in gpus if "slug" in rec}
for category, records in (
("brand", brands),
("soc", socs),
("smartphone", phones),
("tablet", tablets),
("watch", watches),
("pda", pdas),
("gpu", gpus),
("cpu", cpus),
("laptop", laptops),
("monitor", monitors),
):
_check_unique_slugs(category, records, errors)
for fname, rec in brands:
_check_required(fname, rec, BRAND_REQUIRED, errors)
_check_source_urls(fname, rec, errors)
_check_slug(fname, rec.get("slug"), errors)
if "founded_year" in rec:
_check_range(fname, "founded_year", rec["founded_year"], 1800, 2100, errors)
country = rec.get("country")
if country is not None and not (isinstance(country, str) and COUNTRY_RE.match(country)):
errors.append(f"{fname}: country '{country}' must be ISO 3166 alpha-2 (e.g. 'KR')")
cats = rec.get("categories")
if not isinstance(cats, list) or not cats:
errors.append(f"{fname}: categories must be a non-empty list")
else:
bad = [c for c in cats if c not in BRAND_CATEGORIES]
if bad:
errors.append(
f"{fname}: invalid categories {bad}; allowed = {sorted(BRAND_CATEGORIES)}"
)
if len(set(cats)) != len(cats):
errors.append(f"{fname}: categories contains duplicates")
# Path convention: brand/<country_lower>/<slug>.json
parts = Path(fname).parts
if len(parts) != 3:
errors.append(
f"{fname}: must live at 'brand/<country_lower>/<slug>.json' "
f"(got {len(parts) - 1} subpath components)"
)
elif isinstance(country, str) and parts[1] != country.lower():
errors.append(
f"{fname}: lives in '{parts[1]}/' but country='{country}' "
f"(expected '{country.lower()}/')"
)
for fname, rec in socs:
_check_required(fname, rec, SOC_REQUIRED, errors)
_check_source_urls(fname, rec, errors)
_check_slug(fname, rec.get("slug"), errors)
if "release_date" in rec:
_check_date(fname, rec["release_date"], errors)
_check_range(fname, "process_nm", rec.get("process_nm"), 1.0, 100.0, errors)
if rec.get("manufacturer") not in brand_slugs:
errors.append(f"{fname}: manufacturer '{rec.get('manufacturer')}' not a known brand")
for fname, rec in phones:
_check_required(fname, rec, PHONE_REQUIRED, errors)
_check_source_urls(fname, rec, errors)
_check_slug(fname, rec.get("slug"), errors)
if "release_date" in rec:
_check_date(fname, rec["release_date"], errors)
_check_range(fname, "ram_gb", rec.get("ram_gb"), 1, 64, errors)
_check_range(fname, "battery_mah", rec.get("battery_mah"), 500, 12000, errors)
_check_range(fname, "weight_g", rec.get("weight_g"), 50, 500, errors)
if "msrp_usd" in rec:
_check_range(fname, "msrp_usd", rec["msrp_usd"], 50, 5000, errors)
if rec.get("brand") not in brand_slugs:
errors.append(f"{fname}: brand '{rec.get('brand')}' not a known brand")
if rec.get("soc") not in soc_slugs:
errors.append(f"{fname}: soc '{rec.get('soc')}' not a known SoC")
_check_variant_path(fname, rec, "smartphone", errors, allow_flat=True)
for category, records in (("tablet", tablets), ("watch", watches), ("pda", pdas)):
for fname, rec in records:
_check_required(fname, rec, MOBILE_DEVICE_REQUIRED, errors)
_check_source_urls(fname, rec, errors)
_check_slug(fname, rec.get("slug"), errors)
if "release_date" in rec:
_check_date(fname, rec["release_date"], errors)
_check_range(fname, "ram_gb", rec.get("ram_gb"), 0.016, 64, errors)
_check_range(fname, "battery_mah", rec.get("battery_mah"), 50, 20000, errors)
_check_range(fname, "weight_g", rec.get("weight_g"), 10, 2000, errors)
if "msrp_usd" in rec:
_check_range(fname, "msrp_usd", rec["msrp_usd"], 10, 10000, errors)
if rec.get("brand") not in brand_slugs:
errors.append(f"{fname}: brand '{rec.get('brand')}' not a known brand")
if rec.get("soc") is not None and rec.get("soc") not in soc_slugs:
errors.append(f"{fname}: soc '{rec.get('soc')}' not a known SoC")
_check_storage_options_gb(fname, rec, errors)
_check_variant_path(fname, rec, category, errors)
for fname, rec in gpus:
_check_required(fname, rec, GPU_REQUIRED, errors)
_check_source_urls(fname, rec, errors)
_check_slug(fname, rec.get("slug"), errors)
if "release_date" in rec:
_check_date(fname, rec["release_date"], errors)
_check_range(fname, "memory_gb", rec.get("memory_gb"), 0.001, 512, errors)
_check_range(fname, "tdp_w", rec.get("tdp_w"), 1, 3000, errors)
if "msrp_usd" in rec:
_check_range(fname, "msrp_usd", rec["msrp_usd"], 50, 100000, errors)
if rec.get("manufacturer") not in brand_slugs:
errors.append(f"{fname}: manufacturer '{rec.get('manufacturer')}' not a known brand")
valid_segments = {"desktop", "laptop", "hedt", "server"}
for fname, rec in cpus:
_check_required(fname, rec, CPU_REQUIRED, errors)
_check_source_urls(fname, rec, errors)
_check_slug(fname, rec.get("slug"), errors)
if "release_date" in rec:
_check_date(fname, rec["release_date"], errors)
_check_range(fname, "cores", rec.get("cores"), 1, 512, errors)
_check_range(fname, "threads", rec.get("threads"), 1, 1024, errors)
if "msrp_usd" in rec:
_check_range(fname, "msrp_usd", rec["msrp_usd"], 20, 50000, errors)
if rec.get("segment") not in valid_segments:
seg = rec.get("segment")
errors.append(f"{fname}: segment '{seg}' not in {sorted(valid_segments)}")
if rec.get("manufacturer") not in brand_slugs:
errors.append(f"{fname}: manufacturer '{rec.get('manufacturer')}' not a known brand")
for fname, rec in laptops:
_check_required(fname, rec, LAPTOP_REQUIRED, errors)
_check_source_urls(fname, rec, errors)
_check_slug(fname, rec.get("slug"), errors)
if "release_date" in rec:
_check_date(fname, rec["release_date"], errors)
_check_range(fname, "ram_gb", rec.get("ram_gb"), 1, 256, errors)
if rec.get("storage_gb") is not None:
_check_range(fname, "storage_gb", rec.get("storage_gb"), 1, 65536, errors)
if rec.get("weight_g") is not None:
_check_range(fname, "weight_g", rec.get("weight_g"), 300, 6000, errors)
if "msrp_usd" in rec:
_check_range(fname, "msrp_usd", rec["msrp_usd"], 50, 50000, errors)
if rec.get("brand") not in brand_slugs:
errors.append(f"{fname}: brand '{rec.get('brand')}' not a known brand")
if rec.get("cpu") is not None and rec.get("cpu") not in cpu_slugs:
errors.append(f"{fname}: cpu '{rec.get('cpu')}' not a known CPU")
if rec.get("gpu") is not None and rec.get("gpu") not in gpu_slugs:
errors.append(f"{fname}: gpu '{rec.get('gpu')}' not a known GPU")
_check_variant_path(fname, rec, "laptop", errors, allow_flat=True)
for fname, rec in monitors:
_check_required(fname, rec, MONITOR_REQUIRED, errors)
_check_source_urls(fname, rec, errors)
_check_slug(fname, rec.get("slug"), errors)
if "release_date" in rec:
_check_date(fname, rec["release_date"], errors)
_check_range(fname, "size_inch", rec.get("size_inch"), 5, 120, errors)
_check_range(fname, "refresh_hz", rec.get("refresh_hz"), 24, 1000, errors)
if rec.get("ppi") is not None:
_check_range(fname, "ppi", rec.get("ppi"), 20, 1000, errors)
if rec.get("rating") is not None:
_check_range(fname, "rating", rec.get("rating"), 0, 5, errors)
if "msrp_usd" in rec:
_check_range(fname, "msrp_usd", rec["msrp_usd"], 10, 50000, errors)
if rec.get("brand") not in brand_slugs:
errors.append(f"{fname}: brand '{rec.get('brand')}' not a known brand")
_check_variant_path(fname, rec, "monitor", errors, allow_flat=True)
return errors
def run() -> int:
try:
sys.stdout.reconfigure(encoding="utf-8") # type: ignore[union-attr]
except Exception:
pass
errors = validate()
if errors:
print(f"❌ Data validation failed ({len(errors)} issue(s)):")
for err in errors:
print(f" - {err}")
return 1
print("✅ Data validation passed")
return 0
if __name__ == "__main__":
sys.exit(run())