Esempio n. 1
0
 def setup(self):
     self.loaded, self.ignore = set(), set()
     signals.load.connect(self.handle_load, sender=signals.get_worker())
     signals.ignore_load.connect(self.handle_ignore,
                                 sender=signals.get_worker())
     signals.lazy_load.connect(self.handle_lazy,
                               sender=signals.get_worker())
Esempio n. 2
0
def model_select_iter(self):
    if isinstance(self, ManyToManyQuery):
        signals.lazy_load.send(
            signals.get_worker(),
            args=(self._accessor, self._instance),
            parser=parse_get_object,
        )
    return original_model_select_iter(self)
Esempio n. 3
0
 def handle_eager(self,
                  caller,
                  args=None,
                  kwargs=None,
                  context=None,
                  ret=None,
                  parser=None):
     self.tracker.track(*parser(args, kwargs, context))
     signals.touch.connect(self.handle_touch, sender=signals.get_worker())
Esempio n. 4
0
def query_execute(self):
    ret = original_query_execute(self)
    if not is_single(self._offset, self._limit):
        signals.load.send(
            signals.get_worker(),
            args=(self, ),
            ret=list(ret),
            parser=parse_load,
        )
    return ret
Esempio n. 5
0
def query_iter(self):
    ret, clone = itertools.tee(original_query_iter(self))
    if not is_single(self._offset, self._limit):
        signals.load.send(
            signals.get_worker(),
            args=(self, ),
            ret=list(clone),
            parser=parse_load,
        )
    return ret
Esempio n. 6
0
def reverse_get(self, instance, instance_type=None):
    if instance is not None:
        signals.lazy_load.send(
            signals.get_worker(),
            args=(self, instance),
            kwargs={'instance_type': instance_type},
            parser=parse_reverse_get,
        )
        return self.rel_model.select().where(
            self.field == getattr(instance, self.field.to_field.name))
    return self  # pragma: no cover; pasted from peewee
Esempio n. 7
0
def reverse_get(self, instance, instance_type=None):
    if instance is not None:
        signals.lazy_load.send(
            signals.get_worker(),
            args=(self, instance),
            kwargs={'instance_type': instance_type},
            parser=parse_reverse_get,
        )
        return self.rel_model.select().where(
            self.field == getattr(instance, self.field.to_field.name))
    return self  # pragma: no cover; pasted from peewee
def query_iter(self):
    ret, clone = itertools.tee(original_query_iter(self))
    signal = (signals.ignore_load
              if is_single(self._offset, self._limit) else signals.load)
    signal.send(
        signals.get_worker(),
        args=(self, ),
        ret=list(clone),
        parser=parse_load,
    )
    return ret
Esempio n. 9
0
def calls():
    calls = []
    def subscriber(sender, args=None, kwargs=None, context=None, ret=None, parser=None):
        calls.append(
            Call(
                parser(args, kwargs, context),
                stack.get_caller(patterns=PATTERNS)
            )
        )
    signals.lazy_load.connect(subscriber, sender=signals.get_worker())
    yield calls
def _populate_full(*args, **kwargs):
    ret = original_populate_full(*args, **kwargs)
    context = inspect.getcallargs(original_populate_full, *args, **kwargs)
    for key, _ in context['populators'].get('eager', []):
        if context['dict_'].get(key):
            signals.eager_load.send(
                signals.get_worker(),
                args=args,
                kwargs=kwargs,
                context={'key': key},
                parser=parse_populate,
            )
    return ret
Esempio n. 11
0
def _populate_full(*args, **kwargs):
    ret = original_populate_full(*args, **kwargs)
    context = inspect.getcallargs(original_populate_full, *args, **kwargs)
    for key, _ in context['populators'].get('eager', []):
        if context['dict_'].get(key):
            signals.eager_load.send(
                signals.get_worker(),
                args=args,
                kwargs=kwargs,
                context={'key': key},
                parser=parse_populate,
            )
    return ret
Esempio n. 12
0
def query_execute(self):
    ret = original_query_execute(self)
    signal = (signals.ignore_load
              if is_single(self._offset, self._limit) else signals.load)
    signal.send(
        signals.get_worker(),
        args=(self, ),
        ret=list(ret),
        parser=parse_load,
    )
    # Reset result index so that later code can call `next`
    ret._idx = 0
    return ret
