@@ -0,0 +1,2739 @@
+import abc%0A%0Afrom wdim.client import fields%0A%0A%0Aclass StorableMeta(abc.ABCMeta):%0A%0A    STORABLE_CLASSES = %7B%7D%0A%0A    def __init__(cls, name, bases, dct):%0A        super().__init__(name, bases, dct)%0A%0A        cls._collection_name = cls.__name__.lower()%0A%0A        cls._fields = %7B%7D%0A%0A        for key, value in cls.__dict__.items():%0A            if not isinstance(value, fields.Field):%0A                continue%0A%0A            cls._fields%5Bkey%5D = value%0A%0A        cls._fields = %7B%0A            key: value%0A            for key, value%0A            in cls.__dict__.items()%0A            if isinstance(value, fields.Field)%0A        %7D%0A%0A        if cls.__name__ != 'Storable':%0A            assert '_id' in cls._fields, '_id must be specified'%0A            StorableMeta.STORABLE_CLASSES%5Bcls._collection_name%5D = cls%0A%0A%0Aclass _ConnectionAssertion:%0A%0A    def __get__(self, inst, owner):%0A        raise ValueError('connect must be called before interacting with the database')%0A%0A%0Aclass Storable(metaclass=StorableMeta):%0A%0A    _DATABASE = _ConnectionAssertion()%0A%0A    @classmethod%0A    def ClassGetter(cls, name):%0A        def getter():%0A            try:%0A                return StorableMeta%5Bname.lower()%5D%0A            except KeyError:%0A                raise KeyError('Class %7B%7D not found'.format(name))%0A        return getter%0A%0A    @classmethod%0A    async def _bootstrap(cls):%0A        for klass in Storable.__subclasses__():%0A            await cls._DATABASE.ensure_index(%0A                klass._collection_name,%0A                %5B%0A                    %7B'key': key, 'unique': value.unique%7D%0A                    for key, value in klass._fields.items()%0A                    if value.index%0A                %5D)%0A        return True%0A%0A    @classmethod%0A    async def connect(cls, db, bootstrap=True):%0A        Storable._DATABASE = db%0A        if bootstrap:%0A            return await Storable._bootstrap()%0A        return True%0A%0A    @classmethod%0A    async def create(cls, *, _id=None, **kwargs):%0A        assert _id is None, '_id cannot be supplied'%0A        inst = cls(**kwargs)%0A%0A        _id = await cls._DATABASE.insert(inst)%0A        cls._fields%5B'_id'%5D.__set__(inst, _id, override=True)%0A%0A        return inst%0A%0A    @classmethod%0A    def from_document(cls, document):%0A        return cls(**document)%0A%0A    @classmethod%0A    async def find_one(cls, query):%0A        return cls.from_document(%0A            await Storable._DATABASE.find_one(cls, query)%0A        )%0A%0A    def __init__(self, **kwargs):%0A        self._data = %7B%0A            key: value.parse(kwargs.get(key))%0A            for key, value in self._fields.items()%0A        %7D%0A%0A    async def to_document(self, join=False):%0A        return %7B%0A            key: value.to_document(self._data.get(key), join=join)%0A            for key, value%0A            in self._fields.items()%0A        %7D%0A

Implement Storable