def _update_local(fname, qparams, replacement): """Update a document created using the local framework Parameters ----------- fname: str Name of the query should be run qparams: dict Query parameters. Similar to online query methods replacement: dict Fields/value pair to be updated. Beware of disallowed fields such as time and uid """ try: with open(fname, 'r') as fp: local_payload = ujson.load(fp) qobj = mongoquery.Query(qparams) for _sample in local_payload: try: if qobj.match(_sample): for k, v in replacement.items(): _sample[k] = v except mongoquery.QueryError: pass with open(fname, 'w') as fp: ujson.dump(local_payload, fp) except FileNotFoundError: raise RuntimeWarning('Local file {} does not exist'.format(fname))
def _find_local(fname, qparams, as_doct=False): """Find a document created using the local framework Parameters ----------- fname: str Name of the query should be run qparams: dict Query parameters. Similar to online query methods Yields ------------ c: doct.Document, StopIteration Result of the query if found """ res_list = [] try: with open(fname, 'r') as fp: local_payload = ujson.load(fp) qobj = mongoquery.Query(qparams) for i in local_payload: if qobj.match(i): res_list.append(i) except FileNotFoundError: raise RuntimeWarning('Local file {} does not exist'.format(fname)) if as_doct: for c in res_list: yield Document(fname.split('.')[0], c) else: for c in res_list: yield c
def emit_to_relevant_sockets(request_data, document): # skip if there are obviously no listeners if not request_data['collection'] in live_sockets\ or len(live_sockets[request_data['collection']]) < 1: return True # for each listener in this collection for socket in live_sockets[request_data['collection']]: # remove disconnected sockets if not socket.connected: live_sockets[request_data['collection']].remove(socket) continue query = { "$and": [{ "$or": [{ "sender": { "$in": socket.ids } }, { "recipient": { "$in": socket.ids } }] }] } if socket.where: query["$and"].append(socket.where) # if the listener is authenticated as the sender or the recipient print(query) print(document) if mongoquery.Query(query).match(document): # document matches this users specifications. Send document print("emitting") socket.emit("data", document)
def filter_data(conf, data): if not conf.filter: return data if mongoquery: q = mongoquery.Query(conf.filter) return filter(q.match, data) print("Can't import monoquery, using a subset of the query language") return [obj for obj in data if match(conf.filter, obj)]
def __init__(self, app, pipeline, query={}, id=None, config=None): super().__init__(app, pipeline, id=id, config=config) # Check if the query is correctly implemented try: self.Query = mongoquery.Query(query) self.Query.match({}) except mongoquery.QueryError: L.warning("Incorrect query") raise
def __init__(self, app, pipeline, query=True, analyze_on_clock=False, inclusive=False, id=None, config=None): super().__init__(app, pipeline, analyze_on_clock=analyze_on_clock, id=id, config=config) max_size = int(self.Config.get('latch_max_size')) if max_size == 0: self.Latch = collections.deque() else: self.Latch = collections.deque(maxlen=max_size) # Check if the query is correctly implemented if isinstance(query, bool): self.Query = query else: try: self.Query = mongoquery.Query(query) self.Query.match({}) except mongoquery.QueryError: L.warning("Incorrect query") raise self.Inclusive = inclusive