from edbm import edbm import ConfigParser, string class Retrieval: def __init__(self, root): self.root = root self.config = ConfigParser.ConfigParser({'root':root, 'list':'',}) self.config.read(['%s/edbm.config' % root]) return class Db: def __init__(self, list): self.list = list self._factory = None return class Result: def __init__(self, result, total): self.total = total self.result = result self.count = len(result) self.factory = result[0].factory return class List: def __init__(self, result, indices): self.result = result self.indices = indices self.index = 0 return def __getitem__(self, index): count, index = self.indices[index] return self.result[count][index] def __getslice__(self, low, high): list = [] for count, index in self.indices[low:high]: list.append(self.result[count][index]) return list def __iter__(self): return self def next(self): if self.index >= len(self.indices): raise StopIteration count, index = self.indices[self.index] self.index += 1 return self.result[count][index] def __len__(self): length = 0 for result in self.result: length += len(result) return length def __nonzero__(self): return self.total != 0 def __iter__(self): return self.List(self.result, self.indices()) def __getitem__(self, index): count = 0 while count < self.count and not len(self.result[count]): count += 1 while count < self.count and index >= len(self.result[count]): index -= len(self.result[count]) count += 1 if count >= self.count: return None return self.result[count][index] def __getslice__(self, low, high): return self.List(self.result, self.indices()[low:high]) def ids(self): list = [] for count in range(self.count): for id in self.result[count].ids(): list.append((count, id)) return list def indices(self): list = [] for count in range(self.count): for index in range(len(self.result[count])): list.append((count, index)) return list def find(self, query, first, last): total = 0 list = [] for db in self.list: result = db.find(query, first, last) list.append(result) first = max(1, first-result.total) last -= result.total if first > last: first = 1 last = 0 total += result.total self._factory = result.factory return self.Result(list, total) def open(self, name, format='short', factory=None): self.name = name self.list = [] for name in self.config.get(name, 'list').split(','): name = name.strip() if name: self.list.append(name) if not self.list: self.list = [self.name] if edbm.config == None: edbm.config = self.config list = [] for name in self.list: db = edbm.opendb(name, 0) if not factory: factory = edbm.Factory db.setfactory(factory(db)) if format == 'complete': db.set_blob_retrieve() list.append(db) if len(list) == 1: return db return self.Db(list) def close(self, name): for name in self.list: edbm.closedb(name) return def access(self, ip, path): return edbm.access(ip, path) class Retrieval_old: def __init__(self, root): self.root = root config = ConfigParser.ConfigParser({'app_root': root}) config.read(['%s/defaults.config' % root, '%s/edbm.config' % root]) self.config = config return def open(self, name, format, factory=None): if edbm.config == None: edbm.config = self.config db = edbm.opendb(name, 0) if not factory: factory = edbm.Factory db.setfactory(factory(db)) if format == "complete": db.set_blob_retrieve() return db def access(self, ip, path): return edbm.access(ip, path) class Factory(edbm.Factory): pass