Skip to content

Commit

Permalink
Add tox+isort+black
Browse files Browse the repository at this point in the history
Update .gitignore for this changes
  • Loading branch information
coolhacker committed Dec 3, 2019
1 parent 410d411 commit 00b4b5c
Show file tree
Hide file tree
Showing 11 changed files with 457 additions and 252 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ dump.rdb
/.direnv
.VScode
.DS_Store
._*
._*
.tox/

.python-version
3 changes: 2 additions & 1 deletion fabfile.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
"""Common development operations."""

from fabric.api import abort, local, lcd, env, settings, hide, quiet, task
import os
import os.path

from fabric.api import abort, env, hide, lcd, local, quiet, settings, task


def _relative_to_fabfile(*path):
return os.path.join(os.path.dirname(env.real_fabfile), *path)
Expand Down
2 changes: 2 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
black~=19.10b0
isort~=4.3.21
2 changes: 1 addition & 1 deletion rq_dashboard/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .cli import main


if __name__ == '__main__':
if __name__ == "__main__":
main()
243 changes: 149 additions & 94 deletions rq_dashboard/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import logging
import os
import sys
from urllib.parse import urlunparse, quote as urlquote
from urllib.parse import quote as urlquote, urlunparse

import click
from flask import Flask, Response, request
Expand All @@ -12,25 +12,25 @@
from .web import blueprint


def add_basic_auth(blueprint, username, password, realm='RQ Dashboard'):
def add_basic_auth(blueprint, username, password, realm="RQ Dashboard"):
"""Add HTTP Basic Auth to a blueprint.
Note this is only for casual use!
"""

@blueprint.before_request
def basic_http_auth(*args, **kwargs):
auth = request.authorization
if (auth is None or auth.password != password or
auth.username != username):
if auth is None or auth.password != password or auth.username != username:
return Response(
'Please login',
"Please login",
401,
{'WWW-Authenticate': 'Basic realm="{}"'.format(realm)})
{"WWW-Authenticate": 'Basic realm="{}"'.format(realm)},
)


def make_flask_app(config, username, password, url_prefix,
compatibility_mode=True):
def make_flask_app(config, username, password, url_prefix, compatibility_mode=True):
"""Return Flask app with default configuration and registered blueprint."""
app = Flask(__name__)

