def test_create_collection(self): db = Database(self.client, "pymongo_test") db.test.insert({"hello": "world"}) self.assertRaises(CollectionInvalid, db.create_collection, "test") db.drop_collection("test") self.assertRaises(TypeError, db.create_collection, 5) self.assertRaises(TypeError, db.create_collection, None) self.assertRaises(InvalidName, db.create_collection, "coll..ection") test = db.create_collection("test") test.save({"hello": u"world"}) self.assertEqual(db.test.find_one()["hello"], "world") self.assertTrue(u"test" in db.collection_names()) db.drop_collection("test.foo") db.create_collection("test.foo") self.assertTrue(u"test.foo" in db.collection_names()) expected = {} if version.at_least(self.client, (2, 7, 0)): # usePowerOf2Sizes server default expected["flags"] = 1 result = db.test.foo.options() # mongos 2.2.x adds an $auth field when auth is enabled. result.pop('$auth', None) self.assertEqual(result, expected) self.assertRaises(CollectionInvalid, db.create_collection, "test.foo")
def test_create_collection(self): db = Database(self.client, "pymongo_test") db.test.insert({"hello": "world"}) self.assertRaises(CollectionInvalid, db.create_collection, "test") db.drop_collection("test") self.assertRaises(TypeError, db.create_collection, 5) self.assertRaises(TypeError, db.create_collection, None) self.assertRaises(InvalidName, db.create_collection, "coll..ection") test = db.create_collection("test") test.save({"hello": u"world"}) self.assertEqual(db.test.find_one()["hello"], "world") self.assertTrue(u"test" in db.collection_names()) db.drop_collection("test.foo") db.create_collection("test.foo") self.assertTrue(u"test.foo" in db.collection_names()) expected = {} if version.at_least(self.client, (2, 7, 0)): # usePowerOf2Sizes server default expected["flags"] = 1 result = db.test.foo.options() # mongos 2.2.x adds an $auth field when auth is enabled. result.pop("$auth", None) self.assertEqual(result, expected) self.assertRaises(CollectionInvalid, db.create_collection, "test.foo")
def test_drop_collection(self): db = Database(self.connection, "pymongo_test") self.assertRaises(TypeError, db.drop_collection, 5) self.assertRaises(TypeError, db.drop_collection, None) db.test.save({"dummy": u"object"}) self.assertTrue("test" in db.collection_names()) db.drop_collection("test") self.assertFalse("test" in db.collection_names()) db.test.save({"dummy": u"object"}) self.assertTrue("test" in db.collection_names()) db.drop_collection(u"test") self.assertFalse("test" in db.collection_names()) db.test.save({"dummy": u"object"}) self.assertTrue("test" in db.collection_names()) db.drop_collection(db.test) self.assertFalse("test" in db.collection_names()) db.test.save({"dummy": u"object"}) self.assertTrue("test" in db.collection_names()) db.test.drop() self.assertFalse("test" in db.collection_names()) db.test.drop() db.drop_collection(db.test.doesnotexist)
def test_drop_collection(self): db = Database(self.client, "pymongo_test") self.assertRaises(TypeError, db.drop_collection, 5) self.assertRaises(TypeError, db.drop_collection, None) db.test.insert_one({"dummy": u("object")}) self.assertTrue("test" in db.collection_names()) db.drop_collection("test") self.assertFalse("test" in db.collection_names()) db.test.insert_one({"dummy": u("object")}) self.assertTrue("test" in db.collection_names()) db.drop_collection(u("test")) self.assertFalse("test" in db.collection_names()) db.test.insert_one({"dummy": u("object")}) self.assertTrue("test" in db.collection_names()) db.drop_collection(db.test) self.assertFalse("test" in db.collection_names()) db.test.insert_one({"dummy": u("object")}) self.assertTrue("test" in db.collection_names()) db.test.drop() self.assertFalse("test" in db.collection_names()) db.test.drop() db.drop_collection(db.test.doesnotexist)
def test_drop_collection(self): db = Database(self.client, "pymongo_test") self.assertRaises(TypeError, db.drop_collection, 5) self.assertRaises(TypeError, db.drop_collection, None) db.test.insert_one({"dummy": u"object"}) self.assertTrue("test" in db.collection_names()) db.drop_collection("test") self.assertFalse("test" in db.collection_names()) db.test.insert_one({"dummy": u"object"}) self.assertTrue("test" in db.collection_names()) db.drop_collection(u"test") self.assertFalse("test" in db.collection_names()) db.test.insert_one({"dummy": u"object"}) self.assertTrue("test" in db.collection_names()) db.drop_collection(db.test) self.assertFalse("test" in db.collection_names()) db.test.insert_one({"dummy": u"object"}) self.assertTrue("test" in db.collection_names()) db.test.drop() self.assertFalse("test" in db.collection_names()) db.test.drop() db.drop_collection(db.test.doesnotexist) if client_context.version.at_least(3, 3, 9) and client_context.is_rs: db_wc = Database(self.client, 'pymongo_test', write_concern=IMPOSSIBLE_WRITE_CONCERN) with self.assertRaises(WriteConcernError): db_wc.drop_collection('test')
def test_collection_names(self): db = Database(self.client, "pymongo_test") db.test.save({"dummy": u"object"}) db.test.mike.save({"dummy": u"object"}) colls = db.collection_names() self.assertTrue("test" in colls) self.assertTrue("test.mike" in colls) for coll in colls: self.assertTrue("$" not in coll) colls_without_systems = db.collection_names(False) for coll in colls_without_systems: self.assertTrue(not coll.startswith("system."))
def test_drop_collection(self): db = Database(self.connection, "pymongo_test") self.assertRaises(TypeError, db.drop_collection, 5) self.assertRaises(TypeError, db.drop_collection, None) db.test.save({"dummy": u"object"}) self.assert_("test" in db.collection_names()) db.drop_collection("test") self.assertFalse("test" in db.collection_names()) db.test.save({"dummy": u"object"}) self.assert_("test" in db.collection_names()) db.drop_collection(u"test") self.assertFalse("test" in db.collection_names()) db.test.save({"dummy": u"object"}) self.assert_("test" in db.collection_names()) db.drop_collection(db.test) self.assertFalse("test" in db.collection_names()) db.test.save({"dummy": u"object"}) self.assert_("test" in db.collection_names()) db.test.drop() self.assertFalse("test" in db.collection_names()) db.test.drop() db.drop_collection(db.test.doesnotexist)
def test_collection_names(self): db = Database(self.connection, "pymongo_test") db.test.save({"dummy": u"object"}) db.test.mike.save({"dummy": u"object"}) colls = db.collection_names() self.assertTrue("test" in colls) self.assertTrue("test.mike" in colls) for coll in colls: self.assertTrue("$" not in coll)
def getSince(db: database.Database, sinceCollName: str) -> datetime.datetime: # Get the default 'since' value. retval = os.environ["Since"] or '2019-12-01' # Check Cosmos for an existing value from a previous function run. if (sinceCollName in db.collection_names()): result = db[sinceCollName].find({"_id": 0}) if (result.count() > 0): retval = result[0]['since'] return datetime.datetime.strptime(retval, '%Y-%m-%d %H:%M:%S')
def test_collection_names(self): db = Database(self.connection, "pymongo_test") db.test.save({"dummy": u"object"}) db.test.mike.save({"dummy": u"object"}) colls = db.collection_names() self.assert_("test" in colls) self.assert_("test.mike" in colls) for coll in colls: self.assert_("$" not in coll)
def test_create_collection(self): db = Database(self.client, "pymongo_test") db.test.insert_one({"hello": "world"}) self.assertRaises(CollectionInvalid, db.create_collection, "test") db.drop_collection("test") self.assertRaises(TypeError, db.create_collection, 5) self.assertRaises(TypeError, db.create_collection, None) self.assertRaises(InvalidName, db.create_collection, "coll..ection") test = db.create_collection("test") self.assertTrue(u"test" in db.collection_names()) test.insert_one({"hello": u"world"}) self.assertEqual(db.test.find_one()["hello"], "world") db.drop_collection("test.foo") db.create_collection("test.foo") self.assertTrue(u"test.foo" in db.collection_names()) self.assertRaises(CollectionInvalid, db.create_collection, "test.foo")
def test_create_collection(self): db = Database(self.client, "pymongo_test") db.test.insert_one({"hello": "world"}) self.assertRaises(CollectionInvalid, db.create_collection, "test") db.drop_collection("test") self.assertRaises(TypeError, db.create_collection, 5) self.assertRaises(TypeError, db.create_collection, None) self.assertRaises(InvalidName, db.create_collection, "coll..ection") test = db.create_collection("test") self.assertTrue(u("test") in db.collection_names()) test.insert_one({"hello": u("world")}) self.assertEqual(db.test.find_one()["hello"], "world") db.drop_collection("test.foo") db.create_collection("test.foo") self.assertTrue(u("test.foo") in db.collection_names()) self.assertRaises(CollectionInvalid, db.create_collection, "test.foo")
def test_create_collection(self): db = Database(self.connection, "pymongo_test") db.test.insert({"hello": "world"}) self.assertRaises(CollectionInvalid, db.create_collection, "test") db.drop_collection("test") self.assertRaises(TypeError, db.create_collection, 5) self.assertRaises(TypeError, db.create_collection, None) self.assertRaises(InvalidName, db.create_collection, "coll..ection") test = db.create_collection("test") test.save({"hello": u"world"}) self.assertEqual(db.test.find_one()["hello"], "world") self.assert_(u"test" in db.collection_names()) db.drop_collection("test.foo") db.create_collection("test.foo") self.assert_(u"test.foo" in db.collection_names()) self.assertEqual(db.test.foo.options(), {}) self.assertRaises(CollectionInvalid, db.create_collection, "test.foo")
def test_collection_names(self): db = Database(self.client, "pymongo_test") db.test.insert_one({"dummy": u"object"}) db.test.mike.insert_one({"dummy": u"object"}) colls = db.collection_names() self.assertTrue("test" in colls) self.assertTrue("test.mike" in colls) for coll in colls: self.assertTrue("$" not in coll) colls_without_systems = db.collection_names(False) for coll in colls_without_systems: self.assertTrue(not coll.startswith("system.")) # Force more than one batch. db = self.client.many_collections for i in range(101): db["coll" + str(i)].insert_one({}) # No Error try: db.collection_names() finally: self.client.drop_database("many_collections")
def test_collection_names(self): db = Database(self.client, "pymongo_test") db.test.insert_one({"dummy": u("object")}) db.test.mike.insert_one({"dummy": u("object")}) colls = db.collection_names() self.assertTrue("test" in colls) self.assertTrue("test.mike" in colls) for coll in colls: self.assertTrue("$" not in coll) colls_without_systems = db.collection_names(False) for coll in colls_without_systems: self.assertTrue(not coll.startswith("system.")) # Force more than one batch. db = self.client.many_collections for i in range(101): db["coll" + str(i)].insert_one({}) # No Error try: db.collection_names() finally: self.client.drop_database("many_collections")
def borrarindicesevitardups(): # Conexion a local client = MongoClient() try: # The ismaster command is cheap and does not require auth. client.admin.command('ismaster') db = Database(client=client, name=ConfiguracionGlobal.MONGODB) except ConnectionFailure: print("Server not available") # Limpiado tablas e indices con pymongo for collname in db.collection_names(): coll = Collection(db, name=collname) coll.drop_indexes() client.close()
def conn(self, line): """Conenct to mongo database in ipython shell. Examples:: %mongo_connect %mongo_connect mongodb://hostname:port """ if line: uri = line else: uri = 'mongodb://127.0.0.1:27017' self._conn = MongoClient(uri) # add db and collection property to object for autocomplete. for db in self._conn.database_names(): setattr(self._conn, db, Database(self._conn, db)) _db = Database(self._conn, db) for collection in _db.collection_names(): # [TODO] change eval to other!! setattr(eval('self._conn.'+db), collection, Collection(_db, collection)) return self._conn
def conn(self, line): """Conenct to mongo database in ipython shell. Examples:: %mongo_connect %mongo_connect mongodb://hostname:port """ if line: uri = line else: uri = 'mongodb://127.0.0.1:27017' self._conn = MongoClient(uri) # add db and collection property to object for autocomplete. for db in self._conn.database_names(): setattr(self._conn, db, Database(self._conn, db)) _db = Database(self._conn, db) for collection in _db.collection_names(): # [TODO] change eval to other!! setattr(eval('self._conn.' + db), collection, Collection(_db, collection)) return self._conn
def load_feature_vectors_and_classes(db_name): """ :param db_name: name of database to use :return: - list of feature vectors - dictionary where the keys are the class labels and the values are dictionaries of the form {start: <integer>, end: <integer>} """ print 'Loading feature vectors and target classes...' db = Database(MongoClient(), db_name) collection_names = db.collection_names() if not ('naive_bayes' in collection_names and 'feature_vectors' in collection_names): print 'Database missing collections needed to train classifier on.' exit() target_classes = Collection(db, 'naive_bayes').find_one({'type': 'classes'}) if 'classes' not in target_classes: raise KeyError("'target_classes' must contain a 'classes' key.") feature_vectors = list(Collection(db, 'feature_vectors').find()) return feature_vectors, target_classes['classes']
def show_cols(self, db): db = Database(self, db) return db.collection_names()
class Store(object): def __init__(self, conn_string, database, trace=False): if not conn_string.startswith('mongodb://'): conn_string = 'mongodb://' + conn_string self.connection = Connection(conn_string) self.database = Database(self.connection, database) self.trace = trace self.collections = [] self.obj_infos = set() self._cache = {} def _load(self, cls_info, operation, *args, **kwargs): fields = kwargs.pop('fields', None) if not fields: fields = cls_info.attributes.keys() return operation(*args, fields=fields, **kwargs) def _build_doc(self, cls_info, doc): obj_id = doc["_id"] obj = self._cache.get((cls_info, obj_id)) if obj is not None: return obj cls = cls_info.cls obj = cls.__new__(cls) obj_info = get_obj_info(obj) obj_info.set("store", self) for attr, value in doc.items(): if attr == '_id': continue else: field = cls_info.attributes[attr] obj_info.variables[field] = value obj._id = obj_id self._cache[(cls_info, obj_id)] = obj func = getattr(obj, '__thunder_loaded__', None) if func: func() return obj def _encode(self, obj_info): cls_info = obj_info.cls_info doc = {} for attr, field in cls_info.attributes.items(): value = obj_info.variables.get(field, field.default) doc[attr] = value return doc def _flush_one(self, obj_info): cls_info = obj_info.cls_info collection = cls_info.get_collection(self) mongo_doc = self._encode(obj_info) action = obj_info.get('action') obj = obj_info.obj func = getattr(obj, '__thunder_pre_flush__', None) if func: func() if action == 'remove': collection.remove(mongo_doc) obj_info.delete("store") else: collection.save(mongo_doc) obj = obj_info.obj obj_id = mongo_doc['_id'] obj._id = obj_id self._cache[(cls_info, obj_id)] = obj func = getattr(obj, '__thunder_flushed__', None) if func: func() def get(self, cls, obj_id): cls_info = get_cls_info(cls) collection = cls_info.get_collection(self) obj = self._cache.get((cls_info, obj_id)) if obj is not None: return obj cursor = self._load(cls_info, collection.find, {'_id': obj_id}, limit=2) if cursor.count(): return self._build_doc(cls_info, cursor[0]) def find(self, cls, *args, **kwargs): cls_info = get_cls_info(cls) collection = cls_info.get_collection(self) cursor = self._load(cls_info, collection.find, *args, **kwargs) for item in cursor: yield self._build_doc(cls_info, item) def find_one(self, cls, *args, **kwargs): cls_info = get_cls_info(cls) collection = cls_info.get_collection(self) item = self._load(cls_info, collection.find_one, *args, **kwargs) if item is not None: return self._build_doc(cls_info, item) def find_by(self, cls, **kwargs): cls_info = get_cls_info(cls) collection = cls_info.get_collection(self) cursor = self._load(cls_info, collection.find, kwargs) for item in cursor: yield self._build_doc(cls_info, item) def find_one_by(self, cls, **kwargs): cls_info = get_cls_info(cls) collection = cls_info.get_collection(self) item = self._load(cls_info, collection.find_one, **kwargs) if item is not None: return self._build_doc(cls_info, item) def count(self, cls): cls_info = get_cls_info(cls) collection = cls_info.get_collection(self) return collection.find().count() def add(self, obj): obj_info = get_obj_info(obj) if obj_info.get('store') is not None: # pragma: nocoverage raise TypeError( "Document %s is already in a store" % (obj, )) obj_info.set('store', self) obj_info.flush_pending = True self.obj_infos.add(obj_info) def remove(self, obj): obj_info = get_obj_info(obj) store = obj_info.get('store') if store != self: raise Exception("This object does not belong to this store") obj_info.set('action', 'remove') obj_info.flush_pending = True self.obj_infos.add(obj_info) del self._cache[(obj_info.cls_info, obj._id)] def flush(self): for obj_info in self.obj_infos: # FIXME: Use obj_info.flush_pending self._flush_one(obj_info) def drop_cache(self): self._cache = {} def drop_collection(self, cls): cls_info = get_cls_info(cls) collection = cls_info.get_collection(self) collection.drop() def drop_collections(self): for name in self.database.collection_names(): if name.startswith('system'): continue collection = self.database[name] collection.drop()
class Store(object): def __init__(self, conn_string, database, trace=False): if not conn_string.startswith('mongodb://'): conn_string = 'mongodb://' + conn_string self.connection = Connection(conn_string) self.database = Database(self.connection, database) self.trace = trace self.collections = [] self.obj_infos = set() self._cache = {} def _load(self, cls_info, operation, *args, **kwargs): fields = kwargs.pop('fields', None) if not fields: fields = cls_info.attributes.keys() return operation(*args, fields=fields, **kwargs) def _build_doc(self, cls_info, doc): obj_id = doc["_id"] obj = self._cache.get((cls_info, obj_id)) if obj is not None: return obj cls = cls_info.cls obj = cls.__new__(cls) obj_info = get_obj_info(obj) obj_info.set("store", self) for attr, value in doc.items(): if attr == '_id': continue else: field = cls_info.attributes[attr] obj_info.variables[field] = value obj._id = obj_id self._cache[(cls_info, obj_id)] = obj func = getattr(obj, '__thunder_loaded__', None) if func: func() return obj def _encode(self, obj_info): cls_info = obj_info.cls_info doc = {} for attr, field in cls_info.attributes.items(): value = obj_info.variables.get(field, field.default) doc[attr] = value return doc def _flush_one(self, obj_info): cls_info = obj_info.cls_info collection = cls_info.get_collection(self) mongo_doc = self._encode(obj_info) action = obj_info.get('action') obj = obj_info.obj func = getattr(obj, '__thunder_pre_flush__', None) if func: func() if action == 'remove': collection.remove(mongo_doc) obj_info.delete("store") else: collection.save(mongo_doc) obj = obj_info.obj obj_id = mongo_doc['_id'] obj._id = obj_id self._cache[(cls_info, obj_id)] = obj func = getattr(obj, '__thunder_flushed__', None) if func: func() def get(self, cls, obj_id): cls_info = get_cls_info(cls) collection = cls_info.get_collection(self) obj = self._cache.get((cls_info, obj_id)) if obj is not None: return obj cursor = self._load(cls_info, collection.find, {'_id': obj_id}, limit=2) if cursor.count(): return self._build_doc(cls_info, cursor[0]) def find(self, cls, *args, **kwargs): cls_info = get_cls_info(cls) collection = cls_info.get_collection(self) cursor = self._load(cls_info, collection.find, *args, **kwargs) for item in cursor: yield self._build_doc(cls_info, item) def find_one(self, cls, *args, **kwargs): cls_info = get_cls_info(cls) collection = cls_info.get_collection(self) item = self._load(cls_info, collection.find_one, *args, **kwargs) if item is not None: return self._build_doc(cls_info, item) def find_by(self, cls, **kwargs): cls_info = get_cls_info(cls) collection = cls_info.get_collection(self) cursor = self._load(cls_info, collection.find, kwargs) for item in cursor: yield self._build_doc(cls_info, item) def find_one_by(self, cls, **kwargs): cls_info = get_cls_info(cls) collection = cls_info.get_collection(self) item = self._load(cls_info, collection.find_one, **kwargs) if item is not None: return self._build_doc(cls_info, item) def count(self, cls): cls_info = get_cls_info(cls) collection = cls_info.get_collection(self) return collection.find().count() def add(self, obj): obj_info = get_obj_info(obj) if obj_info.get('store') is not None: # pragma: nocoverage raise TypeError("Document %s is already in a store" % (obj, )) obj_info.set('store', self) obj_info.flush_pending = True self.obj_infos.add(obj_info) def remove(self, obj): obj_info = get_obj_info(obj) store = obj_info.get('store') if store != self: raise Exception("This object does not belong to this store") obj_info.set('action', 'remove') obj_info.flush_pending = True self.obj_infos.add(obj_info) del self._cache[(obj_info.cls_info, obj._id)] def flush(self): for obj_info in self.obj_infos: # FIXME: Use obj_info.flush_pending self._flush_one(obj_info) def drop_cache(self): self._cache = {} def drop_collection(self, cls): cls_info = get_cls_info(cls) collection = cls_info.get_collection(self) collection.drop() def drop_collections(self): for name in self.database.collection_names(): if name.startswith('system'): continue collection = self.database[name] collection.drop()
class Conexao: def __init__(self, usuario, senha, nome_banco=None, nome_colecao=None): self.__conexao = None self.__banco = None self.__colecao = None self.__SERVIDOR = 'infogocd' self.__PORTA = 27017 self.__abrir_conexao(usuario, senha) if nome_banco: self.acessar_banco(nome_banco) if nome_colecao: self.acessar_colecao(nome_colecao) else: Log.imprime("COLECAO NAO INFORMADA.", "ERRO NA CONEXAO COM O MONGO.", classe=Conexao) else: Log.imprime("BANCO NAO INFORMADO.", "ERRO NA CONEXAO COM O MONGO.", classe=Conexao) def __abrir_conexao(self, usuario, senha): self.__conexao = MongoClient(self.__SERVIDOR, self.__PORTA) if not usuario or not senha: Log.imprime("USUARIO/SENHA NAO INFORMADO.", "ERRO NA CONEXAO COM O MONGO.", classe=Conexao) exit(1) if self.__conexao: autenticado = self.__conexao.admin.authenticate( usuario, senha, mechanism='SCRAM-SHA-1') if autenticado: Log.imprime("CONEXAO ABERTA COM SUCESSO") return True else: Log.imprime( "USUARIO NAO PODE SER AUTENTICADO. CHECAR SENHA NO KEEPASS. VALOR RECEBIDO PARA USUARIO: " + str(usuario), "ERRO DE AUTENTICACAO NO MONGO.", classe=Conexao) exit(1) else: Log.imprime( "CONEXAO PARECE NAO TER SIDO INICIADA DA MANEIRA CORRETA. VALOR RECEBIDO: " + str(self.__conexao), "ERRO NA CONEXAO COM O MONGO.", classe=Conexao) exit(2) def acessar_banco(self, nome_banco): if not nome_banco: Log.imprime("O NOME DO BANCO NAO FOI INFORMADO.", "ERRO NA CONEXAO COM O MONGO.", classe=Conexao) exit(1) if not self.__conexao: Log.imprime( "CONEXAO PARECE NAO TER SIDO INICIADA DA MANEIRA CORRETA. VALOR RECEBIDO: " + str(self.__conexao), "ERRO NA CONEXAO COM O MONGO.", classe=Conexao) exit(2) if nome_banco not in self.__conexao.database_names(): Log.imprime("BANCO NAO EXISTE NO MONGODB. VALOR RECEBIDO: " + str(self.__conexao) + "\nLISTA DE BANCOS ENCONTRADOS: " + str(self.__conexao.database_names()), "ERRO NA CONEXAO COM O MONGO.", classe=Conexao) exit(3) self.__banco = Database(self.__conexao, nome_banco) Log.imprime("CONECTADO AO BANCO " + nome_banco) return True def acessar_colecao(self, nome_colecao): if not nome_colecao: Log.imprime("COLECAO NAO INFORMADA.", "ERRO NA CONEXAO COM O MONGO.", classe=Conexao) exit(1) if not self.__banco: Log.imprime("BANCO NAO INFORMADO.", "ERRO NA CONEXAO COM O MONGO.", classe=Conexao) exit(2) if nome_colecao not in self.__banco.collection_names(): Log.imprime( "COLECAO NAO EXISTE NO BANCO.\nVALOR RECEBIDO PARA BANCO: " + str(self.__banco) + "\nVALOR RECEBIDO PARA COLECAO: " + str(nome_colecao) + "\nLISTA DE COLECOES DO BANCO: " + str(self.__banco.collection_names()), "ERRO NA CONEXAO COM O MONGO.", classe=Conexao) exit(3) self.__colecao = Collection(self.__banco, nome_colecao) Log.imprime("CONECTADO A COLECAO " + nome_colecao) return True def fechar_conexao(self): if not self.__conexao: Log.imprime( "CONEXAO PARECE NAO TER SIDO INICIADA DA MANEIRA CORRETA.\nVALOR RECEBIDO: " + str(self.__conexao), "ERRO NA CONEXAO COM O MONGO.", classe=Conexao) return False else: self.__conexao.close() Log.imprime("CONEXAO ENCERRADA COM SUCESSO") return True def obter_colecao(self): return self.__colecao
def show_cols( self, db ): db = Database( self, db ) return db.collection_names()
class ConfigDB: def __init__(self): db_server_ip = conf("config.db_server_ip") db_server_port = conf("config.db_server_port") self.__connection = Connection(host=db_server_ip, port=db_server_port) db_name = conf("config.db_name") self.__db = Database(connection= self.__connection, name=db_name) # Returns a tuple with paired values to be used in drop down lists. def get_tuple(self, collection, text, value="_id", where={}): l = [] for i in list(self.__db[collection].find(spec=where)): l += [(i[value], i[text])] return tuple(l) # Moves unique records from source collection to destination, order parameter still pending. def move_unique(self, source, destination): map_func = Code( "function () {" " emit(this._id, 1);" "}") reduce_func = Code( "function (key, values) {" " return key;" "}") self.__db[destination].drop() self.__db[source].map_reduce(map=map_func, reduce=reduce_func, out=destination) # Return collection with the specified name and filter. def get_collection(self, collection, where={}): return self.__db[collection].find(spec=where) # Returns the first document that matches the where clause within the specified collection. def get_document(self, collection, where={}): return self.__db[collection].find_one(spec_or_id=where) # Removes the specified document. def dispose_document(self, collection, where): return self.__db[collection].remove(spec_or_id=where) # Updates the document with the specified id, the document is inserted if the _id is not found. # Returns the updated document. def set_document(self, collection, _id, values): if _id is None: # If no _id to update, insert it. _id = self.__db[collection].insert(values) else: # If _id to update, update it. self.__db[collection].update( spec={"_id": _id}, document={"$set": values}, upsert=True, new=True) # Return modified document. return self.__db[collection].find_one({"_id": _id}) # Removes the specified collection from the database. def dispose_collection(self, collection): self.__db[collection].drop() # Insert a bulk of documents into a collection. def bulk_import(self, collection, doc_or_docs): self.__db[collection].insert(doc_or_docs) # Return the count of documents for a specific collection. def count_documents(self, collection, where={}): cur = self.__db[collection].find(where) return cur.count() def exists_collection(self, name): return name in self.__db.collection_names() def get_categories(self): return self.get_tuple( collection="categories", text="name" ) def get_products(self): return self.get_tuple( collection="products", text="name" ) def get_priorities(self): return self.get_tuple( collection="priorities", text="label" ) def get_users(self, role): return self.get_tuple( collection="users", text="user_name", where={"role": role} ) def get_destinations(self): return self.get_tuple( collection="destinations", text="dial" ) def get_whitelists(self): return self.get_tuple( collection="lists", text="name", where={"type": "white"} ) def get_blacklists(self): return self.get_tuple( collection="lists", text="name", where={"type": "black"} )
class DataDB: def __init__(self, name): db_server_ip = conf("config.db_server_ip") db_server_port = conf("config.db_server_port") self.__db_name = "camp{0}".format(name) self.__connection = Connection(host=db_server_ip, port=db_server_port) self.__db = Database(connection=self.__connection, name=self.__db_name) # Return collection with the specified name and filter. def get_collection(self, collection, where={}): return self.__db[collection].find(spec=where) # Returns the first document that matches the where clause within the specified collection. def get_document(self, collection, where={}): return self.__db[collection].find_one(spec_or_id=where) # Removes the specified document. def dispose_document(self, collection, where): return self.__db[collection].remove(spec_or_id=where) # Updates the document with the specified id, the document is inserted if the _id is not found. # Returns the updated document. def set_document(self, collection, _id, values): if _id is None: # If no _id to update, insert it. _id = self.__db[collection].insert(values) else: # If _id to update, update it. self.__db[collection].update( spec={"_id": _id}, document={"$set": values}, upsert=True, new=True) # Return modified document. return self.__db[collection].find_one({"_id": _id}) # Removes the specified collection from the database. def dispose_collection(self, collection): self.__db[collection].drop() # Insert a bulk of documents into a collection. def bulk_import(self, collection, doc_or_docs): self.__db[collection].insert(doc_or_docs) # Return the count of documents for a specific collection. def count_documents(self, collection, where={}): cur = self.__db[collection].find(where) return cur.count() def exists_collection(self, name): return name in self.__db.collection_names() # Moves unique records from source collection to destination, order parameter still pending. def perform_cleanup(self, source, destination): func = ( "function(doc){" + ("if (db.whitelist.find(doc._id).count() == 1)" if self.exists_collection("whitelists") else "") + ("if (db.blacklist.find(doc._id).count() == 0)" if self.exists_collection("blacklists") else "") + " db.data.insert(doc._id)" + "}") logging.debug("Function foreach: " + func) self.__db[destination].drop() self.__db[source].find().forEach(bson.Code(func)) logging.debug("forEach execution completed")