示例#1
0
    def custom_db_creator(self, number_of_rows, pipe, custom):
        try:
            custom_d = faker_options_container()
            for c in custom:
                if custom_d.get(c):
                    logger.warning(
                        "fake2db found valid custom key provided: %s" % c,
                        extra=d)
                else:
                    logger.error(
                        "fake2db does not support the custom key you provided.",
                        extra=d)
                    sys.exit(1)

            for i in range(0, number_of_rows):
                dict_c = {}
                for c in custom:
                    dict_c[c] = getattr(self.faker, c)()

                pipe.hmset('custom:%s' % i, dict_c)

            pipe.execute()

            logger.warning('custom Commits are successful after write job!',
                           extra=d)

        except Exception as e:
            logger.error(e, extra=d)
    def custom_db_creator(self, number_of_rows, db, custom):
        try:
            customdb = db.custom
            data_list = list()
            custom_d = faker_options_container()
            for c in custom:
                if custom_d.get(c):
                    logger.warning(
                        "fake2db found valid custom key provided: %s" % c,
                        extra=d)
                else:
                    logger.error(
                        "fake2db does not support the custom key you provided.",
                        extra=d)
                    sys.exit(1)

            for i in range(0, number_of_rows):
                dict_c = {"id": rnd_id_generator(self)}
                for c in custom:
                    dict_c[c] = getattr(self.faker, c)()

                data_list.append(dict_c)

            customdb.insert_many(data_list)
            logger.warning('custom Commits are successful after write job!',
                           extra=d)

        except Exception as e:
            logger.error(e, extra=d)
示例#3
0
    def custom_db_creator(self, number_of_rows, cursor, conn, custom):
        '''creates and fills the table with simple regis. information
        '''

        custom_d = faker_options_container()
        sqlst = "CREATE TABLE `custom` (`id` varchar(300) NOT NULL,"
        custom_payload = "INSERT INTO custom (id,"

        # form the sql query that will set the db up
        for c in custom:
            if custom_d.get(c):
                sqlst += " `" + c + "` " + custom_d[c] + ","
                custom_payload += " " + c + ","
                logger.warning("fake2db found valid custom key provided: %s" %
                               c,
                               extra=extra_information)
            else:
                logger.error(
                    "fake2db does not support the custom key you provided.",
                    extra=extra_information)
                sys.exit(1)

        # the indice thing is for trimming the last extra comma
        sqlst += " PRIMARY KEY (`id`)) ENGINE=InnoDB"
        custom_payload = custom_payload[:-1]
        custom_payload += ") VALUES (%s, "

        for i in range(0, len(custom)):
            custom_payload += "%s, "
        custom_payload = custom_payload[:-2] + ")"

        try:
            cursor.execute(sqlst)
            conn.commit()
        except mysql.connector.Error as err:
            logger.error(err.msg, extra=extra_information)

        multi_lines = []
        try:
            for i in range(0, number_of_rows):
                multi_lines.append([rnd_id_generator(self)])
                for c in custom:
                    multi_lines[i].append(getattr(self.faker, c)())

            cursor.executemany(custom_payload, multi_lines)
            conn.commit()
            logger.warning('custom Commits are successful after write job!',
                           extra=extra_information)
        except Exception as e:
            logger.error(e, extra=extra_information)
示例#4
0
    def custom_db_creator(self, number_of_rows, cursor, conn, custom):
        '''creates and fills the table with simple regis. information
        '''

        custom_d = faker_options_container()
        sqlst = "CREATE TABLE custom (id serial PRIMARY KEY,"
        custom_payload = "INSERT INTO custom ("

        # form the sql query that will set the db up
        for c in custom:
            if custom_d.get(c):
                sqlst += " " + c + " " + custom_d[c] + ","
                custom_payload += " " + c + ","
                logger.warning("fake2db found valid custom key provided: %s" %
                               c,
                               extra=d)
            else:
                logger.error(
                    "fake2db does not support the custom key you provided.",
                    extra=d)
                sys.exit(1)

        # the indice thing is for trimming the last extra comma
        sqlst = sqlst[:-1]
        sqlst += ");"
        custom_payload = custom_payload[:-1]
        custom_payload += ") VALUES ("

        for i in range(0, len(custom)):
            custom_payload += "%s, "
        custom_payload = custom_payload[:-2] + ")"
        cursor.execute(sqlst)
        conn.commit()

        multi_lines = []
        try:
            for i in range(0, number_of_rows):
                multi_lines.append([])
                for c in custom:
                    multi_lines[i].append(getattr(self.faker, c)())

            cursor.executemany(custom_payload, multi_lines)
            conn.commit()
            logger.warning('custom Commits are successful after write job!',
                           extra=d)
        except Exception as e:
            logger.error(e, extra=d)
