Skip to content
Snippets Groups Projects
Commit da9a0fe039b6 authored by Houzefa Abbasbhay's avatar Houzefa Abbasbhay :slight_smile:
Browse files

``jsonschema`` ➔ ``fastjsonschema``

parent c23bed64ca21
No related branches found
No related tags found
3 merge requests!37Merge 15.0 (15.0.3.2.0 with fastjsonschema),!36Merge 13.0 (13.0.3.2.0 with fastjsonschema),!34``jsonschema`` ➔ ``fastjsonschema``
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
History History
******* *******
13.0.3.2.0
==========
Export constants from ``.base``. Export constants from ``.base``.
Used by other modules; these exports got removed when I replaced an ``import *`` earlier. Used by other modules; these exports got removed when I replaced an ``import *`` earlier.
...@@ -5,6 +8,8 @@ ...@@ -5,6 +8,8 @@
Export constants from ``.base``. Export constants from ``.base``.
Used by other modules; these exports got removed when I replaced an ``import *`` earlier. Used by other modules; these exports got removed when I replaced an ``import *`` earlier.
``jsonschema`` ➔ ``fastjsonschema``.
13.0.3.1.0 13.0.3.1.0
========== ==========
......
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
{ {
"name": "Converter", "name": "Converter",
"license": "AGPL-3", "license": "AGPL-3",
"version": "13.0.3.1.0", "version": "13.0.3.2.0",
"category": "Hidden", "category": "Hidden",
"author": "XCG Consulting", "author": "XCG Consulting",
"website": "https://odoo.consulting/", "website": "https://odoo.consulting/",
...@@ -29,5 +29,5 @@ ...@@ -29,5 +29,5 @@
"data": [], "data": [],
"installable": True, "installable": True,
# These dependencies are in the "requirements" file. # These dependencies are in the "requirements" file.
"external_dependencies": {"python": ["jsonschema"]}, "external_dependencies": {"python": ["fastjsonschema"]},
} }
...@@ -34,8 +34,6 @@ ...@@ -34,8 +34,6 @@
) )
from .validate import VALIDATION_SKIP, VALIDATION_STRICT, Validator from .validate import VALIDATION_SKIP, VALIDATION_STRICT, Validator
import jsonschema
_logger = logging.getLogger(__name__) _logger = logging.getLogger(__name__)
...@@ -96,7 +94,7 @@ ...@@ -96,7 +94,7 @@
if self.validation != VALIDATION_SKIP and self._jsonschema is not None: if self.validation != VALIDATION_SKIP and self._jsonschema is not None:
try: try:
self.validator.validate(self._jsonschema, message_data) self.validator.validate(self._jsonschema, message_data)
except jsonschema.exceptions.ValidationError as exception: except Exception as exception:
_logger.warning("Validation failed", exc_info=1) _logger.warning("Validation failed", exc_info=1)
if self.validation == VALIDATION_STRICT: if self.validation == VALIDATION_STRICT:
raise exception raise exception
......
# Used by converter module # Used by converter module
jsonschema==3.2.0 fastjsonschema
...@@ -19,9 +19,8 @@ ...@@ -19,9 +19,8 @@
############################################################################## ##############################################################################
import json import json
import math
import os import os
from typing import NoReturn from typing import NoReturn
import odoo.addons import odoo.addons
...@@ -23,9 +22,9 @@ ...@@ -23,9 +22,9 @@
import os import os
from typing import NoReturn from typing import NoReturn
import odoo.addons import odoo.addons
import jsonschema import fastjsonschema
VALIDATION_SKIP = "skip" VALIDATION_SKIP = "skip"
VALIDATION_SOFT = "soft" VALIDATION_SOFT = "soft"
...@@ -47,5 +46,4 @@ ...@@ -47,5 +46,4 @@
self.repository = repository self.repository = repository
# exemple "https://annonces-legales.fr/xbus/schemas/v1/{}.schema.json" # exemple "https://annonces-legales.fr/xbus/schemas/v1/{}.schema.json"
self.default_url_pattern = default_url_pattern self.default_url_pattern = default_url_pattern
self.schemastore = {}
self.validators = {} self.validators = {}
...@@ -51,5 +49,4 @@ ...@@ -51,5 +49,4 @@
self.validators = {} self.validators = {}
self.search_path = "not initialized, please call initialize()"
self.initialized = False self.initialized = False
self.encoding = "UTF-8" self.encoding = "UTF-8"
...@@ -57,6 +54,9 @@ ...@@ -57,6 +54,9 @@
repo_module_basepath = os.path.dirname( repo_module_basepath = os.path.dirname(
getattr(odoo.addons, self.repository_module_name).__file__ getattr(odoo.addons, self.repository_module_name).__file__
) )
# Read local schema definitions.
schemas = {}
schema_search_path = os.path.abspath( schema_search_path = os.path.abspath(
os.path.join(repo_module_basepath, self.repository) os.path.join(repo_module_basepath, self.repository)
) )
...@@ -60,12 +60,10 @@ ...@@ -60,12 +60,10 @@
schema_search_path = os.path.abspath( schema_search_path = os.path.abspath(
os.path.join(repo_module_basepath, self.repository) os.path.join(repo_module_basepath, self.repository)
) )
self.search_path = schema_search_path for root, _dirs, files in os.walk(schema_search_path):
for root, _dirs, files in os.walk(self.search_path):
for fname in files: for fname in files:
fpath = os.path.join(root, fname) fpath = os.path.join(root, fname)
if fpath.endswith((".json",)): if fpath.endswith((".json",)):
with open(fpath, "r", encoding=self.encoding) as schema_fd: with open(fpath, "r", encoding=self.encoding) as schema_fd:
schema = json.load(schema_fd) schema = json.load(schema_fd)
if "$id" in schema: if "$id" in schema:
...@@ -66,9 +64,18 @@ ...@@ -66,9 +64,18 @@
for fname in files: for fname in files:
fpath = os.path.join(root, fname) fpath = os.path.join(root, fname)
if fpath.endswith((".json",)): if fpath.endswith((".json",)):
with open(fpath, "r", encoding=self.encoding) as schema_fd: with open(fpath, "r", encoding=self.encoding) as schema_fd:
schema = json.load(schema_fd) schema = json.load(schema_fd)
if "$id" in schema: if "$id" in schema:
self.schemastore[schema["$id"]] = schema schemas[schema["$id"]] = schema
# Prepare validators for each schema. We add an HTTPS handler that
# points back to our schema definition cache built above.
for schema_id, schema in schemas.items():
self.validators[schema_id] = fastjsonschema.compile(
schema,
handlers={"https": lambda uri: schemas[uri]},
use_default=False,
)
self.initialized = True self.initialized = True
...@@ -73,6 +80,6 @@ ...@@ -73,6 +80,6 @@
self.initialized = True self.initialized = True
def validate(self, schema_id, doc) -> NoReturn: def validate(self, schema_id, payload) -> NoReturn:
if not self.initialized: if not self.initialized:
raise NotInitialized("please call the initialize() method") raise NotInitialized("please call the initialize() method")
...@@ -76,38 +83,4 @@ ...@@ -76,38 +83,4 @@
if not self.initialized: if not self.initialized:
raise NotInitialized("please call the initialize() method") raise NotInitialized("please call the initialize() method")
uri = "file://{}/{}.schema.json".format(self.search_path, schema_id) self.validators[self.default_url_pattern.format(schema_id)](payload)
validator = self.validators.get(uri)
if validator is None:
schema = self.schemastore[
self.default_url_pattern.format(schema_id)
]
resolver = jsonschema.RefResolver(uri, schema, self.schemastore)
validator = jsonschema.Draft7Validator(schema, resolver=resolver)
self.validators[uri] = validator
validator.validate(doc)
def multiple_of(validator, db, instance, schema):
"""Fix rounding which fails when comparing e.g. 66.6 vs 0.01"""
if not validator.is_type(instance, "number"):
return
if isinstance(db, float):
quotient = instance / db
failed = not math.isclose(round(quotient), quotient)
else:
failed = instance % db
if failed:
yield jsonschema.ValidationError(
"%r is not a multiple of %r" % (instance, db)
)
# Fix rounding which fails when comparing e.g. 66.6 vs 0.01
jsonschema.Draft7Validator.VALIDATORS["multipleOf"] = multiple_of
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment