Skip to content

Commit

Permalink
fix: Updates to jsonschema dereference script (#235)
Browse files Browse the repository at this point in the history
- Fix linting errors when opening files without selecting an encoding
 - Search for values.schema.tmpl.json instead of Chart.lock files
 - Rename 'schema' variables to prevent shadowing or redefining
 - Add Module docstring
 - Add comments explaining what is being done
 - Rename file to follow Python module naming convention of snake_case
 - Rename some variables to correctly represent what they are doing, schema_template instead of chart, etc.
 - Update pre-commit to point to new jsonschema_dereference script name

Signed-off-by: Corey Daley <cdaley@redhat.com>
  • Loading branch information
coreydaley authored Nov 25, 2024
1 parent b0e982d commit 5129443
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repos:
hooks:
- id: jsonschema-dereference
name: jsonschema-dereference
entry: python .pre-commit/jsonschema-dereference.py
entry: python .pre-commit/jsonschema_dereference.py
additional_dependencies: [jsonref]
language: python
types_or: [yaml, json]
34 changes: 0 additions & 34 deletions .pre-commit/jsonschema-dereference.py

This file was deleted.

54 changes: 54 additions & 0 deletions .pre-commit/jsonschema_dereference.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
This Python module:
- Searches for JSON Schema templates with the name values.schema.tmpl.json
- Dereferences any $refs contained in those files
- Outputs the new Schema to a values.schema.json file in the same directory
"""

import sys
import json
from typing import List, Dict, Any
from pathlib import Path

# External library dependency
# Install with 'pip install jsonref'
import jsonref

# File to write the dereferenced JSON Schema to
JSONSCHEMA_NAME = "values.schema.json"
# File that contains the JSON Schema that needs dereferencing
JSONSCHEMA_TEMPLATE_NAME = "values.schema.tmpl.json"

def load_template_schema(schema_dir: Path) -> Dict[str, Any]:
"""Load the schema template values.schema.tmpl.json"""
with open(schema_dir / JSONSCHEMA_TEMPLATE_NAME, "r", encoding="utf-8") as f:
return json.loads(f.read())

def save(schema_dir: Path, schema_data: Any):
"""Save the dereferenced schema to values.schema.json"""
with open(schema_dir / JSONSCHEMA_NAME, "w", encoding="utf-8") as f:
json.dump(schema_data, f, indent=4, sort_keys=True)

if __name__ == '__main__':
# Search for all values.schema.tmpl.json files
schema_templates = [p.parent for p in Path(".").rglob(JSONSCHEMA_TEMPLATE_NAME)]

# Create a list to hold any exceptions
errors: List[BaseException] = []
# Iterate over the List of found schema templates
for schema_template in schema_templates:
try:
# Load the schema into a variable as JSON
st = load_template_schema(schema_template)
# Dereference all of the $refs
s = jsonref.replace_refs(st)
# Save the dereferenced JSON
save(schema_template, s)
except BaseException as e:
# Print any errors to the screen
print(f"Could not process schema for '{schema_template}': {e}")
# Append any exceptions to the errors List
errors.append(e)
if errors:
# Exit with status 1 if any exceptions were thrown
sys.exit(1)

0 comments on commit 5129443

Please # to comment.