-
Notifications
You must be signed in to change notification settings - Fork 40
/
validate.py
55 lines (49 loc) · 1.84 KB
/
validate.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
import glob
import json
import logging
import sys
from typing import List
from jsonschema import validate
from jsonschema.exceptions import ValidationError
from ruamel.yaml import YAML
import requests
logger = logging.getLogger("PlexAniSync")
yaml = YAML(typ='safe')
SUCCESS = True
schema_url = "https://raw.githubusercontent.com/RickDB/PlexAniSync/master/custom_mappings_schema.json"
local_schema_path = './custom_mappings_schema.json'
# Try to load the schema from the local file
try:
with open(local_schema_path, 'r', encoding='utf-8') as f:
schema = json.load(f)
except FileNotFoundError:
# If the local file doesn't exist, fetch it from the remote URL
try:
response = requests.get(schema_url)
response.raise_for_status()
schema = response.json()
# Save the fetched schema locally
with open(local_schema_path, 'w', encoding='utf-8') as f:
json.dump(schema, f, indent=4)
except requests.RequestException as e:
logger.error(f"Failed to fetch schema from {schema_url}: {e}")
sys.exit(1)
for file in glob.glob("*.yaml"):
# Create a Data object
with open(file, 'r', encoding='utf-8') as f:
file_mappings = yaml.load(f)
try:
# Validate data against the schema.
validate(file_mappings, schema)
titles: List[str] = []
for entry in file_mappings["entries"]:
title: str = entry["title"]
if title.lower() in titles:
raise ValidationError(f"{title} is already mapped", instance=entry)
titles.append(title.lower())
except ValidationError as e:
logger.error(f"Custom Mappings validation failed for {file}!")
logger.error(f"{e.message} at entry {e.instance}")
SUCCESS = False
if not SUCCESS:
sys.exit(1)