示例#5
0
    def custom_db_creator(self, number_of_rows, cursor, conn, custom):
        """creates and fills the table with simple regis. information
        """

        custom_d = faker_options_container()
        sqlst = "CREATE TABLE `custom` (`id` varchar(300) NOT NULL,"
        custom_payload = "INSERT INTO custom (id,"

        # form the sql query that will set the db up
        for c in custom:
            if custom_d.get(c):
                sqlst += " `" + c + "` " + custom_d[c] + ","
                custom_payload += " " + c + ","
                logger.warning("fake2db found valid custom key provided: %s" % c, extra=extra_information)
            else:
                logger.error("fake2db does not support the custom key you provided.", extra=extra_information)
                sys.exit(1)

        # the indice thing is for trimming the last extra comma
        sqlst += " PRIMARY KEY (`id`)) ENGINE=InnoDB"
        custom_payload = custom_payload[:-1]
        custom_payload += ") VALUES (%s, "

        for i in range(0, len(custom)):
            custom_payload += "%s, "
        custom_payload = custom_payload[:-2] + ")"

        try:
            cursor.execute(sqlst)
            conn.commit()
        except mysql.connector.Error as err:
            logger.error(err.msg, extra=extra_information)

        multi_lines = []
        try:
            for i in range(0, number_of_rows):
                multi_lines.append([rnd_id_generator(self)])
                for c in custom:
                    multi_lines[i].append(getattr(self.faker, c)())

            cursor.executemany(custom_payload, multi_lines)
            conn.commit()
            logger.warning("custom Commits are successful after write job!", extra=extra_information)
        except Exception as e:
            logger.error(e, extra=extra_information)
示例#6
0
    def custom_db_creator(self, number_of_rows, cursor, conn, custom):
        '''creates and fills the table with simple regis. information
        '''

        custom_d = faker_options_container()
        sqlst = "CREATE TABLE custom (id serial PRIMARY KEY,"
        custom_payload = "INSERT INTO custom ("
        
        # form the sql query that will set the db up
        for c in custom:
            if custom_d.get(c):
                sqlst += " " + c + " " + custom_d[c] + ","
                custom_payload += " " + c + ","
                logger.warning("fake2db found valid custom key provided: %s" % c, extra=d)
            else:
                logger.error("fake2db does not support the custom key you provided.", extra=d )
                sys.exit(1)
                
        # the indice thing is for trimming the last extra comma
        sqlst = sqlst[:-1]
        sqlst += ");"
        custom_payload = custom_payload[:-1]
        custom_payload += ") VALUES ("

        for i in range(0, len(custom)):
            custom_payload += "%s, "
        custom_payload = custom_payload[:-2] + ")"
        cursor.execute(sqlst)
        conn.commit()

        multi_lines = []
        try:
            for i in range(0, number_of_rows):
                multi_lines.append([])
                for c in custom:
                    multi_lines[i].append(getattr(self.faker, c)())
            
            cursor.executemany(custom_payload, multi_lines)
            conn.commit()
            logger.warning('custom Commits are successful after write job!', extra=d)
        except Exception as e:
            logger.error(e, extra=d)
    def custom_db_creator(self, number_of_rows, conn, custom):
        '''creates and fills the table with simple regis. information
        '''
        cursor = conn.cursor()
        custom_d = faker_options_container()
        sqlst = '''
        CREATE TABLE custom(id TEXT PRIMARY KEY,'''

        # first one is always ID primary key
        exec_many = 'insert into custom values(?,'

        for c in custom:
            if custom_d.get(c):
                sqlst += " " + c + " TEXT, "
                exec_many += '?,'
                logger.warning("fake2db found valid custom key provided: %s" %
                               c,
                               extra=d)
            else:
                logger.error(
                    "fake2db does not support the custom key you provided.",
                    extra=d)
                sys.exit(1)

        sqlst = sqlst[:-2] + ")"
        cursor.execute(sqlst)
        conn.commit()
        multi_lines = []
        exec_many = exec_many[:-1] + ')'

        try:
            for i in range(0, number_of_rows):
                multi_lines.append([rnd_id_generator(self)])
                for c in custom:
                    multi_lines[i].append(getattr(self.faker, c)())

            cursor.executemany(exec_many, multi_lines)
            conn.commit()
            logger.warning('custom Commits are successful after write job!',
                           extra=d)
        except Exception as e:
            logger.error(e, extra=d)
