Skip to content

Commit

Permalink
Keep line endings in Sort, and fix schema in Validate (#80)
Browse files Browse the repository at this point in the history
* Keep file line endings in sort.py
* Auto-download Schema if missing
  • Loading branch information
Soitora authored Mar 9, 2024
1 parent 9cc8f90 commit 727722f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# PlexAniSync files
custom_mappings_schema.json
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
jsonschema==4.6.1
ruamel.yaml==0.17.21
requests==2.31.0
2 changes: 1 addition & 1 deletion sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
yaml.width = 1024

for file in glob.glob("*.yaml"):
with open(file, 'r+', encoding='utf-8') as f:
with open(file, 'r+b') as f:
file_mappings = yaml.load(f)
file_mappings["entries"].sort(key=lambda entry: entry["title"].lower())
# Jump to beginning of file and overwrite with sorted entries
Expand Down
22 changes: 20 additions & 2 deletions validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,31 @@
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'

with open('./custom_mappings_schema.json', 'r', encoding='utf-8') as f:
schema = json.load(f)
# 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
Expand Down

0 comments on commit 727722f

Please sign in to comment.