From d98b3fff7d0b4e9a6af5cc7a65c547de85516d14 Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Thu, 11 Feb 2016 19:23:29 -0500 Subject: [PATCH] v0.3.0 --- records.py | 25 +++++++++++++++++-------- setup.py | 10 ++++++---- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/records.py b/records.py index 1ed9de4..3a16a89 100644 --- a/records.py +++ b/records.py @@ -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 [] [--url=] [--params ...] + records [--url=] [--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[''] not in supported_formats: + arguments[''].insert(0, arguments['']) + arguments[''] = None + # Create the Database. db = Database(arguments['--url']) query = arguments[''] + params = arguments[''] # 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() diff --git a/setup.py b/setup.py index a5c54fe..94515a2 100644 --- a/setup.py +++ b/setup.py @@ -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',