def expand(self): lst = [] for item in self.lookup.expand(): c = Context(self.value.clone(), {self.alias: item}) c.set_parent(self) if self.filter: f = self.filter.clone() f.set_parent(c) if not f.resolve(): continue item.set_parent(c) if self.mode == "nochain": lst.append(c) else: c.set_parent(self) val = c.resolve() if isinstance(val, list): if self.mode == "flatten": lst.extend(BoxingFactory.box(v) for v in flatten(val)) else: lst.extend(BoxingFactory.box(v) for v in val) else: lst.append(BoxingFactory.box(val)) sq = Sequence(lst) sq.set_parent(self.parent) return sq
def get(self, key): v = getattr(self.value, key) if key in self.related or key in self.many_to_many: return InstanceList(v) return BoxingFactory.box(getattr(self.value, key))
def get(self, key): try: idx = int(key) except ValueError: return BoxingFactory.box(getattr(self.value, key)) _all = self.value.objects.all() return Instance(_all[idx])
def add(self, data): boxed = BoxingFactory.box(data) if not isinstance(boxed, Mapping): raise ProgrammingError("Tried to call Config.add on type '%s'. This cannot be boxed as a 'Mapping'." % type(data)) boxed.predecessor = self.mapping if boxed.predecessor: boxed.predecessor.set_parent(boxed) self.mapping = boxed
def add(self, data): boxed = BoxingFactory.box(data) if not isinstance(boxed, Mapping): raise ProgrammingError( "Tried to call Config.add on type '%s'. This cannot be boxed as a 'Mapping'." % type(data)) boxed.predecessor = self.mapping if boxed.predecessor: boxed.predecessor.set_parent(boxed) self.mapping = boxed
def test_box_manager(self): b = BoxingFactory.box(Car.objects) self.assert_(isinstance(b, dj.InstanceList))
def test_box_instance(self): b = BoxingFactory.box(Car(name="pudding")) self.assert_(isinstance(b, dj.Instance))
def test_box_query(self): b = BoxingFactory.box(Car.objects.filter(name="pudding")) self.assert_(isinstance(b, dj.InstanceList))
def expand(self): return [BoxingFactory.box(x) for x in self.resolve()]
def box_generator(val): return Sequence(list(BoxingFactory.box(itm) for itm in val))
return Table(v) def get(self, key): if key in self.tables: return self.tables[key] if self.config.get("model"): tbl = self.tables[key] = self.get_model(key) return tbl self.error("'model' not specified") def error(self, msg): raise RuntimeError(msg) def lazy_isinstance(classname): def _(obj): cls = obj.__class__ if not hasattr(cls, "__mro__"): return False modname, cname = classname.rsplit(".", 1) return any(x.__module__ == modname and x.__name__ == cname for x in cls.__mro__) return _ BoxingFactory.register(lazy_isinstance("django.db.models.base.Model"), Instance) BoxingFactory.register(lazy_isinstance("django.db.models.query.QuerySet"), InstanceList) BoxingFactory.register(lazy_isinstance("django.db.models.manager.Manager"), InstanceList)
def get(self, idx): v = self.value.all()[idx] return BoxingFactory.box(v)
def box_dict(val): m = Mapping(None) for k, v in val.items(): m.set(k, BoxingFactory.box(v)) return m
[x.set_parent(self) for x in value] def get(self, idx): return self.value[int(idx)] #b = BoxingFactory.box(self.resolve()[int(idx)]) #return b def resolve(self): data = [] for val in self.value: data.append(val.resolve()) return data def __iter__(self): return iter(self.value) def walk(self): for val in self.value: yield val def clone(self): return Sequence([x.clone() for x in self.value]) def box_generator(val): return Sequence(list(BoxingFactory.box(itm) for itm in val)) BoxingFactory.register(inspect.isgenerator, box_generator) BoxingFactory.register(lambda x: isinstance(x, list), box_generator)
return self.tables[key] if self.config.get("model"): tbl = self.tables[key] = self.get_model(key) return tbl self.error("'model' not specified") def error(self, msg): raise RuntimeError(msg) def lazy_isinstance(classname): def _(obj): cls = obj.__class__ if not hasattr(cls, "__mro__"): return False modname, cname = classname.rsplit(".", 1) return any(x.__module__ == modname and x.__name__ == cname for x in cls.__mro__) return _ BoxingFactory.register(lazy_isinstance("django.db.models.base.Model"), Instance) BoxingFactory.register(lazy_isinstance("django.db.models.query.QuerySet"), InstanceList) BoxingFactory.register(lazy_isinstance("django.db.models.manager.Manager"), InstanceList)
def get(self, idx, default=None): return BoxingFactory.box(self.resolve()[int(idx)])
def get(self, idx): return self.value[int(idx)] #b = BoxingFactory.box(self.resolve()[int(idx)]) #return b def resolve(self): data = [] for val in self.value: data.append(val.resolve()) return data def __iter__(self): return iter(self.value) def walk(self): for val in self.value: yield val def clone(self): return Sequence([x.clone() for x in self.value]) def box_generator(val): return Sequence(list(BoxingFactory.box(itm) for itm in val)) BoxingFactory.register(inspect.isgenerator, box_generator) BoxingFactory.register(lambda x: isinstance(x, list), box_generator)
data[key] = self.get(key).resolve() return data def walk(self): for itm in self.value.items(): yield itm yield self.predecessor def clone(self): p = None if self.predecessor: p = self.predecessor.clone() m = Mapping(p) for k, v in self.value.items(): m.set(k, v.clone()) return m def __iter__(self): return iter(BoxingFactory.box(v) for v in sorted(self.keys())) def box_dict(val): m = Mapping(None) for k, v in val.items(): m.set(k, BoxingFactory.box(v)) return m BoxingFactory.register(lambda x: isinstance(x, dict), box_dict)
def __iter__(self): return iter(BoxingFactory.box(v) for v in sorted(self.keys()))