Skip to content

Commit

Permalink
Move to using click rather than docopt
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Knupp committed May 13, 2014
1 parent 0dd5cac commit 5c4b707
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 61 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ WTForms==1.0.5
Werkzeug==0.9.4
itsdangerous==0.24
wsgiref==0.1.2
docopt==0.6.1
click==0.7
sphinx-rtd-theme==0.1.6
86 changes: 28 additions & 58 deletions sandman/sandmanctl.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,43 @@
"""Script to run sandman via command line
Usage:
sandmanctl.py URI [--show-pks --generate-pks] [--host=host] [--port=port] [--version]
sandmanctl.py --version
Start sandman and connect to database at URI
Supported database dialects:
[PostgreSQL]
[MySQL]
[Oracle]
[Microsoft SQL Server]
[SQLite]
[Drizzle]
[Firebird]
[Sybase]
External dialects and packages:
[IBM DB2]: ibm_db_sa
[SAP Sybase SQL Anywhere]: sqlalchemy-sqlany
[MonetDB]: sqlalchemy-monetdb
Arguments:
URI RFC-1738 style database URI
Options:
-h --help Show this screen.
-s --show-primary-keys Display primary key columns in the admin
interface
-g --generate-pks Use the combination of all columns as the
primary key for tables without primary keys
(primary keys are required by the mapping
engine). Implies --primary-keys
-v --version Display current version of sandman and exit
--host <host> Host to run sandmanctl on
--port <port> Port to run sandmanctl on
'postgresql+psycopg2://scott:tiger@localhost/test'
'postgresql+psycopg2://scott:tiger@localhost/test' --generate-pks --host localhost --port 8080
'sqlite+pysqlite:///relative/path/to/db.db'
'sqlite:////absolute/path/to/db.db'
"""
"""Script to run sandman via command line."""
from __future__ import absolute_import
import sys

from docopt import docopt
import click

from sandman import app
from sandman.model import activate

def main(test_options=None):
"""Main entry point for script."""
def print_version(ctx, value):
"""Print the current version of sandman and exit."""
if not value:
return
import pkg_resources
version = None
try:
version = pkg_resources.get_distribution('sandman').version
finally:
del pkg_resources

options = test_options or docopt(__doc__, version=version)
URI = options['URI']
app.config['SQLALCHEMY_DATABASE_URI'] = options['URI']
app.config['SANDMAN_GENERATE_PKS'] = options['--generate-pks'] or False
app.config['SANDMAN_SHOW_PKS'] = options['--show-pks'] or False
host = options.get('--host') or '0.0.0.0'
port = options.get('--port') or 5000
click.echo(version)
ctx.exit()


@click.command()
@click.option('--generate-pks/--no-generate-pks', default=False,
help='Have sandman generate primary keys for tables without one')
@click.option('--show-pks/--no-show-pks', default=False,
help='Have sandman show primary key columns in the admin view')
@click.option('--host', default='0.0.0.0',
help='Hostname of database server to connect to')
@click.option('--port', default=8080,
help='Port of database server to connect to')
@click.option('--version', is_flag=True,
callback=print_version, expose_value=False, is_eager=True)
@click.argument('URI', metavar='<URI>')
def run(generate_pks, show_pks, host, port, uri):
"""Connect sandman to <URI> and start the API server/admin
interface."""
app.config['SQLALCHEMY_DATABASE_URI'] = uri
app.config['SANDMAN_GENERATE_PKS'] = generate_pks
app.config['SANDMAN_SHOW_PKS'] = show_pks
app.config['SERVER_HOST'] = host
app.config['SERVER_PORT'] = port
activate(name='sandmanctl')
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ def run_tests(self):
'Flask-Admin>=1.0.6',
'Flask-HTTPAuth>=2.2.1',
'docopt>=0.6.1',
'sphinx-rtd-theme',
'click',
#'sphinx-rtd-theme',
],
cmdclass={'test': PyTest},
author_email='jeff@jeffknupp.com',
description='Automated REST APIs for existing database-driven systems',
long_description=long_description,
entry_points={
'console_scripts': [
'sandmanctl = sandman.sandmanctl:main',
'sandmanctl = sandman.sandmanctl:run',
],
},
packages=['sandman', 'sandman.model'],
Expand Down
Binary file modified tests/data/chinook.sqlite3
Binary file not shown.

0 comments on commit 5c4b707

Please sign in to comment.