diff --git a/README.md b/README.md index 5a92406..cc200f2 100644 --- a/README.md +++ b/README.md @@ -54,9 +54,8 @@ mysql connector is needed for mysql db generation: ##### Usage -It is as follows, at this point in time, fake2db accepts rows and db argument. -*--rows* argument is pretty clear... +*--rows* argument is pretty clear :) integer *--db* argument takes 6 possible options : sqlite, mysql, postgresql, mongodb, redis, couchdb @@ -66,7 +65,10 @@ It is as follows, at this point in time, fake2db accepts rows and db argument. *--port* argument is OPTIONAL. Port to use for database connection. Not used for sqlite. -*--password* argument is OPTIONAL. Password for root. Only supported for mysql. +*--username* argument is OPTIONAL. Username for the databse user. + +*--password* argument is OPTIONAL. Password for database user. Only supported for mysql & postgresql. + > fake2db --rows 200 --db sqlite
@@ -79,9 +81,28 @@ In addition to the databases supported in the db argument, you can also run fake > fake2db --rows --db postgresql --port 15432 + +##### Custom Database Generation + +If you want to create a custom db/table, you have to provide **--custom** parameter followed by the column item you want. At the point in time, i mapped all the possible column items you can use here: + + + +Feed any keys you want to the custom flag: + +> fake2db.py --rows 250 --db mysql --username mysql --password somepassword --custom name date country +
+>fake2db.py --rows 1500 --db mysql --password randompassword --custom currency_code credit_card_full credit_card_provider +
+>fake2db.py --rows 20 --db mongodb --custom name date country + +
##### Sample output - sqlite ![Screenshot](https://raw.github.com/emirozer/fake2db/master/docs/fake2db_example_sqlite.png) - +
+![Screenshot](https://raw.github.com/emirozer/fake2db/master/docs/mysql_fake2db.png) +
+![Screenshot](https://raw.github.com/emirozer/fake2db/master/docs/pg_fake2db.png)
diff --git a/docs/mysql_fake2db.png b/docs/mysql_fake2db.png new file mode 100644 index 0000000..ef3d464 Binary files /dev/null and b/docs/mysql_fake2db.png differ diff --git a/docs/pg_fake2db.png b/docs/pg_fake2db.png new file mode 100644 index 0000000..f7a9eb6 Binary files /dev/null and b/docs/pg_fake2db.png differ diff --git a/fake2db/__init__.py b/fake2db/__init__.py index 72d7b4c..1a93225 100644 --- a/fake2db/__init__.py +++ b/fake2db/__init__.py @@ -6,6 +6,6 @@ """ __title__ = 'fake2db' -__version__ = '0.4.1' +__version__ = '0.5.0' __author__ = 'Emir Ozer' __license__ = 'GNU General Public License v2' diff --git a/fake2db/couchdb_handler.py b/fake2db/couchdb_handler.py index 03f3936..19d9504 100644 --- a/fake2db/couchdb_handler.py +++ b/fake2db/couchdb_handler.py @@ -1,4 +1,6 @@ import couchdb +import sys +from custom import faker_options_container from helpers import fake2db_logger, lower_str_generator, rnd_id_generator logger, extra_information = fake2db_logger() @@ -15,12 +17,16 @@ class Fake2dbCouchdbHandler(): faker = Factory.create() - def fake2db_couchdb_initiator(self, number_of_rows, name=None): + def fake2db_couchdb_initiator(self, number_of_rows, name=None, custom=None): '''Main handler for the operation ''' rows = number_of_rows db = self.database_caller_creator(name) + if custom: + self.custom_db_creator(rows, db, custom) + sys.exit(0) + self.data_filler_simple_registration(rows, db) self.data_filler_detailed_registration(rows, db) self.data_filler_company(rows, db) @@ -45,6 +51,32 @@ def database_caller_creator(self, name=None): return db + 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) + + def data_filler_simple_registration(self, number_of_rows, db): '''creates and fills the table with simple regis. information ''' diff --git a/fake2db/custom.py b/fake2db/custom.py new file mode 100644 index 0000000..ddaffd9 --- /dev/null +++ b/fake2db/custom.py @@ -0,0 +1,151 @@ +def faker_options_container(): + ''' + Following dictionary is mapped with fake-factory + providers, so that custom db creation be made with + faker. + + for detailed info for providers: + + http://fake-factory.readthedocs.org/en/latest/# + + ''' + faker_options = {"name_male": "varchar(200)", + "address": "varchar(300)", + "am_pm": "varchar(3)", + "boolean": "varchar(10)", + "opera": "varchar(300)", + "paragraph": "varchar(300)", + "bs": "varchar(300)", + "building_number": "INT", + "password": "varchar(300)", + "century": "varchar(50)", + "phone_number": "varchar(250)", + "chrome": "varchar(300)", + "postalcode": "varchar(300)", + "city": "varchar(300)", + "postalcode_plus4": "varchar(300)", + "city_prefix": "varchar(300)", + "postcode": "varchar(300)", + "city_suffix": "varchar(300)", + "prefix": "varchar(300)", + "color_name": "varchar(300)", + "prefix_female": "varchar(300)", + "company": "varchar(300)", + "prefix_male": "varchar(300)", + "company_email": "varchar(300)", + "profile": "varchar(300)", + "company_suffix": "varchar(300)", + "provider": "varchar(300)", + "country": "varchar(300)", + "country_code": "varchar(300)", + "credit_card_expire": "varchar(300)", + "credit_card_full": "varchar(300)", + "credit_card_number": "varchar(300)", + "credit_card_provider": "varchar(300)", + "credit_card_security_code": "varchar(300)", + "currency_code": "varchar(30)", + "date": "varchar(300)", + "date_time_ad": "varchar(300)", + "date_time_between": "varchar(300)", + "date_time_between_dates": "varchar(300)", + "random_digit": "INT", + "random_digit_not_null": "varchar(300)", + "date_time_this_decade": "varchar(300)", + "random_digit_not_null_or_empty": "varchar(300)", + "date_time_this_month": "varchar(300)", + "random_digit_or_empty": "varchar(300)", + "date_time_this_year": "varchar(300)", + "random_element": "varchar(300)", + "day_of_month": "varchar(300)", + "random_int": "INT", + "day_of_week": "varchar(300)", + "random_letter": "varchar(5)", + "domain_name": "varchar(300)", + "random_number": "INT", + "domain_word": "varchar(300)", + "ean": "varchar(300)", + "rgb_color": "varchar(300)", + "ean13": "varchar(300)", + "rgb_color_list": "varchar(300)", + "ean8": "varchar(300)", + "prgb_css_color": "varchar(300)", + "email": "varchar(300)", + "safari": "varchar(300)", + "file_extension": "varchar(300)", + "safe_color_name": "varchar(300)", + "file_name": "varchar(300)", + "safe_email": "varchar(300)", + "firefox": "varchar(300)", + "safe_hex_color": "varchar(300)", + "first_name": "varchar(300)", + "secondary_address": "varchar(300)", + "first_name_female": "varchar(300)", + "first_name_male": "varchar(300)", + "sentence": "varchar(300)", + "free_email": "varchar(300)", + "free_email_domain": "varchar(300)", + "sha1": "varchar(300)", + "geo_coordinate": "varchar(300)", + "sha256": "varchar(300)", + "slug": "varchar(300)", + "hex_color": "varchar(300)", + "ssn": "varchar(300)", + "image_url": "varchar(300)", + "state": "varchar(300)", + "internet_explorer": "varchar(300)", + "state_abbr": "varchar(300)", + "ipv4": "varchar(300)", + "street_address": "varchar(300)", + "ipv6": "varchar(300)", + "street_name": "varchar(300)", + "iso8601": "varchar(300)", + "street_suffix": "varchar(300)", + "job": "varchar(300)", + "suffix": "varchar(300)", + "language_code": "varchar(300)", + "suffix_female": "varchar(300)", + "last_name": "varchar(300)", + "suffix_male": "varchar(300)", + "last_name_female": "varchar(300)", + "text": "varchar(300)", + "last_name_male": "varchar(300)", + "time": "varchar(300)", + "latitude": "varchar(300)", + "time_delta": "varchar(300)", + "timezone": "varchar(300)", + "linux_platform_token": "varchar(300)", + "tld": "varchar(300)", + "linux_processor": "varchar(300)", + "unix_time": "varchar(300)", + "locale": "varchar(300)", + "uri": "varchar(300)", + "longitude": "varchar(300)", + "uri_extension": "varchar(300)", + "mac_address": "varchar(300)", + "uri_page": "varchar(300)", + "mac_platform_token": "varchar(300)", + "uri_path": "varchar(300)", + "mac_processor": "varchar(300)", + "url": "varchar(300)", + "md5": "varchar(300)", + "user_agent": "varchar(300)", + "military_apo": "varchar(300)", + "user_name": "varchar(300)", + "military_dpo": "varchar(300)", + "uuid4": "varchar(300)", + "military_ship": "varchar(300)", + "windows_platform_token": "varchar(300)", + "military_state": "varchar(300)", + "word": "varchar(300)", + "mime_type": "varchar(300)", + "words": "varchar(300)", + "month": "varchar(300)", + "year": "varchar(300)", + "month_name": "varchar(300)", + "zipcode": "varchar(300)", + "name": "varchar(300)", + "zipcode_plus4": "varchar(300)", + "name_female": "varchar(300)", + } + + return faker_options diff --git a/fake2db/fake2db.py b/fake2db/fake2db.py index bf8b56a..3e73f39 100644 --- a/fake2db/fake2db.py +++ b/fake2db/fake2db.py @@ -2,7 +2,8 @@ import getpass import subprocess import time - +import sys +from custom import faker_options_container from helpers import fake2db_logger logger, extra_information = fake2db_logger() @@ -98,6 +99,8 @@ def main(): 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() @@ -112,15 +115,26 @@ def main(): 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: + if args.name and args.custom: fake_sqlite_handler.fake2db_sqlite_initiator(args.rows, - args.name) + 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) @@ -133,12 +147,16 @@ def main(): _mysqld_process_checkpoint() host = args.host or "127.0.0.1" port = args.port or 3306 - if args.name: + username = args.username or getpass.getuser() + if args.name and args.custom: fake_mysql_handler.fake2db_mysql_initiator( - host, port, args.password, args.rows, args.name) + 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, args.rows) + host, port, args.password, username, args.rows, None, None) elif args.db == 'postgresql': try: @@ -155,9 +173,13 @@ def main(): 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) + username=username, + password=args.password, + number_of_rows=args.rows, + name=args.name, + custom=custom) elif args.db == 'mongodb': try: @@ -174,9 +196,13 @@ def main(): _mongodb_process_checkpoint() host = args.host or "localhost" port = args.port or 27017 - if args.name: + + if args.name and args.custom: fake_mongodb_handler.fake2db_mongodb_initiator( - host, port, args.rows, args.name) + 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) @@ -194,13 +220,16 @@ def main(): except Exception: raise InstantiateDBHandlerException _couchdb_process_checkpoint() - - if args.name: - fake_couchdb_handler.fake2db_couchdb_initiator(args.rows, - args.name) + + 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', @@ -221,9 +250,12 @@ def main(): host = args.host or "localhost" port = args.port or 6379 _redis_process_checkpoint(host, port) - if args.name: + 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, args.name) + host, port, args.rows, None, args.custom) else: fake_redis_handler.fake2db_redis_initiator(host, port, args.rows) diff --git a/fake2db/mongodb_handler.py b/fake2db/mongodb_handler.py index 5286620..b51b530 100644 --- a/fake2db/mongodb_handler.py +++ b/fake2db/mongodb_handler.py @@ -1,4 +1,6 @@ import pymongo +import sys +from custom import faker_options_container from helpers import fake2db_logger, str_generator, rnd_id_generator @@ -16,18 +18,49 @@ class Fake2dbMongodbHandler(): faker = Factory.create() - def fake2db_mongodb_initiator(self, host, port, number_of_rows, name=None): + def fake2db_mongodb_initiator(self, host, port, number_of_rows, name=None, custom=None): '''Main handler for the operation ''' rows = number_of_rows db = self.database_caller_creator(host, port, name) + if custom: + self.custom_db_creator(rows, db, custom) + sys.exit(0) + self.data_filler_simple_registration(rows, db) self.data_filler_detailed_registration(rows, db) self.data_filler_company(rows, db) self.data_filler_user_agent(rows, db) self.data_filler_customer(rows, db) + + 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) + + def database_caller_creator(self, host, port, name=None): '''creates a mongodb database returns the related connection object diff --git a/fake2db/mysql_handler.py b/fake2db/mysql_handler.py index be4eae2..3d39746 100644 --- a/fake2db/mysql_handler.py +++ b/fake2db/mysql_handler.py @@ -1,5 +1,5 @@ import sys - +from custom import faker_options_container from helpers import fake2db_logger, str_generator, rnd_id_generator @@ -23,15 +23,21 @@ class Fake2dbMySqlHandler(): faker = Factory.create() - def fake2db_mysql_initiator(self, host, port, password, number_of_rows, name=None): + def fake2db_mysql_initiator(self, host, port, password, username, number_of_rows, name=None, custom=None): '''Main handler for the operation ''' rows = number_of_rows if name: - cursor, conn = self.database_caller_creator(host, port, password, name) + cursor, conn = self.database_caller_creator(host, port, password, username, name) else: - cursor, conn = self.database_caller_creator(host, port, password) + cursor, conn = self.database_caller_creator(host, port, password, username) + + if custom: + self.custom_db_creator(rows, cursor, conn, custom) + cursor.close() + conn.close() + sys.exit(0) tables = self.mysql_table_creator() keys = tables.keys() @@ -46,6 +52,7 @@ def fake2db_mysql_initiator(self, host, port, password, number_of_rows, name=Non logger.info("OK", extra=extra_information) logger.warning('Table creation ops finished', extra=extra_information) + self.data_filler_simple_registration(rows, cursor, conn) self.data_filler_detailed_registration(rows, cursor, conn) self.data_filler_company(rows, cursor, conn) @@ -54,7 +61,7 @@ def fake2db_mysql_initiator(self, host, port, password, number_of_rows, name=Non cursor.close() conn.close() - def database_caller_creator(self, host, port, password, name=None): + def database_caller_creator(self, host, port, password, username, name=None): '''creates a mysql db returns the related connection object which will be later used to spawn the cursor @@ -68,7 +75,7 @@ def database_caller_creator(self, host, port, password, name=None): else: db = 'mysql_' + str_generator(self) - conn = mysql.connector.connect(user='root', host=host, port=port, password=password) + conn = mysql.connector.connect(user=username, host=host, port=port, password=password) cursor = conn.cursor() cursor.execute('CREATE DATABASE IF NOT EXISTS ' + db) cursor.execute('USE ' + db) @@ -143,6 +150,52 @@ def mysql_table_creator(self): return tables + 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) + def data_filler_simple_registration(self, number_of_rows, cursor, conn): '''creates and fills the table with simple regis. information ''' diff --git a/fake2db/postgresql_handler.py b/fake2db/postgresql_handler.py index 3c41508..67acb2d 100644 --- a/fake2db/postgresql_handler.py +++ b/fake2db/postgresql_handler.py @@ -1,7 +1,8 @@ -from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT import psycopg2 - +import sys +from custom import faker_options_container from helpers import fake2db_logger, str_generator +from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT logger, extra_information = fake2db_logger() d = extra_information @@ -21,7 +22,7 @@ def fake2db_initiator(self, number_of_rows, **connection_kwargs): '''Main handler for the operation ''' rows = number_of_rows - cursor, conn = self.database_caller_creator(**connection_kwargs) + cursor, conn = self.database_caller_creator(number_of_rows, **connection_kwargs) self.data_filler_simple_registration(rows, cursor, conn) self.data_filler_detailed_registration(rows, cursor, conn) @@ -31,7 +32,7 @@ def fake2db_initiator(self, number_of_rows, **connection_kwargs): cursor.close() conn.close() - def database_caller_creator(self, username, password, host, port, name=None): + def database_caller_creator(self, number_of_rows, username, password, host, port, name=None, custom=None): '''creates a postgresql db returns the related connection object which will be later used to spawn the cursor @@ -61,8 +62,57 @@ def database_caller_creator(self, username, password, host, port, name=None): logger.error(err, extra=d) raise + if custom: + self.custom_db_creator(number_of_rows, cursor, conn, custom) + cursor.close() + conn.close() + sys.exit(0) + return cursor, conn + 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 data_filler_simple_registration(self, number_of_rows, cursor, conn): '''creates and fills the table with simple regis. information ''' @@ -76,7 +126,6 @@ def data_filler_simple_registration(self, number_of_rows, cursor, conn): for i in range(0, number_of_rows): simple_registration_data.append((self.faker.safe_email(), self.faker.md5(raw_output=False))) - simple_registration_payload = ("INSERT INTO simple_registration " "(email, password) " "VALUES (%s, %s)") diff --git a/fake2db/redis_handler.py b/fake2db/redis_handler.py index a89fe21..c6d053f 100644 --- a/fake2db/redis_handler.py +++ b/fake2db/redis_handler.py @@ -1,4 +1,6 @@ import redis +import sys +from custom import faker_options_container from helpers import fake2db_logger, rnd_id_generator @@ -15,12 +17,17 @@ class Fake2dbRedisHandler(): faker = Factory.create() - def fake2db_redis_initiator(self, host, port, number_of_rows, name=None): + def fake2db_redis_initiator(self, host, port, number_of_rows, name=None, custom=None): '''Main handler for the operation ''' client, pipe = self.database_caller_creator(host, port, name) + if custom: + self.custom_db_creator(number_of_rows, pipe, custom) + client.save() + sys.exit(0) + self.data_filler_simple_registration(number_of_rows, pipe) self.data_filler_detailed_registration(number_of_rows, pipe) self.data_filler_company(number_of_rows, pipe) @@ -29,6 +36,30 @@ def fake2db_redis_initiator(self, host, port, number_of_rows, name=None): client.save() + 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 database_caller_creator(self, host, port, name=None): '''creates a redis connection object which will be later used to modify the db diff --git a/fake2db/sqlite_handler.py b/fake2db/sqlite_handler.py index 08784b5..4ec5a56 100644 --- a/fake2db/sqlite_handler.py +++ b/fake2db/sqlite_handler.py @@ -1,5 +1,6 @@ import sqlite3 - +import sys +from custom import faker_options_container from helpers import fake2db_logger, str_generator, rnd_id_generator logger, extra_information = fake2db_logger() @@ -20,12 +21,17 @@ class DbConnException(Exception): class Fake2dbSqliteHandler(): faker = Factory.create() - def fake2db_sqlite_initiator(self, number_of_rows, name=None): + def fake2db_sqlite_initiator(self, number_of_rows, name=None, custom=None): '''Main handler for the operation ''' rows = number_of_rows conn = self.database_caller_creator(name) + if custom: + self.custom_db_creator(rows, conn, custom) + conn.close() + sys.exit(0) + self.data_filler_simple_registration(rows, conn) self.data_filler_detailed_registration(rows, conn) self.data_filler_company(rows, conn) @@ -33,6 +39,44 @@ def fake2db_sqlite_initiator(self, number_of_rows, name=None): self.data_filler_customer(rows, conn) conn.close() + 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) + def database_caller_creator(self, name=None): '''creates a sqlite3 db returns the related connection object @@ -52,7 +96,7 @@ def database_caller_creator(self, name=None): raise DbConnException return conn - + def data_filler_simple_registration(self, number_of_rows, conn): '''creates and fills the table with simple regis. information ''' diff --git a/setup.py b/setup.py index 6757da6..e9bd500 100644 --- a/setup.py +++ b/setup.py @@ -3,17 +3,17 @@ setup( name='fake2db', - version='0.4.1', + version='0.5.0', author='Emir Ozer', author_email='emirozer@yandex.com', url='https://github.com/emirozer/fake2db', description= - 'Generate test databases filled with fake data (current support - sqlite, mysql, postgresql, mongodb, redis, couchdb)', + 'Generate test databases filled with fake data (NOW CUSTOM SCHEMA CREATION SUPPORTED)(current support - sqlite, mysql, postgresql, mongodb, redis, couchdb)', long_description=os.path.join(os.path.dirname(__file__), 'README.md'), packages=find_packages(exclude=[]), entry_points={'console_scripts': ['fake2db = fake2db.fake2db:main']}, install_requires=[ - 'fake-factory>=0.4.2', + 'fake-factory>=0.5.3', ], include_package_data=True, classifiers=[