From 43d561f2f993e4ce10bbf5932ba44b6f235c054d Mon Sep 17 00:00:00 2001 From: Jeffrey Paul Date: Mon, 17 Feb 2014 00:46:14 +0100 Subject: [PATCH] refactored script, created setuptools config --- lite2j.py | 49 ---------------------------------- setup.py | 18 +++++++++++++ sqlite2json/__init__.py | 58 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 49 deletions(-) delete mode 100755 lite2j.py create mode 100644 setup.py create mode 100755 sqlite2json/__init__.py diff --git a/lite2j.py b/lite2j.py deleted file mode 100755 index 3dd1907..0000000 --- a/lite2j.py +++ /dev/null @@ -1,49 +0,0 @@ -import sqlite3 -import sys -import json -import base64 - -def show_usage(): - print 'Usage:\n\tlite2j sqlitefile.db --exclude=table_name --exclude=table_name' - -exclude_tables = [] - -try: - for i, token in enumerate(sys.argv): - if i < 1: - continue - if i == 1: - db_file = token - if i > 1: - exclude_tables.append(token.split('--exclude=')[1]) - assert(db_file) -except (IndexError, NameError): - show_usage() - sys.exit(127) - -exclude_tables = set(exclude_tables) - -# Make sure blobs are base64 encoded -sqlite3.register_converter('BLOB', base64.b64encode) - -conn = sqlite3.connect(db_file, detect_types=sqlite3.PARSE_DECLTYPES) -cursor = conn.cursor() - -def get_table_list(cursor, exclude_tables=exclude_tables): - cursor.execute('SELECT * FROM main.sqlite_master WHERE type="table"') - return set(row[1] for row in cursor.fetchall()) - exclude_tables - -def get_column_names(cursor, table_name): - cursor.execute('SELECT * FROM %s' % table_name) - return [c[0] for c in cursor.description] - -def get_table(cursor, table_name): - column_names = get_column_names(cursor,table_name) - cursor.execute('SELECT * FROM main.%s' % table_name) - return [dict(zip(column_names, row)) for row in cursor.fetchall()] - -def get_tables(cursor): - table_list = get_table_list(cursor) - return [{table_name : get_table(cursor, table_name) for table_name in table_list}] - -print json.dumps(get_tables(cursor)) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..343ba15 --- /dev/null +++ b/setup.py @@ -0,0 +1,18 @@ +from setuptools import setup + +setup( + name='sqlite2json', + version='1.0.0', + author='John gerlock', + author_email='john@pikkey.com', + packages=['sqlite2json'], + url='https://github.com/john-g-g/sqlite2json', + license=open('LICENSE').read(), + description='SQLite to JSON converter', + long_description=open('README.md').read(), + entry_points = { + 'console_scripts': [ + 'sqlite2json = sqlite2json:main' + ] + } +) diff --git a/sqlite2json/__init__.py b/sqlite2json/__init__.py new file mode 100755 index 0000000..243c610 --- /dev/null +++ b/sqlite2json/__init__.py @@ -0,0 +1,58 @@ +import sqlite3 +import sys +import json +import base64 + +def main(): + exclude_tables = [] + + try: + for i, token in enumerate(sys.argv): + if i < 1: + continue + if i == 1: + db_file = token + if i > 1: + exclude_tables.append(token.split('--exclude=')[1]) + assert(db_file) + except (IndexError, NameError): + show_usage() + sys.exit(127) + + exclude_tables = set(exclude_tables) + print sqlite2json( + db_file=db_file, + exclude_tables=exclude_tables + ) + +def show_usage(): + print 'Usage:\n\tsqlite2json sqlitefile.db --exclude=table_name --exclude=table_name' + +def sqlite2json(exclude_tables=None,db_file=None): + # Make sure blobs are base64 encoded + sqlite3.register_converter('BLOB', base64.b64encode) + + conn = sqlite3.connect(db_file, detect_types=sqlite3.PARSE_DECLTYPES) + cursor = conn.cursor() + + return json.dumps(get_tables(cursor)) + +def get_table_list(cursor, exclude_tables=exclude_tables): + cursor.execute('SELECT * FROM main.sqlite_master WHERE type="table"') + return set(row[1] for row in cursor.fetchall()) - exclude_tables + +def get_column_names(cursor, table_name): + cursor.execute('SELECT * FROM %s' % table_name) + return [c[0] for c in cursor.description] + +def get_table(cursor, table_name): + column_names = get_column_names(cursor,table_name) + cursor.execute('SELECT * FROM main.%s' % table_name) + return [dict(zip(column_names, row)) for row in cursor.fetchall()] + +def get_tables(cursor): + table_list = get_table_list(cursor) + return [{table_name : get_table(cursor, table_name) for table_name in table_list}] + +if __name__ == "__main__": + main()