def generate_from_views(cls, views): # id -> instance instances = dict() def get_instance(name): existing = instances.get(name, None) if not existing: doc_id = u"_design/%s" % (name, ) existing = cls(doc_id=doc_id) instances[name] = existing return existing for view in views: view = IViewFactory(view) instance = get_instance(view.design_doc_id) entry = dict() if hasattr(view, 'map'): entry['map'] = view.get_code('map') if view.use_reduce: entry['reduce'] = view.get_code('reduce') instance.views[view.name] = entry if hasattr(view, 'filter'): instance.filters[view.name] = unicode(view.filter.source) return instances.values()
def cancel_listener(self, filter_): for l_id, listener_filter in self._listeners.items(): if ((IViewFactory.providedBy(listener_filter) and filter_ is listener_filter) or (isinstance(listener_filter, (list, tuple)) and (filter_ in listener_filter))): self._cancel_listener(l_id)
def listen_changes(self, filter_, callback, kwargs=dict()): assert callable(callback), ("Callback should be callable, got %r" % (callback), ) l_id = str(uuid.uuid1()) if isinstance(filter_, (list, tuple, )): doc_ids = list(filter_) filter_i = self._filters['doc_ids'] filter_i.add_listener(callback, l_id, doc_ids) self.log("Registering listener for doc_ids: %r, callback %r", doc_ids, callback) elif IViewFactory.providedBy(filter_): filter_i = ViewFilter(filter_, kwargs) if filter_i.name in self._filters: filter_i = self._filters[filter_i.name] self._filters[filter_i.name] = filter_i filter_i.add_listener(callback, l_id) else: raise AttributeError("Not suported filter. You should pass a list" " of document ids or a IViewFactory object " "passed: %r" % (filter_)) d = self._setup_notifier(filter_i) d.addCallback(defer.override_result, l_id) return d
def generate_from_views(cls, views): # id -> instance instances = dict() def get_instance(view): name = view.design_doc_id if name is None: raise ValueError("%r.design_doc_id is None" % (view,)) existing = instances.get(name, None) if not existing: doc_id = u"_design/%s" % (name,) existing = cls(doc_id=doc_id, language=view.language) instances[name] = existing elif existing.language != view.language: raise ValueError( "Language mismatch! Design document %s " "has language: %s, the view %s has language: " " %s" % (doc_id, existing.language, name, view.language) ) return existing for view in views: view = IViewFactory(view) if view.name is None: raise ValueError("%r.name is None!" % (view,)) instance = get_instance(view) entry = dict() if hasattr(view, "map"): entry["map"] = view.get_code("map") if view.use_reduce: entry["reduce"] = view.get_code("reduce") instance.views[view.name] = entry if hasattr(view, "filter"): instance.filters[view.name] = view.get_code("filter") if view.disable_reduce_limit: instance.disable_reduce_limit = True return instances.values()
def get_changes(self, filter_=None, limit=None, since=0): if IViewFactory.providedBy(filter_): filter_ = ViewFilter(filter_, params=dict()) elif filter_ is not None: raise ValueError("%r should provide IViewFacory" % (filter_, )) return self._database.get_changes(filter_, limit, since)