Skip to content

Commit

Permalink
DPNLPF-2213: Fix non-working includes
Browse files Browse the repository at this point in the history
  • Loading branch information
bart0003 committed Feb 9, 2024
1 parent bc7082d commit 69e29b1
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 21 deletions.
6 changes: 4 additions & 2 deletions core/repositories/folder_filename_key_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ def _form_file_upload_map(self, shard_desc):
for shard in shard_desc:
shard_data = self._load_item(shard)
if shard_data:
filename_to_data[os.path.relpath(shard, self.path)] = shard_data
filename_to_data[os.path.relpath(shard, self.path).split(".")[0]] = shard_data
self._save_to_repo(filename_to_data, shard, shard_data)
return filename_to_data

def _save_to_repo(self, repo, shard_path, shard_data):
repo[os.path.basename(shard_path).split(".")[0]] = shard_data

def fill(self, data):
self.data = data
26 changes: 10 additions & 16 deletions smart_kit/message/validators/json_schema_validator.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from __future__ import annotations
from typing import Callable, TYPE_CHECKING

import fastjsonschema
from pathlib import Path
from typing import Callable, TYPE_CHECKING
import jsonschema
from jsonschema.validators import RefResolver

from core.message.message import SmartAppMessage
from core.logging.logger_utils import log
Expand All @@ -16,19 +17,19 @@ class JSONSchemaValidator(BaseMessageValidatorWithResources):
VALIDATOR_EXCEPTION = jsonschema.ValidationError

def _update_resources(self):
schemas = self.resources.get("payload_schema")
self._schemas = {k: self._compile_schema(v) for k, v in schemas.items() if '.json' not in k and "/" not in k}
self._store = self.resources.get("payload_schema")
self._schemas = {Path(k).stem: self._compile_schema(k, v) for k, v in self._store.items()
if len(Path(k).parents) == 1}

def _compile_schema(self, schema: dict) -> Callable[[dict], None]:
def _compile_schema(self, uri: str, schema: dict) -> Callable[[dict], None]:
cls = jsonschema.validators.validator_for(schema)
cls.check_schema(schema)
instance = cls(schema)
instance.resolver.handlers[""] = self._handle_includes
instance = cls(schema, resolver=RefResolver(base_uri=uri, referrer=schema,
handlers={"": self._handle_includes}))
return instance.validate

def _handle_includes(self, path):
schemas = self.resources.get("payload_schema")
return schemas[path]
return self._store[path]

def _log(self, exception: VALIDATOR_EXCEPTION, message: SmartAppMessage, level="WARNING"):
log_params = self._log_params(message)
Expand All @@ -40,10 +41,3 @@ def _validate(self, message: SmartAppFromMessage):
validator = self._schemas.get(message.message_name)
if validator:
validator(message.payload)


class FastJSONSchemaValidator(JSONSchemaValidator):
VALIDATOR_EXCEPTION = fastjsonschema.JsonSchemaValueException

def _compile_schema(self, schema: dict) -> Callable[[dict], None]:
return fastjsonschema.compile(schema, handlers={"": self._handle_includes})
Empty file.
9 changes: 9 additions & 0 deletions smart_kit/repositories/payload_schema_repository.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import os

from core.repositories.folder_filename_key_repository import FolderFilenameKeyRepository


class PayloadSchemaRepository(FolderFilenameKeyRepository):
def _save_to_repo(self, repo, shard_path, shard_data):
path = os.path.relpath(shard_path, self.path)
repo[path if path.startswith("/") else f"/{path}"] = shard_data
6 changes: 3 additions & 3 deletions smart_kit/resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
from core.model.registered import registered_factories
from core.repositories.file_repository import FileRepository
from core.repositories.folder_repository import FolderRepository
from core.repositories.folder_filename_key_repository import FolderFilenameKeyRepository
from core.request.base_request import requests_registered
from core.request.rest_request import RestRequest
from core.utils.loader import ordered_json
Expand Down Expand Up @@ -98,6 +97,7 @@
from smart_kit.action.smart_geo_action import SmartGeoAction
from smart_kit.message.get_to_message import to_messages
from smart_kit.message.as_is_to_message import AsIsToMessage
from smart_kit.repositories.payload_schema_repository import PayloadSchemaRepository
from smart_kit.request.kafka_request import SmartKitKafkaRequest

from core.db_adapter.aioredis_sentinel_adapter import AIORedisSentinelAdapter
Expand Down Expand Up @@ -133,8 +133,8 @@ def __init__(self, source, references_path, settings):
source=source, key="last_action_ids"),
FolderRepository(self.subfolder_path("bundles"), loader=ordered_json, source=source,
key="bundles"),
FolderFilenameKeyRepository(self.subfolder_path("payload_schema"), loader=json.loads, source=source,
key="payload_schema"),
PayloadSchemaRepository(self.subfolder_path("payload_schema"), loader=json.loads, source=source,
key="payload_schema"),
]

self.repositories = self.override_repositories(self.repositories)
Expand Down

0 comments on commit 69e29b1

Please sign in to comment.