diff options
author | F. Eugene Aumson <feuGeneA@users.noreply.github.com> | 2018-12-14 03:31:07 +0800 |
---|---|---|
committer | F. Eugene Aumson <feuGeneA@users.noreply.github.com> | 2018-12-15 05:53:21 +0800 |
commit | 087469f1f303285c3c234b367f60dbe8a0e9258a (patch) | |
tree | 4ac0c7035878934a64484a9d1a96cff9bc25b468 | |
parent | b6c8126589a94c2986c591ad7741cd3787a96e58 (diff) | |
download | dexon-sol-tools-087469f1f303285c3c234b367f60dbe8a0e9258a.tar dexon-sol-tools-087469f1f303285c3c234b367f60dbe8a0e9258a.tar.gz dexon-sol-tools-087469f1f303285c3c234b367f60dbe8a0e9258a.tar.bz2 dexon-sol-tools-087469f1f303285c3c234b367f60dbe8a0e9258a.tar.lz dexon-sol-tools-087469f1f303285c3c234b367f60dbe8a0e9258a.tar.xz dexon-sol-tools-087469f1f303285c3c234b367f60dbe8a0e9258a.tar.zst dexon-sol-tools-087469f1f303285c3c234b367f60dbe8a0e9258a.zip |
Support ALL the schemas
-rwxr-xr-x | python-packages/json_schemas/setup.py | 2 | ||||
-rw-r--r-- | python-packages/json_schemas/src/zero_ex/json_schemas/__init__.py | 39 | ||||
-rw-r--r-- | python-packages/json_schemas/stubs/stringcase/__init__.pyi | 2 |
3 files changed, 23 insertions, 20 deletions
diff --git a/python-packages/json_schemas/setup.py b/python-packages/json_schemas/setup.py index f8c3cf14b..7813f101f 100755 --- a/python-packages/json_schemas/setup.py +++ b/python-packages/json_schemas/setup.py @@ -136,7 +136,7 @@ setup( "publish": PublishCommand, "publish_docs": PublishDocsCommand, }, - install_requires=["jsonschema", "mypy_extensions"], + install_requires=["jsonschema", "mypy_extensions", "stringcase"], extras_require={ "dev": [ "bandit", diff --git a/python-packages/json_schemas/src/zero_ex/json_schemas/__init__.py b/python-packages/json_schemas/src/zero_ex/json_schemas/__init__.py index a76a2fa3b..f609ea103 100644 --- a/python-packages/json_schemas/src/zero_ex/json_schemas/__init__.py +++ b/python-packages/json_schemas/src/zero_ex/json_schemas/__init__.py @@ -6,6 +6,7 @@ from typing import Mapping from pkg_resources import resource_string import jsonschema +from stringcase import snakecase class _LocalRefResolver(jsonschema.RefResolver): @@ -13,19 +14,23 @@ class _LocalRefResolver(jsonschema.RefResolver): def __init__(self): """Initialize a new instance.""" - self.ref_to_file = { - "/addressSchema": "address_schema.json", - "/hexSchema": "hex_schema.json", - "/orderSchema": "order_schema.json", - "/wholeNumberSchema": "whole_number_schema.json", - "/ECSignature": "ec_signature_schema.json", - "/signedOrderSchema": "signed_order_schema.json", - "/ecSignatureParameterSchema": ( - "ec_signature_parameter_schema.json" + "" - ), - } jsonschema.RefResolver.__init__(self, "", "") + @staticmethod + def _ref_to_file(ref: str) -> str: + """Translate a JSON schema ref to its corresponding file name. + + >>> _LocalRefResolver._ref_to_file("/addressSchema") + 'address_schema.json' + """ + _ref = ref.lstrip("/") + + # handle weird special cases + _ref = _ref.replace("ECSignature", "EcSignature") + _ref = _ref.replace("Schema", "") + + return f"{snakecase(_ref)}_schema.json" + def resolve_from_url(self, url: str) -> str: """Resolve the given URL. @@ -35,15 +40,11 @@ class _LocalRefResolver(jsonschema.RefResolver): `url` does not exist. """ ref = url.replace("file://", "") - if ref in self.ref_to_file: - return json.loads( - resource_string( - "zero_ex.json_schemas", f"schemas/{self.ref_to_file[ref]}" - ) + return json.loads( + resource_string( + "zero_ex.json_schemas", + f"schemas/{_LocalRefResolver._ref_to_file(ref)}", ) - raise jsonschema.ValidationError( - f"Unknown ref '{ref}'. " - + f"Known refs: {list(self.ref_to_file.keys())}." ) diff --git a/python-packages/json_schemas/stubs/stringcase/__init__.pyi b/python-packages/json_schemas/stubs/stringcase/__init__.pyi new file mode 100644 index 000000000..56d784cf5 --- /dev/null +++ b/python-packages/json_schemas/stubs/stringcase/__init__.pyi @@ -0,0 +1,2 @@ +def snakecase(_: str): + ... |