MessageCallback = Callable[['Message'], Coroutine[Any, Any, None]]
+# Global cache of JSON schema validators:
+_validators: Dict[str, jsonschema.Draft7Validator] = {}
+
+
class Message(Dict[str, MessageValue]):
"""Define arbitrary message.
>>> print(t)
{'key': {'const': 'value'}}
"""
- self._validators: Dict[str, jsonschema.Draft7Validator] = {}
if init is not None:
self.update(init)
TypeError: 'schema' is not a valid value in MessageTemplate
(not a valid JSON schema).
"""
+ global _validators
if not isinstance(key, str):
raise TypeError(f"'{key}' is not a valid key in MessageTemplate"
" (not a string).")
- try:
- jsonschema.Draft7Validator.check_schema(value)
- # Draft7Validator is hardcoded, because _LATEST_VERSION is
- # non-public in jsonschema and we also perhaps do not want to
- # upgrade automatically.
- except jsonschema.exceptions.SchemaError:
- raise TypeError(f"'{value}' is not a valid value in"
- " MessageTemplate (not a valid JSON schema).")
- self._validators[key] = jsonschema.Draft7Validator(value)
+ schema_string = json.dumps(value)
+ if schema_string not in _validators:
+ try:
+ jsonschema.Draft7Validator.check_schema(value)
+ # Draft7Validator is hardcoded, because _LATEST_VERSION is
+ # non-public in jsonschema and we also perhaps do not want
+ # to upgrade automatically.
+ except jsonschema.exceptions.SchemaError:
+ raise TypeError(f"'{value}' is not a valid value in"
+ " MessageTemplate (not a valid JSON schema).")
+ _validators[schema_string] = jsonschema.Draft7Validator(value)
super().__setitem__(key, value)
def update(self, *args, **kwargs) -> None:
if key not in message:
return False
else:
- validator = self._validators[key]
+ schema_string = json.dumps(self[key])
+ validator = _validators[schema_string]
for error in validator.iter_errors(message[key]):
return False
return True
>>> r = MessageTemplateRegistry()
"""
- self._validators: Dict[str, jsonschema.Draft7Validator] = {}
self._clients: List[str] = []
self._children: Dict[str, Dict[str, MessageTemplateRegistry]] = {}
{'k1': 'v2', 'k2': 'v1'}: False
{'k1': 'v2', 'k2': 'v2'}: True
"""
+ global _validators
if client in self._clients:
return True
for key in self._children:
if key in message:
for schema_string in self._children[key]:
- if schema_string not in self._validators:
+ if schema_string not in _validators:
schema = json.loads(schema_string)
- self._validators[schema_string] = \
+ _validators[schema_string] = \
jsonschema.Draft7Validator(schema)
- validator = self._validators[schema_string]
+ validator = _validators[schema_string]
validated = True
for error in validator.iter_errors(message[key]):
validated = False
{'k1': 'v2', 'k2': 'v1'}: []
{'k1': 'v2', 'k2': 'v2'}: ['Client 2']
"""
+ global _validators
result = []
for client in self._clients:
if client not in result:
for key in self._children:
if key in message:
for schema_string in self._children[key]:
- if schema_string not in self._validators:
+ if schema_string not in _validators:
schema = json.loads(schema_string)
- self._validators[schema_string] = \
+ _validators[schema_string] = \
jsonschema.Draft7Validator(schema)
- validator = self._validators[schema_string]
+ validator = _validators[schema_string]
validated = True
for error in validator.iter_errors(message[key]):
validated = False