2016-07-23 06:18:23 +03:00
|
|
|
class Registry(object):
|
|
|
|
def __init__(self):
|
|
|
|
self._registry = {}
|
|
|
|
self._registry_models = {}
|
2016-09-01 00:39:16 +03:00
|
|
|
self._registry_composites = {}
|
2016-07-23 06:18:23 +03:00
|
|
|
|
|
|
|
def register(self, cls):
|
|
|
|
from .types import SQLAlchemyObjectType
|
|
|
|
assert issubclass(cls, SQLAlchemyObjectType), 'Only SQLAlchemyObjectType can be registered, received "{}"'.format(cls.__name__)
|
|
|
|
assert cls._meta.registry == self, 'Registry for a Model have to match.'
|
2016-08-15 01:42:27 +03:00
|
|
|
# assert self.get_type_for_model(cls._meta.model) in [None, cls], (
|
|
|
|
# 'SQLAlchemy model "{}" already associated with '
|
|
|
|
# 'another type "{}".'
|
|
|
|
# ).format(cls._meta.model, self._registry[cls._meta.model])
|
2016-07-23 06:18:23 +03:00
|
|
|
self._registry[cls._meta.model] = cls
|
|
|
|
|
|
|
|
def get_type_for_model(self, model):
|
|
|
|
return self._registry.get(model)
|
|
|
|
|
2016-09-01 00:39:16 +03:00
|
|
|
def register_composite_converter(self, composite, converter):
|
|
|
|
self._registry_composites[composite] = converter
|
|
|
|
|
|
|
|
def get_converter_for_composite(self, composite):
|
|
|
|
return self._registry_composites.get(composite)
|
|
|
|
|
2016-07-23 06:18:23 +03:00
|
|
|
|
|
|
|
registry = None
|
|
|
|
|
|
|
|
|
|
|
|
def get_global_registry():
|
|
|
|
global registry
|
|
|
|
if not registry:
|
|
|
|
registry = Registry()
|
|
|
|
return registry
|
|
|
|
|
|
|
|
|
|
|
|
def reset_global_registry():
|
|
|
|
global registry
|
|
|
|
registry = None
|