Skip to content

Commit

Permalink
switch from argparse to docopt
Browse files Browse the repository at this point in the history
  • Loading branch information
larsmans committed Feb 25, 2015
1 parent f7ba425 commit bb10b63
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 50 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ celery>=3.0.0
chardet
corenlp_xml
cytoolz
docopt
elasticsearch>=1.2.0,<2.0.0
flask>=0.10.1
gensim
Expand Down
1 change: 1 addition & 0 deletions requirements3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ celery>=3.0.0
chardet
corenlp_xml
cytoolz
docopt
elasticsearch>=1.2.0,<2.0.0
flask>=0.10.1
gensim
Expand Down
30 changes: 18 additions & 12 deletions xtas/make_config/__main__.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
# Generate xtas configuration file.
"""Generate xtas configuration file.
Usage:
make_config [-o file]
if __name__ == '__main__':
import argparse
from os.path import abspath, dirname, join
from shutil import copyfileobj
import sys
Options:
-h, --help show this help message and exit
-o file Write output to <file> [default: xtas_config.py].
"""

from os.path import abspath, dirname, join
from shutil import copyfileobj
import sys

from docopt import docopt

parser = argparse.ArgumentParser(description=
'Make xtas configuration file.')
parser.add_argument('-o', dest='output', default='xtas_config.py')
args = parser.parse_args()

if __name__ == '__main__':
args = docopt(__doc__)

with open(join(dirname(__file__), '..', '_defaultconfig.py')) as default:
print("Generating configuration file at %r" % args['-o'])
# XXX in Python 3, we can make this safer by opening with 'x'
with open(args.output, 'w') as out:
with open(args['-o'], 'w') as out:
copyfileobj(default, out)
print("Generated configuration file at %r" % args.output)
39 changes: 23 additions & 16 deletions xtas/webserver/__main__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
"""xtas web server/REST API
Usage:
webserver [options]
Options:
-h, --help show this help message and exit
--debug Enable debugging mode.
--host=HOST Host to listen on [default: 127.0.0.1].
--port=PORT Port to listen on [default: 5000].
--threads=THREADS Number of threads [default: 5].
"""

from __future__ import absolute_import

import argparse
import json
import logging
from pprint import pprint
Expand All @@ -9,6 +22,9 @@
from celery import chain
from celery import __version__ as celery_version
import celery.result

from docopt import docopt

from flask import Flask, Response, abort, request
from flask import __version__ as flask_version

Expand Down Expand Up @@ -102,26 +118,17 @@ def show_tasks():


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="xtas web server")
parser.add_argument('--debug', dest='debug', action='store_true',
help='Enable debugging mode.')
parser.add_argument('--host', dest='host', default='127.0.0.1',
help='Host to listen on.')
parser.add_argument('--port', dest='port', default=5000, type=int,
help='Port to listen on.')
parser.add_argument('--threads', dest='threads', default=5, type=int,
help='Number of threads.')
args = parser.parse_args()

loglevel = logging.DEBUG if args.debug else logging.INFO
args = docopt(__doc__)

loglevel = logging.DEBUG if args.get('--debug') else logging.INFO
logging.basicConfig(level=loglevel)

app.debug = args.debug
app.debug = args.get('--debug')
print("xtas %s REST endpoint" % __version__)
if app.debug:
print("Serving tasks:")
pprint(list(taskq.tasks.keys()))
http_server = HTTPServer(WSGIContainer(app))
http_server.bind(args.port, address=args.host)
http_server.start(args.threads)
http_server.bind(args['--port'], address=args['--host'])
http_server.start(int(args['--threads']))
IOLoop.instance().start()
50 changes: 28 additions & 22 deletions xtas/worker.py
Original file line number Diff line number Diff line change
@@ -1,46 +1,52 @@
"""xtas worker
Usage:
xtas.worker [options]
Options:
-h, --help show this help message and exit
--loglevel=LOGLEVEL Set minimum severity level of logger, e.g. DEBUG,
ERROR, INFO. [default: WARNING].
--pidfile=PIDFILE Write PID of worker to PIDFILE (not removed at
shutdown!).
--version Print version info and exit.
"""

from __future__ import print_function

from argparse import ArgumentParser
import logging
import os
import sys

from celery.bin.worker import worker
from docopt import docopt

from . import __version__


if __name__ == '__main__':
parser = ArgumentParser()
parser.add_argument('--loglevel', dest='loglevel', default='WARN',
help='Set minimum severity level of logger,'
' e.g. DEBUG, ERROR, INFO. Default=WARNING.')
parser.add_argument('--pidfile', dest='pidfile',
help='Write PID of worker to PIDFILE'
' (not removed at shutdown!).')
parser.add_argument('--version', dest='version', const=True,
action='store_const',
help='Print version info and exit.')
args = parser.parse_args()

logging.basicConfig(level=args.loglevel.upper())

if args.pidfile is not None:
args = docopt(__doc__)

loglevel = args['--loglevel'].upper()
logging.basicConfig(level=loglevel)

pidfilepath = args.get('--pidfile')
if pidfilepath is not None:
# XXX use 'x' in Python 3
with open(args.pidfile, 'w') as pidfile:
logging.info('Writing PID to %s' % args.pidfile)
with open(pidfilepath, 'w') as pidfile:
logging.info('Writing PID to %s' % pidfilepath)
pidfile.write(str(os.getpid()))

# This import must be done after we've parsed the cmdline args and set
# the logging level.
from .core import app

if args.version:
if args.get('--version'):
print("xtas %s" % __version__)
print("Celery", end=" ")
app.worker_main(["--version"])
sys.exit()

# XXX app.worker_main is prettier but doesn't seem to reply to --loglevel.
w = worker(app=app)
w.run(loglevel=args.loglevel)
# XXX app.worker_main is prettier but doesn't seem to respond to --loglevel
worker(app=app).run(loglevel=loglevel)

0 comments on commit bb10b63

Please sign in to comment.