Skip to content

Commit

Permalink
Initial attempt at simplifying CLI object supportO
Browse files Browse the repository at this point in the history
  • Loading branch information
timothycrosley committed Sep 22, 2016
1 parent 002e308 commit 34d8c46
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 43 deletions.
59 changes: 19 additions & 40 deletions hug/route.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ def __call__(self, method_or_class):

for argument in dir(instance):
argument = getattr(instance, argument, None)
routes = getattr(argument, '_hug_http_routes', None)
if routes:
for route in routes:
http(**self.where(**route).route)(argument)

http_routes = getattr(handler, '_hug_http_routes', ())
for route in http_routes:
http(**router.accept(method).where(**route).route)(handler)

cli_routes = getattr(argument, '_hug_cli_routes', ())
for route in cli_routes:
cli(**router.accept(method).where(**route).route)(handler)

return method_or_class

Expand All @@ -73,48 +77,23 @@ def decorator(class_definition):
for method in HTTP_METHODS:
handler = getattr(instance, method.lower(), None)
if handler:
routes = getattr(handler, '_hug_http_routes', None)
if routes:
for route in routes:
http_routes = getattr(handler, '_hug_http_routes', ())
cli_routes = getattr(argument, '_hug_cli_routes', ())
if http_routes or cli_routes:
for route in http_routes:
http(**router.accept(method).where(**route).route)(handler)
for route in cli_routes:
cli(**router.accept(method).where(**route).route)(handler)
else:
http(**router.accept(method).route)(handler)
return class_definition
return decorator


class CLIObject(cli):
"""Defines a router for objects intended to be exposed to the command line"""

def __init__(self, name=None, version=None, doc=None, **kwargs):
super().__init__(version=version, doc=doc, **kwargs)
self.name = name

@property
def cli(self):
return getattr(self.route.get('api', None), 'cli', None)

def __call__(self, method_or_class):
if isinstance(method_or_class, (MethodType, FunctionType)):
routes = getattr(method_or_class, '_hug_cli_routes', [])
routes.append(self.route)
method_or_class._hug_cli_routes = routes
return method_or_class

instance = method_or_class
if isinstance(method_or_class, type):
instance = method_or_class()

if not 'api' in self.route:
self.route['api'] = hug.api.API(self.name or self.__class__.__name__)
for argument in dir(instance):
argument = getattr(instance, argument, None)
routes = getattr(argument, '_hug_cli_routes', None)
if routes:
for route in routes:
cli(**self.where(**route).route)(argument)

instance.__class__.cli = self.cli
def cli(self, method):
"""Registers a method on an Object as a CLI route"""
routes = getattr(method_or_class, '_hug_cli_routes', [])
routes.append(self.route)
method_or_class._hug_cli_routes = routes
return method_or_class


Expand Down
6 changes: 3 additions & 3 deletions tests/test_route.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ class TestCLIObject(object):

def test_commands(self):
"""Basic operation test"""
@hug.cli_object(name='git', version='1.0.0')
@hug.object(name='git', version='1.0.0')
class GIT(object):
"""An example of command like calls via an Object"""

@hug.cli_object()
@hug.object.cli
def push(self, branch='master'):
return 'Pushing {}'.format(branch)

@hug.cli_object()
@hug.object.cli
def pull(self, branch='master'):
return 'Pulling {}'.format(branch)

Expand Down

0 comments on commit 34d8c46

Please sign in to comment.