Skip to content

Commit

Permalink
Add schema validation like in odr: Apply default values
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanatosGit committed Aug 20, 2023
1 parent 37b1dd9 commit ae36109
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 4 additions & 1 deletion open_samus_returns_rando/samus_returns_patcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from open_samus_returns_rando.patcher_editor import PatcherEditor, path_for_level
from open_samus_returns_rando import lua_util
from open_samus_returns_rando.model_data import get_data
from open_samus_returns_rando.validator_with_default import DefaultValidatingDraft7Validator


T = typing.TypeVar("T")
Expand Down Expand Up @@ -140,16 +141,18 @@ def patch_pickups(editor: PatcherEditor, pickups_config: list[dict]):
_read_powerup_lua(),
in_pkgs=pkgs_for_lua)


def patch_exefs(exefs_patches: Path, configuration: dict):
exefs_patches.mkdir(parents=True, exist_ok=True)
patch = DSPatch()
# file needs to be named code.ips for Citra
exefs_patches.joinpath("code.ips").write_bytes(bytes(patch))


def patch(input_path: Path, output_path: Path, configuration: dict):
LOG.info("Will patch files from %s", input_path)

jsonschema.validate(instance=configuration, schema=_read_schema())
DefaultValidatingDraft7Validator(_read_schema()).validate(configuration)

out_romfs = output_path.joinpath("romfs")
out_exefs= output_path.joinpath("exefs")
Expand Down
21 changes: 21 additions & 0 deletions open_samus_returns_rando/validator_with_default.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from jsonschema import Draft7Validator, validators


def extend_with_default(validator_class):
validate_properties = validator_class.VALIDATORS["properties"]

def set_defaults(validator, properties, instance, schema):
for property, subschema in properties.items():
if "default" in subschema:
instance.setdefault(property, subschema["default"])

yield from validate_properties(
validator, properties, instance, schema,
)

return validators.extend(
validator_class, {"properties": set_defaults},
)


DefaultValidatingDraft7Validator = extend_with_default(Draft7Validator)

0 comments on commit ae36109

Please sign in to comment.