mirror of https://github.com/backstage/charts.git
fix: Updates to jsonschema dereference script (#235)
- 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>
This commit is contained in:
parent
b0e982d951
commit
51294434ea
|
|
@ -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]
|
||||
|
|
|
|||
|
|
@ -1,34 +0,0 @@
|
|||
import json
|
||||
from typing import List, Dict, Any
|
||||
from pathlib import Path
|
||||
|
||||
import jsonref
|
||||
|
||||
JSONSCHEMA_TEMPLATE_NAME = "values.schema.tmpl.json"
|
||||
JSONSCHEMA_NAME = "values.schema.json"
|
||||
CHART_LOCK = "Chart.lock"
|
||||
|
||||
def load_template_schema(chart_dir: Path) -> Dict[str, Any]:
|
||||
"""Load values.schema.tmpl.json and template it via Jinja2."""
|
||||
with open(chart_dir / JSONSCHEMA_TEMPLATE_NAME, "r") as f:
|
||||
return json.loads(f.read())
|
||||
|
||||
def save(chart_dir: Path, schema: Any):
|
||||
"""Take schema containing $refs and dereference them."""
|
||||
with open(chart_dir / JSONSCHEMA_NAME, "w") as f:
|
||||
json.dump(schema, f, indent=4, sort_keys=True)
|
||||
|
||||
if __name__ == '__main__':
|
||||
charts = [p.parent for p in Path(".").rglob(CHART_LOCK)]
|
||||
|
||||
errors: List[BaseException] = []
|
||||
for chart in charts:
|
||||
try:
|
||||
schema_template = load_template_schema(chart)
|
||||
schema = jsonref.replace_refs(schema_template)
|
||||
save(chart, schema)
|
||||
except BaseException as e:
|
||||
print(f"Could not process schema for '{chart}': {e}")
|
||||
errors.append(e)
|
||||
if errors:
|
||||
exit(1)
|
||||
|
|
@ -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)
|
||||
Loading…
Reference in New Issue