示例#8
0
    def custom_db_creator(self, number_of_rows, pipe, custom):
        try:
            custom_d = faker_options_container()
            for c in custom:
                if custom_d.get(c):
                    logger.warning("fake2db found valid custom key provided: %s" % c, extra=d)
                else:
                    logger.error("fake2db does not support the custom key you provided.", extra=d )
                    sys.exit(1)

            for i in range(0, number_of_rows):
                dict_c = {}
                for c in custom:
                    dict_c[c] = getattr(self.faker, c)()
                    
                pipe.hmset('custom:%s' % i, dict_c)
                
            pipe.execute()
            
            logger.warning('custom Commits are successful after write job!', extra=d)

        except Exception as e:
            logger.error(e, extra=d)
示例#9
0
    def custom_db_creator(self, number_of_rows, conn, custom):
        '''creates and fills the table with simple regis. information
        '''
        cursor = conn.cursor()
        custom_d = faker_options_container()
        sqlst = '''
        CREATE TABLE custom(id TEXT PRIMARY KEY,'''

        # first one is always ID primary key
        exec_many = 'insert into custom values(?,'
        
        for c in custom:
            if custom_d.get(c):
                sqlst += " " + c + " TEXT, "
                exec_many += '?,'
                logger.warning("fake2db found valid custom key provided: %s" % c, extra=d)
            else:
                logger.error("fake2db does not support the custom key you provided.", extra=d )
                sys.exit(1)
                
        sqlst = sqlst[:-2] + ")"
        cursor.execute(sqlst)   
        conn.commit()
        multi_lines = []
        exec_many = exec_many[:-1] +')'
        
        try:
            for i in range(0, number_of_rows):
                multi_lines.append([rnd_id_generator(self)])
                for c in custom:
                    multi_lines[i].append(getattr(self.faker, c)())
            
            cursor.executemany(exec_many, multi_lines)
            conn.commit()
            logger.warning('custom Commits are successful after write job!', extra=d)
        except Exception as e:
            logger.error(e, extra=d)
示例#10
0
    def custom_db_creator(self, number_of_rows, db, custom):
        try:
            data_list = list()
            custom_d = faker_options_container()
            
            for c in custom:
                if custom_d.get(c):
                    logger.warning("fake2db found valid custom key provided: %s" % c, extra=d)
                else:
                    logger.error("fake2db does not support the custom key you provided.", extra=d )
                    sys.exit(1)
                    
            for i in range(0, number_of_rows):
                dict_c = {"id": rnd_id_generator(self)}
                for c in custom:
                    dict_c[c] = getattr(self.faker, c)()
                          
                db.save(dict_c)

            
            logger.warning('custom Commits are successful after write job!', extra=d)

        except Exception as e:
            logger.error(e, extra=d)
