Skip to content

Instantly share code, notes, and snippets.

@reubano reubano/examples.md
Last active Aug 3, 2017

Embed
What would you like to do?

manage

usage: manage [-m CONFIG_MODE] [-f CONFIG_FILE] [-?]
              {runserver,serve,lint,test,shell} ...

positional arguments:
  {runserver,serve,lint,test,shell}
    runserver           Runs the flask development server Overrides the built-
                        in `runserver` behavior
    serve               Runs the flask development server Alias for
                        `runserver`
    lint                Check style with flake8 linter
    test                Run nose, tox, and script tests
    shell               Runs a Python shell inside Flask application context.

optional arguments:
  -m CONFIG_MODE, --cfgmode CONFIG_MODE
  -f CONFIG_FILE, --cfgfile CONFIG_FILE
  -?, --help            show this help message and exit

manage serve

 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 268-470-607

manage lint

manage test

======================================== test session starts ========================================
platform darwin -- Python 3.6.1, pytest-3.1.3, py-1.4.34, pluggy-0.4.0
rootdir: /Users/reubano/Documents/Projects/alcf/prediction, inifile:
plugins: flask-0.10.0
collected 5 items

src/tests/test_endpoints.py .....

===================================== 5 passed in 0.69 seconds ======================================

manage -m Production serve

 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
#!/usr/bin/env python
"""
Provides script to manage development tasks
"""
from os import path
from subprocess import check_call, CalledProcessError
from urllib.parse import urlsplit
from flask import current_app as app
from flask_script import Server, Manager
from src.factory import create_app
DEF_PORT = 5000
manager = Manager(create_app)
manager.add_option('-m', '--cfgmode', dest='config_mode', default='Development')
manager.add_option('-f', '--cfgfile', dest='config_file', type=path.abspath)
manager.main = manager.run # Needed to do `manage <command>` from the cli
@manager.option('-h', '--host', help='The server host')
@manager.option('-p', '--port', help='The server port')
@manager.option('-t', '--threaded', help='Run multiple threads', action='store_true')
def runserver(**kwargs):
"""Runs the flask development server
Overrides the built-in `runserver` behavior
"""
with app.app_context():
if app.config.get('SERVER'):
parsed = urlsplit(app.config['SERVER'])
host, port = parsed.netloc, parsed.port or DEF_PORT
else:
host, port = app.config['HOST'], DEF_PORT
kwargs.setdefault('host', host)
kwargs.setdefault('port', port)
server = Server(**kwargs)
args = [
app, server.host, server.port, server.use_debugger,
server.use_reloader, server.threaded, server.processes,
server.passthrough_errors]
server(*args)
@manager.option('-h', '--host', help='The server host')
@manager.option('-p', '--port', help='The server port')
@manager.option('-t', '--threaded', help='Run multiple threads', action='store_true')
def serve(**kwargs):
"""Runs the flask development server
Alias for `runserver`
"""
runserver(**kwargs)
@manager.option('-w', '--where', help='Modules to check')
def lint(where):
"""Check style with flake8 linter"""
def_where = ['src', 'manage.py', 'config.py']
extra = where.split(' ') if where else def_where
try:
check_call(['flake8'] + extra)
except CalledProcessError as e:
exit(e.returncode)
@manager.option('-w', '--where', help='test path')
@manager.option(
'-x', '--stop', help='Stop after first error', action='store_true')
@manager.option(
'-f', '--failed', help='Run failed tests', action='store_true')
@manager.option(
'-c', '--cover', help='Add coverage report', action='store_true')
@manager.option(
'-v', '--verbose', help='Use detailed errors', action='store_true')
@manager.option(
'-p', '--parallel', help='Run tests in parallel in multiple processes',
action='store_true')
def test(where, stop, **kwargs):
"""Run nose, tox, and script tests"""
opts = '-x' if stop else ''
opts += '-vv' if kwargs.get('verbose') else ''
opts += '--with-coverage' if kwargs.get('cover') else ''
opts += '--last-failed' if kwargs.get('failed') else ''
opts += '--processes=-1' if kwargs.get('parallel') else ''
opts += '-w %s' % where if where else ''
try:
check_call('python -m pytest src/tests {}'.format(opts).split(' '))
except CalledProcessError as e:
exit(e.returncode)
if __name__ == '__main__':
manager.run()
Flask-Script==2.0.5
manage.py==0.2.10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.