def collectionOfInterface(self, interface, check=True, omit=(), prefix=None, factory=None): return RecordsProxyCollection(self, interface, check, omit, prefix, factory)
def __init__(self, registry, schema, check=True, omitted=(), prefix=None, factory=None, key_name=None): self.map = RecordsProxyCollection(registry, schema, check, omitted, prefix, factory) self.key_name = key_name if key_name is not None: # will store as ordereddict with items stored using key_name's value and order kept in special keys list keys_key = prefix + '.ordereddict_keys' if registry.get(keys_key) is None: registry.records[keys_key] = Record( field.List(title=u"Keys of prefix"), []) self.keys = registry.records[keys_key]
class RecordsProxyList(ListMixin): """A proxy that represents a List of RecordsProxy objects. Stored as prefix+"/i0001" where the number is the index """ def __init__(self, registry, schema, check=True, omitted=(), prefix=None, factory=None): self.map = RecordsProxyCollection(registry, schema, check, omitted, prefix, factory) # self.data = [v for k,v in sorted(self.map.items())] def _get_element(self, i): return self.map[self.genKey(i)] def _set_element(self, index, value): self.map[self.genKey(index)] = value def __len__(self): return len(self.map) def _resize_region(self, start, end, new_size): #move everything along one offset = new_size - (end - start) if offset > 0: moves = range(end-1, start, -1) else: moves = range(start, end, +1) for i in moves: self.map[self.genKey(i+offset)] = self.map[self.genKey(i)] # remove any additional at the end for i in range(len(self.map)+offset, len(self.map)): del self.map[self.genKey(i)] def __iter__(self): for k,v in sorted(self.map.items()): yield v # def _constructor(self, iterable): # proxy = def genKey(self, index): index_prefix = "i" return "%s%05d" %(index_prefix, index)
class RecordsProxyList(ListMixin): """A proxy that represents a List of RecordsProxy objects. Two storage schemes are supported. A pure listing stored as prefix+"/i0001" where the number is the index. If your list has a field which can be used as a primary key you can pass they key_name in as an optional paramter. This will change the storage format where each entry is prefix+"/"+key_value which looks a lot nicer in the registry. Order is still kept in a special prefix+'.ordereddict_keys' entry. """ def __init__(self, registry, schema, check=True, omitted=(), prefix=None, factory=None, key_name=None): self.map = RecordsProxyCollection(registry, schema, check, omitted, prefix, factory) self.key_name = key_name if key_name is not None: # will store as ordereddict with items stored using key_name's value and order kept in special keys list keys_key = prefix + '.ordereddict_keys' if registry.get(keys_key) is None: registry.records[keys_key] = Record( field.List(title=u"Keys of prefix"), []) self.keys = registry.records[keys_key] def _get_element(self, i): return self.map[self.genKey(i)] def get(self, id, default=None): return self.map.get(id, default) def indexof(self, id): if self.key_name is not None: item = self.map.get(id) key = getattr(item, self.key_name) return self.keys.value.index(key) else: raise Exception('No key_name set') def _set_element(self, index, value): if self.key_name is not None: #First add it to the map to ensure it's a valid key try: key = getattr(value, self.key_name) except: key = value[self.key_name] assert key assert self.keys.value.count(key) == 0 or self.keys.value.index(key) == index self.map[key] = value # we have to remove the old value if it's being overwritten oldkey = self.keys.value[index] if key != oldkey and oldkey is not None: del self.map[oldkey] self.keys.value[index] = key else: self.map[self.genKey(index)] = value def __len__(self): if self.key_name is None: return len(self.map) else: return len(self.keys.value) def _resize_region(self, start, end, new_size): if self.key_name is None: offset = new_size - (end - start) #move everything along one if offset > 0: for i in range(max(len(self.map) - 1, 0), start, -1): self.map[self.genKey(i + offset)] = self.map[self.genKey(i)] else: for i in range(end, len(self.map), +1): self.map[self.genKey(i + offset)] = self.map[self.genKey(i)] # remove any additional at the end for i in range(len(self.map) + offset, len(self.map)): del self.map[self.genKey(i)] else: for i in range(start, end): del self.map[self.keys.value[i]] self.keys.value = self.keys.value[:start] + [None for i in range(new_size)] + self.keys.value[end:] def genKey(self, index): if self.key_name is None: index_prefix = "i" return "%s%05d" % (index_prefix, index) else: if index < len(self.keys.value): return self.keys.value[index] # this could happen during registering menu items, not sure why raise StopIteration
def __init__(self, registry, schema, check=True, omitted=(), prefix=None, factory=None): self.map = RecordsProxyCollection(registry, schema, check, omitted, prefix, factory)