示例#11
0
文件: fake2db.py 项目: Ritaja/fake2db
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--rows", help="Amount of rows desired per table",
                        type=int)
    parser.add_argument(
        "--db",
        help=
        "Db type for creation: sqlite, mysql, postgresql, mongodb, redis, couchdb, to be expanded")
    parser.add_argument("--name", help="The name to the db to be generated")
    parser.add_argument("--host", help="Hostname of db")
    parser.add_argument("--port", help="Port of db", type=int)
    parser.add_argument("--username", help="Username")
    parser.add_argument("--password", help="Password")
    parser.add_argument("--custom", nargs='+', help="Custom schema for db generation, supports functions that fake-factory provides, see fake2db github repository for options https://github.com/emirozer/fake2db")


    args = parser.parse_args()

    if not args.rows or not args.db:
        logger.error('Please use with --help argument for usage information!',
                     extra=extra_information)

    else:
        logger.info('arguments found(rows and db), starting faking!!',
                    extra=extra_information)
        logger.warning('Rows argument : %s', args.rows,
                       extra=extra_information)
        logger.info('DB argument : %s', args.db, extra=extra_information)

        if args.custom:
            custom_d = faker_options_container()
            for c in args.custom:
                if custom_d.get(c):
                    logger.info("fake2db found valid custom key provided: %s" % c, extra=extra_information)
                else:
                    logger.error("fake2db does not support the custom key you provided: %s" % c, extra=extra_information )
                    sys.exit(1)
                
        if args.db == 'sqlite':
            try:
                from sqlite_handler import Fake2dbSqliteHandler
                fake_sqlite_handler = Fake2dbSqliteHandler()
            except Exception:
                raise InstantiateDBHandlerException
            if args.name and args.custom:
                fake_sqlite_handler.fake2db_sqlite_initiator(args.rows,
                                                             args.name, args.custom)
            elif args.custom:
                fake_sqlite_handler.fake2db_sqlite_initiator(args.rows, None, args.custom)
            else:
                fake_sqlite_handler.fake2db_sqlite_initiator(args.rows)

        elif args.db == 'mysql':
            try:
                from mysql_handler import Fake2dbMySqlHandler
                fake_mysql_handler = Fake2dbMySqlHandler()
            except Exception:
                raise InstantiateDBHandlerException
            _mysqld_process_checkpoint()
            host = args.host or "127.0.0.1"
            port = args.port or 3306
            username = args.username or getpass.getuser()
            if args.name and args.custom:
                fake_mysql_handler.fake2db_mysql_initiator(
                    host, port, args.password, username, args.rows, args.name, args.custom)
            elif args.custom:
                fake_mysql_handler.fake2db_mysql_initiator(
                    host, port, args.password, username, args.rows, None, args.custom)
            else:
                fake_mysql_handler.fake2db_mysql_initiator(
                    host, port, args.password, username, args.rows, None, None)

        elif args.db == 'postgresql':
            try:
                import psycopg2
            except ImportError:
                raise MissingDependencyException(
                    'psycopg2 package not found on the python packages, please run: pip install psycopg2')

            try:
                from postgresql_handler import Fake2dbPostgresqlHandler
                fake_postgresql_handler = Fake2dbPostgresqlHandler()
            except Exception:
                raise InstantiateDBHandlerException
            host = args.host or "localhost"
            port = args.port or 5432
            username = args.username or getpass.getuser()
            custom = args.custom or None
            fake_postgresql_handler.fake2db_initiator(host=host, port=port,
                                                      username=username,
                                                      password=args.password,
                                                      number_of_rows=args.rows,
                                                      name=args.name,
                                                      custom=custom)

        elif args.db == 'mongodb':
            try:
                import pymongo
            except ImportError:
                raise MissingDependencyException(
                    'pymongo package not found on the python packages, please run: pip install pymongo')

            try:
                from mongodb_handler import Fake2dbMongodbHandler
                fake_mongodb_handler = Fake2dbMongodbHandler()
            except Exception:
                raise InstantiateDBHandlerException
            _mongodb_process_checkpoint()
            host = args.host or "localhost"
            port = args.port or 27017

            if args.name and args.custom:
                fake_mongodb_handler.fake2db_mongodb_initiator(
                    host, port, args.rows, args.name, args.custom)
            elif args.custom:
                fake_mongodb_handler.fake2db_mongodb_initiator(
                    host, port, args.rows, None, args.custom)
            else:
                fake_mongodb_handler.fake2db_mongodb_initiator(host, port,
                                                               args.rows)

        elif args.db == 'couchdb':
            try:
                import couchdb
            except ImportError:
                raise MissingDependencyException(
                    'couchdb package not found on the python packages, please run: pip install couchdb')

            try:
                from couchdb_handler import Fake2dbCouchdbHandler
                fake_couchdb_handler = Fake2dbCouchdbHandler()
            except Exception:
                raise InstantiateDBHandlerException
            _couchdb_process_checkpoint()
            
            if args.name and args.custom:
                fake_couchdb_handler.fake2db_couchdb_initiator(
                    args.rows, args.name, args.custom)
            elif args.custom:
                fake_couchdb_handler.fake2db_couchdb_initiator(
                    args.rows, None, args.custom)
            else:
                fake_couchdb_handler.fake2db_couchdb_initiator(args.rows)
                
        elif args.db == 'redis':
            if args.name and (not args.name.isdigit() or int(args.name) < 0):
                logger.error('redis db name must be a non-negative integer',
                             extra=extra_information)
                return

            try:
                import redis
            except ImportError:
                raise MissingDependencyException(
                    'redis package not found on the python packages, please run: pip install redis')

            try:
                from redis_handler import Fake2dbRedisHandler
                fake_redis_handler = Fake2dbRedisHandler()
            except Exception:
                raise InstantiateDBHandlerException
            host = args.host or "localhost"
            port = args.port or 6379
            _redis_process_checkpoint(host, port)
            if args.name and args.custom:
                fake_redis_handler.fake2db_redis_initiator(
                    host, port, args.rows, args.name, args.custom)
            elif args.custom:
                fake_redis_handler.fake2db_redis_initiator(
                    host, port, args.rows, None, args.custom)
            else:
                fake_redis_handler.fake2db_redis_initiator(host, port,
                                                           args.rows)

        else:
            logger.error(
                'Wrong arg for db parameter. Valid ones : sqlite - mysql - postgresql - mongodb - redis',
                extra=extra_information)
