Skip to content
This repository has been archived by the owner on May 31, 2019. It is now read-only.

Commit

Permalink
Add shell commands for python, ipython, bpython, ptpython, ptipython
Browse files Browse the repository at this point in the history
  • Loading branch information
c-bata committed Dec 2, 2016
1 parent 40c1a36 commit 2c5036a
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 21 deletions.
12 changes: 6 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ Run this application using wsgiref:

.. code-block:: console
$ wsgicli main.py app
$ wsgicli run main.py app
Run this application in specified host(default: localhost) and port(default: 8000):

.. code-block:: console
$ wsgicli main.py app -h 0.0.0.0 -p 8080
$ wsgicli run main.py app -h 0.0.0.0 -p 8080
Static files
Expand All @@ -60,7 +60,7 @@ And run:

.. code-block:: console
$ wsgicli main.py app -p 8000 --static --staticroot static --staticdirs static/
$ wsgicli run main.py app -p 8000 --static --staticroot static --staticdirs static/
$ curl http://localhost:8000/static/main.css
.container {
max-width: 980px;
Expand All @@ -72,7 +72,7 @@ Live Reloading

.. code-block:: console
$ wsgicli main.py app --reload
$ wsgicli run main.py app --reload
.. image:: https://raw.githubusercontent.com/kobinpy/wsgicli/master/resources/wsgicli-live-reloading-demo.gif
:alt: live reloading demo
Expand All @@ -86,7 +86,7 @@ Usage is like this:

.. code-block:: console
$ wsgicli main.py app -p 8000 --lineprof
$ wsgicli run main.py app -p 8000 --lineprof
Start: 127.0.0.1:8000
Time unit: 1e-06 [sec]
Expand Down Expand Up @@ -121,7 +121,7 @@ Using vmprof and vmprof-server.

.. code-block:: console
$ wsgicli main.py app -p 8000 --vsprof
$ wsgicli run main.py app -p 8000 --vsprof
refs:

Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
BASE_PATH = os.path.abspath(os.path.dirname(__file__))
README = open(os.path.join(BASE_PATH, 'README.rst')).read()

__version__ = '0.1.0'
__version__ = '0.2.0'
__author__ = 'Masashi Shibata <[email protected]>'
__author_email__ = '[email protected]'
__license__ = 'MIT License'
Expand Down Expand Up @@ -39,5 +39,5 @@
license=__license__,
include_package_data=True,
test_suite='tests',
entry_points={'console_scripts': ['wsgicli = wsgicli:cmd']},
entry_points={'console_scripts': ['wsgicli = wsgicli:cli']},
)
102 changes: 89 additions & 13 deletions wsgicli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,24 @@
from wsgiref.simple_server import make_server


#####################################################################################
# Command Line Interface
#####################################################################################
@click.group()
def cli():
pass


#####################################################################################
# For run server
#####################################################################################
def run_server(app, host, port):
print('Start: {host}:{port}'.format(host=host, port=port))
click.echo('Start: {host}:{port}'.format(host=host, port=port))
httpd = make_server(host, port, app)
httpd.serve_forever()


#####################################################################################
# For reloading server when detected python files changes.
#####################################################################################
EXIT_STATUS_RELOAD = 3


Expand Down Expand Up @@ -112,10 +118,7 @@ def run_live_reloading_server(interval, app, host, port):
sys.exit(3)


#####################################################################################
# Command Line Interface
#####################################################################################
@click.command()
@cli.command()
@click.argument('filepath', nargs=1)
@click.argument('wsgiapp', nargs=1)
@click.option('--host', '-h', type=click.STRING, default='127.0.0.1',
Expand All @@ -130,20 +133,20 @@ def run_live_reloading_server(interval, app, host, port):
help='Directories for static files')
@click.option('--lineprof/--no-lineprof', help='Enable line profiler')
@click.option('--lineprof-file', multiple=True, help='The filename profiled by line-profiler')
def cmd(filepath, wsgiapp, host, port, reload, interval,
def run(filepath, wsgiapp, host, port, reload, interval,
static, static_root, static_dirs, lineprof, lineprof_file):
"""
Runs a development server for WSGI Application.
Usage:
$ wsgicli hello.py app -h 0.0.0.0 -p 5000
$ wsgicli run hello.py app -h 0.0.0.0 -p 5000
$ wsgicli hello.py app --reload
$ wsgicli run hello.py app --reload
$ wsgicli hello.py app --static --static-root /static/ --static-dirs ./static/
$ wsgicli run hello.py app --static --static-root /static/ --static-dirs ./static/
$ wsgicli hello.py app --lineprof
$ wsgicli run hello.py app --lineprof
"""
module = SourceFileLoader('module', filepath).load_module()
app = getattr(module, wsgiapp)
Expand Down Expand Up @@ -171,5 +174,78 @@ def cmd(filepath, wsgiapp, host, port, reload, interval,
run_server(app=app, host=host, port=port)


#####################################################################################
# For run shell
#####################################################################################
def run_plain(imported_objects):
import code
code.interact(local=imported_objects)


def run_ipython(imported_objects):
# Start IPython >= 1.0
from IPython import start_ipython
start_ipython(argv=[], user_ns=imported_objects)


def run_bpython(imported_objects):
from bpython import embed
embed(imported_objects)


def run_ptpython(imported_objects, vi_mode=False):
from ptpython.repl import embed, run_config
history_filename = os.path.expanduser('~/.ptpython_history')
embed(globals=imported_objects, history_filename=history_filename,
vi_mode=vi_mode, configure=run_config)


def run_ptipython(imported_objects, vi_mode=False):
from ptpython.repl import run_config
from ptpython.ipython import embed
history_filename = os.path.expanduser('~/.ptpython_history')
embed(user_ns=imported_objects, history_filename=history_filename,
vi_mode=vi_mode, configure=run_config)


interpreters = {
'python': run_plain,
'ipython': run_ipython,
'bpython': run_bpython,
'ptpython': run_ptpython,
'ptipython': run_ptipython,
}


def run_python(interpreter, imported_objects):
for name, _run_python in interpreters.items():
if interpreter == name:
_run_python(imported_objects)
else:
click.BadParameter('Please select from ' + ', '.join(interpreters.keys()))


@cli.command()
@click.option('-i', '--interpreter', default='python',
help="Select python interpreters (default: plain)"
"Supported interpreters are ipython, bpython, ptpython and pyipython.")
@click.option('--models/--no-models', default=False,
help="Automatically import SQLAlchemy or peewee's table definition.")
def shell(interpreter, models):
"""
Runs a python shell.
Usage:
$ wsgicli shell
$ wsgicli shell -i ipython (or bpython, ptpython, ptipython)
$ wsgicli shell --models
"""
imported_objects = {}
run_python(interpreter, imported_objects)


if __name__ == '__main__':
cmd()
cli()

0 comments on commit 2c5036a

Please sign in to comment.