Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add a vaurienctl command-line tool.
Which can be used in combination with the vaurien http server to change the handlers on the fly.
  • Loading branch information
almet committed Oct 29, 2012
1 parent 413aaad commit 592685f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -33,4 +33,5 @@
entry_points="""
[console_scripts]
vaurien = vaurien.run:main
vaurienctl = vaurien.client:main
""")
40 changes: 39 additions & 1 deletion vaurien/client.py
@@ -1,4 +1,5 @@
from contextlib import contextmanager
from urlparse import urlparse
import requests


Expand All @@ -7,7 +8,9 @@ def __init__(self, host='localhost', port=8080, scheme='http'):
self.host = host
self.port = port
self.scheme = scheme
self.handler_url = '%s://%s:%d/handler' % (scheme, host, port)
self.root_url = '%s://%s:%s' % (scheme, host, port)
self.handler_url = self.root_url + '/handler'
self.list_handlers_url = self.root_url + '/handlers'

def set_handler(self, handler):
res = requests.post(self.handler_url, data=handler)
Expand All @@ -20,6 +23,12 @@ def get_handler(self):
raise ValueError(res.content)
return res.content

def list_handlers(self):
res = requests.get(self.list_handlers_url)
if res.status_code != 200:
raise ValueError(res.content)
return res.json['handlers']

@contextmanager
def with_handler(self, handler):
current = self.get_handler()
Expand All @@ -28,3 +37,32 @@ def with_handler(self, handler):
yield
finally:
self.set_handler(current)


def main():
"""Command-line tool to change the handler that's being used by vaurien"""
import argparse
parser = argparse.ArgumentParser(description='Change the vaurien handler')
parser.add_argument('action', help='The action you want to do.',
choices=['list-handlers', 'set-handler', 'get-handler'])
parser.add_argument('handler', nargs='?',
help='The vaurien handler to set for the next calls')
parser.add_argument('--host', dest='host', default='http://localhost:8080',
help='The host to use. Provide the scheme.')

args = parser.parse_args()
parts = urlparse(args.host)
scheme = parts.scheme
host, port = parts.netloc.split(':', -1)
client = Client(host, port, scheme)
if args.action == 'list-handlers':
print client.list_handlers()
elif args.action == 'set-handler':
try:
client.set_handler(args.handler)
print 'Handler changed to "%s"' % args.handler
except ValueError:
print 'The request failed. Please use one of %s' %\
', '.join(client.list_handlers())
elif args.action == 'get-handler':
print client.get_handler()

0 comments on commit 592685f

Please sign in to comment.