def wrapper(*args, config, **kwargs): url = config.get('url', None) if url is None: print('Please use abcd login first!') exit(1) db = ABCD.from_url(url=url) # TODO: AST.from_string() ?! # TODO: parser should accept list # TODO: better ast optimisation query_list = [] for q in kwargs.pop('default_query', []): query_list.append(parser(q)) for q in kwargs.pop('query', []): query_list.append(parser(q)) if not query_list: query = None elif len(query_list) == 1: query = query_list[0] else: query = ('AND', *query_list) func(*args, db=db, query=query, **kwargs)
def rename_property(self, name, new_name, query=None): logger.info('rename: query={}, old={}, new={}'.format( query, name, new_name)) # TODO name in derived.info_keys OR name in derived.arrays_keys OR name in derived.derived_keys self.collection.update_many(parser(query), {'$push': { 'derived.info_keys': new_name }}) self.collection.update_many(parser(query), { '$pull': { 'derived.info_keys': name }, '$rename': { name: new_name } })
def count(self, query=None): query = parser(query) logger.info('query; {}'.format(query)) if not query: query = {} return self.db.atoms.count_documents(query)
def __call__(self, ast): logger.info('parsed ast: {}'.format(ast)) if isinstance(ast, dict): return ast elif isinstance(ast, str): from abcd.parsers.queries import parser p = parser(ast) return self.visit(p) return self.visit(ast) if ast else {}
def add_property(self, data, query=None): logger.info('add: data={}, query={}'.format(data, query)) self.collection.update_many( parser(query), { '$push': { 'derived.info_keys': { '$each': list(data.keys()) } }, '$set': data })
def delete_property(self, name, query=None): logger.info('delete: query={}, porperty={}'.format(name, query)) self.collection.update_many( parser(query), { '$pull': { 'derived.info_keys': name, 'derived.arrays_keys': name }, '$unset': { name: '' } })
def properties(self, query=None): query = parser(query) properties = {} pipeline = [{ '$match': query }, { '$unwind': '$derived.info_keys' }, { '$group': { '_id': '$derived.info_keys' } }] properties['info'] = [ value['_id'] for value in self.db.atoms.aggregate(pipeline) ] pipeline = [{ '$match': query }, { '$unwind': '$derived.arrays_keys' }, { '$group': { '_id': '$derived.arrays_keys' } }] properties['arrays'] = [ value['_id'] for value in self.db.atoms.aggregate(pipeline) ] pipeline = [{ '$match': query }, { '$unwind': '$derived.derived_keys' }, { '$group': { '_id': '$derived.derived_keys' } }] properties['derived'] = [ value['_id'] for value in self.db.atoms.aggregate(pipeline) ] return properties
def property(self, name, query=None): query = parser(query) pipeline = [{ '$match': query }, { '$match': { '{}'.format(name): { "$exists": True } } }, { '$project': { '_id': False, 'data': '${}'.format(name) } }] return [val['data'] for val in self.db.atoms.aggregate(pipeline)]
def count_properties(self, query=None): query = parser(query) properties = {} pipeline = [{ '$match': query }, { '$unwind': '$derived.info_keys' }, { '$group': { '_id': '$derived.info_keys', 'count': { '$sum': 1 } } }] info_keys = self.db.atoms.aggregate(pipeline) for val in info_keys: properties[val['_id']] = { 'count': val['count'], 'category': 'info', 'dtype': self.get_type_of_property(val['_id'], 'info') } pipeline = [{ '$match': query }, { '$unwind': '$derived.arrays_keys' }, { '$group': { '_id': '$derived.arrays_keys', 'count': { '$sum': 1 } } }] arrays_keys = list(self.db.atoms.aggregate(pipeline)) for val in arrays_keys: properties[val['_id']] = { 'count': val['count'], 'category': 'arrays', 'dtype': self.get_type_of_property(val['_id'], 'arrays') } pipeline = [{ '$match': query }, { '$unwind': '$derived.derived_keys' }, { '$group': { '_id': '$derived.derived_keys', 'count': { '$sum': 1 } } }] arrays_keys = list(self.db.atoms.aggregate(pipeline)) for val in arrays_keys: properties[val['_id']] = { 'count': val['count'], 'category': 'derived', 'dtype': self.get_type_of_property(val['_id'], 'derived') } return properties
def get_atoms(self, query=None): query = parser(query) for dct in self.db.atoms.find(query): yield AtomsModel(None, dct).to_ase()
def get_items(self, query=None): # TODO: better method for aggregations query = parser(query) for dct in self.db.atoms.find(query): yield dct
def delete(self, query=None): query = parser(query) return self.collection.delete_many(query)
def wrapper(*args, query=None, **kwargs): print(func) print((args, query, kwargs)) query = parser(query) func(*args, **kwargs, query=query)