This commit is contained in:
2016-02-11 19:23:29 -05:00
parent 29914ade6d
commit d98b3fff7d
2 changed files with 23 additions and 12 deletions
+17 -8
View File
@@ -5,6 +5,7 @@ from datetime import datetime
import tablib
import psycopg2
from docopt import docopt
from psycopg2.extras import register_hstore, NamedTupleCursor
from psycopg2.extensions import cursor as _cursor
@@ -213,6 +214,7 @@ class Database(object):
can, optionally, be provided. Returns a ResultSet, which can be
iterated over to get result rows as dictionaries.
"""
# Execute the given query.
c = self.db.cursor()
c.execute(query, params)
@@ -255,11 +257,12 @@ def _reduce_datetimes(row):
return row
cli_docs ="""Records: SQL for Humans™
def cli():
cli_docs ="""Records: SQL for Humans™
A Kenneth Reitz project.
Usage:
records <query> [<format>] [--url=<url>] [--params <params>...]
records <query> <format> [--url=<url>] [--params <params>...]
records (-h | --help)
Options:
@@ -284,26 +287,30 @@ Notes:
Cake:
✨ 🍰 ✨
"""
from docopt import docopt
"""
supported_formats = 'csv, tsv, json, yaml, html, xls, xlsx, dbf, latex, ods'
if __name__ == '__main__':
# Parse the command-line arguments.
arguments = docopt(cli_docs)
# Cleanup docopt parsing errors.
if arguments['--params'] and arguments['<format>'] not in supported_formats:
arguments['<params>'].insert(0, arguments['<format>'])
arguments['<format>'] = None
# Create the Database.
db = Database(arguments['--url'])
query = arguments['<query>']
params = arguments['<params>']
# Execute the query, if it is a found file.
if os.path.isfile(query):
rows = db.query_file(query)
rows = db.query_file(query, params)
# Execute the query, if it appears to be a query string.
elif len(query.split()) > 2:
rows = db.query(query)
rows = db.query(query, params)
# Otherwise, say the file wasn't found.
else:
@@ -317,6 +324,8 @@ if __name__ == '__main__':
print(rows.dataset)
if __name__ == '__main__':
cli()
+6 -4
View File
@@ -3,7 +3,6 @@
import os
import re
import sys
from codecs import open
from setuptools import setup
@@ -15,8 +14,8 @@ if sys.argv[-1] == 'publish':
os.system('python setup.py bdist_wheel upload --universal')
sys.exit()
requires = ['psycopg2', 'tablib']
version = '0.2.0'
requires = ['psycopg2', 'tablib', 'docopt']
version = '0.3.0'
def read(f):
return open(f, encoding='utf-8').read()
@@ -32,6 +31,9 @@ setup(
py_modules=['records'],
package_data={'': ['LICENSE']},
include_package_data=True,
entry_points = {
'console_scripts': ['records=records:cli'],
},
install_requires=requires,
license='ISC',
zip_safe=False,
@@ -39,7 +41,7 @@ setup(
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Natural Language :: English',
'License :: OSI Approved :: Apache Software License',
'License :: OSI Approved :: ISC License (ISCL)',
'Programming Language :: Python',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',