Expand All @@ -42,8 +42,8 @@ def make_flask_app(config, username, password, url_prefix,
app.config.from_object(importlib.import_module(config))

# Override from a configuration file in the env variable, if present.
if 'RQ_DASHBOARD_SETTINGS' in os.environ:
app.config.from_envvar('RQ_DASHBOARD_SETTINGS')
if "RQ_DASHBOARD_SETTINGS" in os.environ:
app.config.from_envvar("RQ_DASHBOARD_SETTINGS")

# Optionally add basic auth to blueprint and register with app.
if username:
Expand All @@ -55,66 +55,118 @@ def make_flask_app(config, username, password, url_prefix,

@click.command()
@click.option(
'-b', '--bind', default='0.0.0.0',
help='IP or hostname on which to bind HTTP server')
@click.option(
'-p', '--port', default=9181, type=int,
help='Port on which to bind HTTP server')
@click.option(
'--url-prefix', default='',
help='URL prefix e.g. for use behind a reverse proxy')
@click.option(
'--username', default=None,
help='HTTP Basic Auth username (not used if not set)')
@click.option(
'--password', default=None,
help='HTTP Basic Auth password')
@click.option(
'-c', '--config', default=None,
help='Configuration file (Python module on search path)')
@click.option(
'-H', '--redis-host', default=None, hidden=True,
help='[DEPRECATED] IP address or hostname of Redis server. Use --redis-url instead')
@click.option(
'-P', '--redis-port', default=None, type=int, hidden=True,
help='[DEPRECATED] Port of Redis server. Use --redis-url instead')
@click.option(
'--redis-password', default=None, hidden=True,
help='[DEPRECATED] Password for Redis server. Use --redis-url instead')
@click.option(
'-D', '--redis-database', default=None, type=int, hidden=True,
help='[DEPRECATED] Database of Redis server, Use --redis-url instead')
@click.option(
'-u', '--redis-url', default=None, multiple=True,
help='Redis URL. Can be specified multiple times. Default: redis://127.0.0.1:6379')
@click.option(
'--redis-sentinels', default=None, hidden=True,
help='[DEPRECATED] List of redis sentinels. Use --redis-url instead')
@click.option(
'--redis-master-name', default=None, hidden=True,
help='[DEPRECATED] Name of redis master. Only needed when using sentinels. Use --redis-url instead')
@click.option(
'--poll-interval', '--interval', 'poll_interval', default=None, type=int,
help='Refresh interval in ms')
@click.option(
'--extra-path', default='.', multiple=True,
help='Append specified directories to sys.path')
@click.option(
'--web-background', default='black',
help='Background of the web interface')
@click.option(
'--delete-jobs', default=False, help='Delete jobs instead of cancel')
@click.option(
'--debug/--normal', default=False, help='Enter DEBUG mode')
@click.option(
'-v', '--verbose', is_flag=True, default=False, help='Enable verbose logging')
"-b",
"--bind",
default="0.0.0.0",
help="IP or hostname on which to bind HTTP server",
)
@click.option(
"-p", "--port", default=9181, type=int, help="Port on which to bind HTTP server"
)
@click.option(
"--url-prefix", default="", help="URL prefix e.g. for use behind a reverse proxy"
)
@click.option(
"--username", default=None, help="HTTP Basic Auth username (not used if not set)"
)
@click.option("--password", default=None, help="HTTP Basic Auth password")
@click.option(
"-c",
"--config",
default=None,
help="Configuration file (Python module on search path)",
)
@click.option(
"-H",
"--redis-host",
default=None,
hidden=True,
help="[DEPRECATED] IP address or hostname of Redis server. Use --redis-url instead",
)
@click.option(
"-P",
"--redis-port",
default=None,
type=int,
hidden=True,
help="[DEPRECATED] Port of Redis server. Use --redis-url instead",
)
@click.option(
"--redis-password",
default=None,
hidden=True,
help="[DEPRECATED] Password for Redis server. Use --redis-url instead",
)
@click.option(
"-D",
"--redis-database",
default=None,
type=int,
hidden=True,
help="[DEPRECATED] Database of Redis server, Use --redis-url instead",
)
@click.option(
"-u",
"--redis-url",
default=None,
multiple=True,
help="Redis URL. Can be specified multiple times. Default: redis://127.0.0.1:6379",
)
@click.option(
"--redis-sentinels",
default=None,
hidden=True,
help="[DEPRECATED] List of redis sentinels. Use --redis-url instead",
)
@click.option(
"--redis-master-name",
default=None,
hidden=True,
help="[DEPRECATED] Name of redis master. Only needed when using sentinels. Use --redis-url instead",
)
@click.option(
"--poll-interval",
"--interval",
"poll_interval",
default=None,
type=int,
help="Refresh interval in ms",
)
@click.option(
"--extra-path",
default=".",
multiple=True,
help="Append specified directories to sys.path",
)
@click.option(
"--web-background", default="black", help="Background of the web interface"
)
@click.option("--delete-jobs", default=False, help="Delete jobs instead of cancel")
@click.option("--debug/--normal", default=False, help="Enter DEBUG mode")
@click.option(
"-v", "--verbose", is_flag=True, default=False, help="Enable verbose logging"
)
def run(
bind, port, url_prefix, username, password,
config,
redis_host, redis_port, redis_password, redis_database, redis_url,
redis_sentinels, redis_master_name,
poll_interval, extra_path, web_background, debug, delete_jobs,
verbose):
bind,
port,
url_prefix,
username,
password,
config,
redis_host,
redis_port,
redis_password,
redis_database,
redis_url,
redis_sentinels,
redis_master_name,
poll_interval,
extra_path,
web_background,
debug,
delete_jobs,
verbose,
):
"""Run the RQ Dashboard Flask server.
All configuration can be set on the command line or through environment
Expand All @@ -129,63 +181,66 @@ def run(
if extra_path:
sys.path += list(extra_path)

click.echo('RQ Dashboard version {}'.format(VERSION))
click.echo("RQ Dashboard version {}".format(VERSION))
app = make_flask_app(config, username, password, url_prefix)
app.config['DEPRECATED_OPTIONS'] = []
app.config["DEPRECATED_OPTIONS"] = []
if redis_url:
app.config['RQ_DASHBOARD_REDIS_URL'] = redis_url
app.config["RQ_DASHBOARD_REDIS_URL"] = redis_url
else:
app.config['RQ_DASHBOARD_REDIS_URL'] = 'redis://127.0.0.1:6379'
app.config["RQ_DASHBOARD_REDIS_URL"] = "redis://127.0.0.1:6379"
if redis_host:
app.config['DEPRECATED_OPTIONS'].append('--redis-host')
app.config["DEPRECATED_OPTIONS"].append("--redis-host")
if redis_port:
app.config['DEPRECATED_OPTIONS'].append('--redis-port')
app.config["DEPRECATED_OPTIONS"].append("--redis-port")
if redis_password:
app.config['DEPRECATED_OPTIONS'].append('--redis-password')
app.config["DEPRECATED_OPTIONS"].append("--redis-password")
if redis_database:
app.config['DEPRECATED_OPTIONS'].append('--redis-database')
app.config["DEPRECATED_OPTIONS"].append("--redis-database")
if redis_sentinels:
app.config['DEPRECATED_OPTIONS'].append('--redis-sentinels')
app.config["DEPRECATED_OPTIONS"].append("--redis-sentinels")
if redis_master_name:
app.config['DEPRECATED_OPTIONS'].append('--redis-master-name')
app.config["DEPRECATED_OPTIONS"].append("--redis-master-name")
if poll_interval:
app.config['RQ_DASHBOARD_POLL_INTERVAL'] = poll_interval
app.config["RQ_DASHBOARD_POLL_INTERVAL"] = poll_interval
if web_background:
app.config["RQ_DASHBOARD_WEB_BACKGROUND"] = web_background
if delete_jobs:
app.config["RQ_DASHBOARD_DELETE_JOBS"] = delete_jobs
# Conditionally disable Flask console messages
# See: https://stackoverflow.com/questions/14888799
log = logging.getLogger('werkzeug')
log = logging.getLogger("werkzeug")
if verbose:
log.setLevel(logging.DEBUG)
else:
log.setLevel(logging.ERROR)
log.error(" * Running on {}:{}".format(bind, port))

if app.config['DEPRECATED_OPTIONS'] and not redis_url:
if app.config["DEPRECATED_OPTIONS"] and not redis_url:
# redis+sentinel://[:password@]host:port[,host2:port2,...][/service_name[/db]][?param1=value1[&param2=value=2&...]]
scheme = 'redis+sentinel' if redis_sentinels else 'redis'
scheme = "redis+sentinel" if redis_sentinels else "redis"
if redis_sentinels:
netloc = redis_sentinels
else:
netloc = redis_host or 'localhost'
netloc = redis_host or "localhost"
if redis_port:
netloc = '%s:%s' % (netloc, redis_port)
netloc = "%s:%s" % (netloc, redis_port)
if redis_password:
netloc = urlquote(redis_password) + '@' + netloc
path = ''
netloc = urlquote(redis_password) + "@" + netloc
path = ""
if redis_master_name:
path += '/%s' % urlquote(redis_master_name)
path += "/%s" % urlquote(redis_master_name)
if redis_database:
path += '/%s' % redis_database
url = urlunparse((scheme, netloc, path, '', '', ''))
log.error('Use --redis-url=%s configuration option '
'instead of specifying host, port and other parameters separately', url)
app.config['RQ_DASHBOARD_REDIS_URL'] = url
path += "/%s" % redis_database
url = urlunparse((scheme, netloc, path, "", "", ""))
log.error(
"Use --redis-url=%s configuration option "
"instead of specifying host, port and other parameters separately",
url,
)
app.config["RQ_DASHBOARD_REDIS_URL"] = url

app.run(host=bind, port=port, debug=debug)


def main():
run(auto_envvar_prefix='RQ_DASHBOARD')
run(auto_envvar_prefix="RQ_DASHBOARD")
Loading

0 comments on commit 00b4b5c

Please sign in to comment.