Ejemplo n.º 1
0
class TestClass(churro.Persistent):
    one = churro.PersistentProperty()
    two = churro.PersistentProperty()

    def __init__(self, one, two):
        self.one = one
        self.two = two
Ejemplo n.º 2
0
class Dummy(churro.Persistent):
    __module__ = "churrodb.tests"
    value = churro.PersistentProperty()

    def __init__(self, value=None):
        if value is not None:
            self.value = value
Ejemplo n.º 3
0
class GitDictKeyHashIndex(GitObjectHashIndex):
    dict_key = churro.PersistentProperty()

    def __init__(self, *args, **kwargs):
        try:
            self.dict_key = kwargs.pop("dict_key")
        except KeyError:
            self.dict_key = "id"

        super().__init__(*args, **kwargs)

    def __new__(cls, *args, **kwargs):
        obj = super().__new__(cls, *args, **kwargs)
        obj.git_index_key_mapper = obj.mapper_factory()
        return obj

    def mapper_factory(self):
        def git_index_key_mapper(k, v):
            return v.get(self.dict_key)
        return git_index_key_mapper

    def idx_update(self, data=None):
        super().idx_update(GitObjectProxy(data, self.git_index_key_mapper))
Ejemplo n.º 4
0
class GitObjectHashIndex(AbstractDictIndex):
    _inverse = churro.PersistentProperty()
    name = churro.PersistentProperty()
    supply = churro.PersistentProperty()
    auxiliary = churro.PersistentProperty()
    clear_before_update = churro.PersistentProperty()

    def __init__(
            self, inverse=False, clear_before_update=False,
            supply=None, name=None):
        self._inverse = inverse
        self._db = None
        self.name = name
        self.supply = supply
        self.clear_before_update = clear_before_update
        self.auxiliary = churro.PersistentDict()
        super().__init__()

    def __new__(cls, *args, **kwargs):
        obj = super().__new__(cls, *args, **kwargs)
        obj._db = None
        return obj

    @property
    def churrodb(self):
        return self._db

    @churrodb.setter
    def churrodb(self, value):
        if value is not None:
            self._db = value
            if self.name is not None:
                self._db.named_indexes[self.name] = self

    def supply_index(self):
        if self.churrodb is None:
            return
        if self.supply is not None:
            if self.name is None:
                raise Exception("must provide name for this sub-index")
            return self.churrodb.named_indexes[self.supply]
        return None

    def idx_update(self, data=None, namespace=None):
        if self.churrodb is None:
            return
        supply_index = self.supply_index()
        if supply_index is not None:
            supply_index.idx_update(data, namespace=self.name)
            return

        db = self.churrodb
        db.flush()

        if namespace is not None:
            if namespace not in self.auxiliary:
                self.auxiliary[namespace] = churro.PersistentDict()
            target = self.auxiliary[namespace]
        else:
            target = self

        if self.clear_before_update:
            target.clear()

        seen_keys = []

        log.info("building git object hash index (" + str(self) + ")...")
        for key, value in data.items():
            resource_path = churro.resource_path(value)
            if not db.fs.isdir(resource_path):
                resource_path += churro.CHURRO_EXT

            hash = db.fs.hash(resource_path)

            if self._inverse:
                target_key = hash
                target_value = key
            else:
                target_key = key
                target_value = hash

            if target_key in seen_keys:
                raise IndexUpdateError(
                    "duplicate key '{key}' for values '{value_a}' and '{value_b}'l"
                        .format(key=target_key, value_a=target[target_key], value_b=target_value))

            seen_keys.append(target_key)
            target[target_key] = target_value

    def idx_find(self, key, subindex=None):
        found = self.get(key)

        if found is None:
            for k, v in self.auxiliary.items():
                found = v.get(key)
                if found is not None:
                    break

        if found is None:
            return []
        else:
            return [found]

    idx_find_first = idx_find_first

    def by_name(self, name):
        for key, subindex in self.auxiliary.items():
            if key.startswith(name):
                return subindex
Ejemplo n.º 5
0
        class MyCollectionA(churrodb.GitIndexMixin, churro.PersistentFolder):
            _index = churro.PersistentProperty()

            def git_index_key_mapper(k, v):
                return k
Ejemplo n.º 6
0
class GitIndexedCollection(churrodb.GitIndexMixin, churro.PersistentFolder):
    __module__ = "churrodb.tests"
    _index = churro.PersistentProperty()

    def git_index_key_mapper(k, v):
        return k
Ejemplo n.º 7
0
 class Prop(churro.Persistent):
     c = churro.PersistentProperty()