Esempio n. 13
0
def reverse_get_many(self, instance, instance_type=None):
    if instance is not None:
        signals.lazy_load.send(
            signals.get_worker(),
            args=(self, instance),
            kwargs={'instance_type': instance_type},
            parser=parse_get_object,
        )
        return (fields.ManyToManyQuery(
            instance, self,
            self.rel_model).select().join(self.through_model).join(
                self.model_class).where(self.src_fk == instance))
    return self.field
Esempio n. 14
0
def reverse_get_many(self, instance, instance_type=None):
    if instance is not None:
        signals.lazy_load.send(
            signals.get_worker(),
            args=(self, instance),
            kwargs={'instance_type': instance_type},
            parser=parse_get_object,
        )
        return (fields.ManyToManyQuery(instance, self, self.rel_model)
                .select()
                .join(self.through_model)
                .join(self.model_class)
                .where(self.src_fk == instance))
    return self.field
Esempio n. 15
0
def get_object_or_id(self, instance):
    rel_id = instance._data.get(self.att_name)
    if rel_id is not None or self.att_name in instance._obj_cache:
        if self.att_name not in instance._obj_cache:
            signals.lazy_load.send(
                signals.get_worker(),
                args=(self, instance),
                parser=parse_get_object,
            )
            obj = self.rel_model.get(self.field.to_field == rel_id)
            instance._obj_cache[self.att_name] = obj
        return instance._obj_cache[self.att_name]
    elif not self.field.null:  # pragma: no cover; pasted from peewee
        raise self.rel_model.DoesNotExist
    return rel_id  # pragma: no cover; pasted from peewee
Esempio n. 16
0
def get_object_or_id(self, instance):
    rel_id = instance._data.get(self.att_name)
    if rel_id is not None or self.att_name in instance._obj_cache:
        if self.att_name not in instance._obj_cache:
            signals.lazy_load.send(
                signals.get_worker(),
                args=(self, instance),
                parser=parse_get_object,
            )
            obj = self.rel_model.get(self.field.to_field == rel_id)
            instance._obj_cache[self.att_name] = obj
        return instance._obj_cache[self.att_name]
    elif not self.field.null:  # pragma: no cover; pasted from peewee
        raise self.rel_model.DoesNotExist
    return rel_id  # pragma: no cover; pasted from peewee
Esempio n. 17
0
def query_execute(self, database):
    ret = original_query_execute(self, database)
    if hasattr(self, '_context'):
        # Query has been marked as lazy during backref lookup
        signals.lazy_load.send(
            signals.get_worker(),
            args=self._context['args'],
            kwargs=self._context['kwargs'],
            parser=parse_reverse_get,
        )
    if not isinstance(self, SelectQuery):
        return ret
    signal = (
        signals.ignore_load
        if is_single(self._offset, self._limit)
        else signals.load
    )
    signal.send(
        signals.get_worker(),
        args=(self, ),
        ret=list(ret),
        parser=parse_load,
    )
    return ret
Esempio n. 18
0
def get_rel_instance(self, instance):
    value = instance.__data__.get(self.name)
    if value is not None or self.name in instance.__rel__:
        if self.name not in instance.__rel__:
            signals.lazy_load.send(
                signals.get_worker(),
                args=(self, instance),
                parser=parse_get_object,
            )
            obj = self.rel_model.get(self.field.rel_field == value)
            instance.__rel__[self.name] = obj
        return instance.__rel__[self.name]
    elif not self.field.null:
        raise self.rel_model.DoesNotExist
    return value
Esempio n. 19
0
 def handle_eager(self, caller, args=None, kwargs=None, context=None, parser=None):
     signals.touch.connect(self.handle_touch, sender=signals.get_worker())
     parsed = parser(args, kwargs, context)
     self.touched.add(parsed)
Esempio n. 20
0
 def setup(self):
     signals.eager_load.connect(self.handle_eager,
                                sender=signals.get_worker())
     self.tracker = EagerTracker()
     self.touched = []
Esempio n. 21
0
 def setup(self):
     self.loaded = set()
     signals.load.connect(self.handle_load, sender=signals.get_worker())
     signals.lazy_load.connect(self.handle_lazy, sender=signals.get_worker())
Esempio n. 22
0
 def setup(self):
     signals.eager_load.connect(self.handle_eager, sender=signals.get_worker())
     self.touched = set()
Esempio n. 23
0
 def handle_eager(self, caller, args=None, kwargs=None, context=None, ret=None,
                  parser=None):
     self.tracker.track(*parser(args, kwargs, context))
     signals.touch.connect(self.handle_touch, sender=signals.get_worker())