def __iter__(self): """Wrap self.data with limit awareness, yielding objects. """ if self.data is None: self.refresh() import dewey # avoid circular import catalog = dewey.get_catalog() resources = catalog.resources # {rid:Resource} i = 0 if self.sort is None: for rid in self.data: if i == self.limit: break yield resources[rid] i += 1 else: sorted = catalog.indices[self.sort].sorted for foo, rids in sorted.iteritems(): for rid in rids: if i == self.limit: break if rid in self.data: yield resources[rid] i += 1
def main(catalog_factory=None, argv=None): """ """ if argv is None: argv = sys.argv # Parse the db connection string. # =============================== arg = argv[1:2] if not arg: print >> sys.stderr, "Please specify a db connection string" print >> sys.stderr, "e.g. file://~/foo.dat or zeo://localhost:9100" raise SystemExit() dbconn = arg[0] if dbconn in '.'+os.sep or not dbconn.startswith('zeo://'): dbconn = 'file://' + dbconn # Run a command or enter interactive mode. # ======================================== dewey.open(dbconn, catalog_factory) cli = CLI() command = ' '.join(argv[2:]) try: if command == 'crawl': dewey.get_catalog().crawl() else: try: cli.cmdloop() except KeyboardInterrupt: cli.onecmd('EOF') finally: dewey.close()
def __set_sort(self, value): """Set with validation. """ if value is None: self.__sort = None return # short-cut elif not isinstance(value, basestring): raise TypeError("string expected") import dewey # avoid circular import index = dewey.get_catalog().indices.get(value, None) if index is None: raise ValueError("no such index: '%s'" % value) sorted = getattr(index, 'sorted', None) if sorted is None: raise ValueError("index '%s' not sortable" % value) self.__sort = value
def validate(self, index_name, search_name, arg): """Given an index, search, and arg, return search and arg. """ import dewey # avoid circular import catalog = dewey.get_catalog() if index_name is None: return catalog.rids # everything if index_name not in catalog.indices: raise ValueError("unknown index: '%s'" % index_name) index = catalog.indices[index_name] call = getattr(index, search_name, None) if call is None: raise ValueError( "unknown search type '%s' " % search_name + "for index '%s'" % index_name ) return call, arg
def refresh(self): """Load the data set from the database. self.constraints contains a list of lists. Within each sublist, the terms are either ANDed or NOTed together; the results are then ORed. The below could be optimized in a couple ways: - stop searching as soon as the result set proves empty - perform both levels of merge from smallest to largest See original ZCatalog code for implementation hints. """ import dewey # avoid circular import all = dewey.get_catalog().rids if self.constraints is None: results = all else: results = [] for grouping in self.constraints: for operation, query, (call, arg) in grouping: if (operation is None) and (call is None): # OR result = all elif (operation is None) and (call is not None): # OR ... result = call(arg) else: # AND/NOT assert None not in (operation, call) # safety net result = operation(result, call(arg)) if result is not None: results.append(result) results = multiunion(results) # OR if results is None: results = IISet() self.data = results
def update(self): self.catalog = dewey.get_catalog() self.nresources = len(self.catalog.rids) self.collection = None self.ncollection = None self.set_prompt() # Pre-compute Resource attribute listing. # ======================================= fields = self.catalog.Resource.__dict__.keys() self.fields = [f for f in fields if not f.startswith('_')] self.fields.sort() fields = [] for field in self.fields: if not field.startswith('_'): fields.append(' '+field) self.fields_listing = os.linesep.join(fields) # Pre-compute indices listing # =========================== indices = [] longest = 0 for name, index in self.catalog.indices.iteritems(): len_name = len(name) longest = (len_name > longest) and len_name or longest indices.append((name, getattr(index, '__name__', repr(index)))) indices.sort() listing = [] for name, index_type in indices: listing.append(' %s %s' % (name.ljust(longest), index_type)) self.indices_listing = os.linesep.join(listing) self.indices = [i[0] for i in indices]