示例#12
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--rows",
                        help="Amount of rows desired per table",
                        type=int)
    parser.add_argument(
        "--db",
        help=
        "Db type for creation: sqlite, mysql, postgresql, mongodb, redis, couchdb, to be expanded"
    )
    parser.add_argument("--name", help="The name to the db to be generated")
    parser.add_argument("--host", help="Hostname of db")
    parser.add_argument("--port", help="Port of db", type=int)
    parser.add_argument("--username", help="Username")
    parser.add_argument("--password", help="Password")
    parser.add_argument(
        "--custom",
        nargs='+',
        help=
        "Custom schema for db generation, supports functions that fake-factory provides, see fake2db github repository for options https://github.com/emirozer/fake2db"
    )

    args = parser.parse_args()

    if not args.rows or not args.db:
        logger.error('Please use with --help argument for usage information!',
                     extra=extra_information)

    else:
        logger.info('arguments found(rows and db), starting faking!!',
                    extra=extra_information)
        logger.warning('Rows argument : %s',
                       args.rows,
                       extra=extra_information)
        logger.info('DB argument : %s', args.db, extra=extra_information)

        if args.custom:
            custom_d = faker_options_container()
            for c in args.custom:
                if custom_d.get(c):
                    logger.info("fake2db found valid custom key provided: %s" %
                                c,
                                extra=extra_information)
                else:
                    logger.error(
                        "fake2db does not support the custom key you provided: %s"
                        % c,
                        extra=extra_information)
                    sys.exit(1)

        if args.db == 'sqlite':
            try:
                from sqlite_handler import Fake2dbSqliteHandler
                fake_sqlite_handler = Fake2dbSqliteHandler()
            except Exception:
                raise InstantiateDBHandlerException
            if args.name and args.custom:
                fake_sqlite_handler.fake2db_sqlite_initiator(
                    args.rows, args.name, args.custom)
            elif args.custom:
                fake_sqlite_handler.fake2db_sqlite_initiator(
                    args.rows, None, args.custom)
            else:
                fake_sqlite_handler.fake2db_sqlite_initiator(args.rows)

        elif args.db == 'mysql':
            try:
                from mysql_handler import Fake2dbMySqlHandler
                fake_mysql_handler = Fake2dbMySqlHandler()
            except Exception:
                raise InstantiateDBHandlerException
            _mysqld_process_checkpoint()
            host = args.host or "127.0.0.1"
            port = args.port or 3306
            username = args.username or getpass.getuser()
            if args.name and args.custom:
                fake_mysql_handler.fake2db_mysql_initiator(
                    host, port, args.password, username, args.rows, args.name,
                    args.custom)
            elif args.custom:
                fake_mysql_handler.fake2db_mysql_initiator(
                    host, port, args.password, username, args.rows, None,
                    args.custom)
            else:
                fake_mysql_handler.fake2db_mysql_initiator(
                    host, port, args.password, username, args.rows, None, None)

        elif args.db == 'postgresql':
            try:
                import psycopg2
            except ImportError:
                raise MissingDependencyException(
                    'psycopg2 package not found on the python packages, please run: pip install psycopg2'
                )

            try:
                from postgresql_handler import Fake2dbPostgresqlHandler
                fake_postgresql_handler = Fake2dbPostgresqlHandler()
            except Exception:
                raise InstantiateDBHandlerException
            host = args.host or "localhost"
            port = args.port or 5432
            username = args.username or getpass.getuser()
            custom = args.custom or None
            fake_postgresql_handler.fake2db_initiator(host=host,
                                                      port=port,
                                                      username=username,
                                                      password=args.password,
                                                      number_of_rows=args.rows,
                                                      name=args.name,
                                                      custom=custom)

        elif args.db == 'mongodb':
            try:
                import pymongo
            except ImportError:
                raise MissingDependencyException(
                    'pymongo package not found on the python packages, please run: pip install pymongo'
                )

            try:
                from mongodb_handler import Fake2dbMongodbHandler
                fake_mongodb_handler = Fake2dbMongodbHandler()
            except Exception:
                raise InstantiateDBHandlerException
            _mongodb_process_checkpoint()
            host = args.host or "localhost"
            port = args.port or 27017

            if args.name and args.custom:
                fake_mongodb_handler.fake2db_mongodb_initiator(
                    host, port, args.rows, args.name, args.custom)
            elif args.custom:
                fake_mongodb_handler.fake2db_mongodb_initiator(
                    host, port, args.rows, None, args.custom)
            else:
                fake_mongodb_handler.fake2db_mongodb_initiator(
                    host, port, args.rows)

        elif args.db == 'couchdb':
            try:
                import couchdb
            except ImportError:
                raise MissingDependencyException(
                    'couchdb package not found on the python packages, please run: pip install couchdb'
                )

            try:
                from couchdb_handler import Fake2dbCouchdbHandler
                fake_couchdb_handler = Fake2dbCouchdbHandler()
            except Exception:
                raise InstantiateDBHandlerException
            _couchdb_process_checkpoint()

            if args.name and args.custom:
                fake_couchdb_handler.fake2db_couchdb_initiator(
                    args.rows, args.name, args.custom)
            elif args.custom:
                fake_couchdb_handler.fake2db_couchdb_initiator(
                    args.rows, None, args.custom)
            else:
                fake_couchdb_handler.fake2db_couchdb_initiator(args.rows)

        elif args.db == 'redis':
            if args.name and (not args.name.isdigit() or int(args.name) < 0):
                logger.error('redis db name must be a non-negative integer',
                             extra=extra_information)
                return

            try:
                import redis
            except ImportError:
                raise MissingDependencyException(
                    'redis package not found on the python packages, please run: pip install redis'
                )

            try:
                from redis_handler import Fake2dbRedisHandler
                fake_redis_handler = Fake2dbRedisHandler()
            except Exception:
                raise InstantiateDBHandlerException
            host = args.host or "localhost"
            port = args.port or 6379
            _redis_process_checkpoint(host, port)
            if args.name and args.custom:
                fake_redis_handler.fake2db_redis_initiator(
                    host, port, args.rows, args.name, args.custom)
            elif args.custom:
                fake_redis_handler.fake2db_redis_initiator(
                    host, port, args.rows, None, args.custom)
            else:
                fake_redis_handler.fake2db_redis_initiator(
                    host, port, args.rows)

        else:
            logger.error(
                'Wrong arg for db parameter. Valid ones : sqlite - mysql - postgresql - mongodb - redis',
                extra=extra_information)