Skip to content

Commit

Permalink
tweak how callbacks are registered
Browse files Browse the repository at this point in the history
  • Loading branch information
willforde committed Apr 9, 2018
1 parent b40fd23 commit f15bf9e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 31 deletions.
2 changes: 1 addition & 1 deletion script.module.codequick/lib/codequick/listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ def next_page(cls, *args, **kwargs):
>>> item.next_page(url="http://example.com/videos?page2")
"""
# Current running callback
callback = dispatcher.current_route.org_callback
callback = dispatcher.current_route.decorated_callback

# Add support params to callback params
kwargs["_updatelisting_"] = True if u"_nextpagecount_" in dispatcher.params else False
Expand Down
55 changes: 26 additions & 29 deletions script.module.codequick/lib/codequick/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,37 @@ class Route(object):
"""
Handle callback route data.
:param parent: The parent class that will handle the response from callback.
:param callback: The callable callback function.
:param org_callback: The decorated func/class.
:param parent: The parent class that will handle the response from callback.
:param str path: The route path to func/class.
:ivar bool is_playable: True if callback is playable, else False.
:ivar bool is_folder: True if callback is a folder, else False.
:ivar org_callback: The decorated func/class.
:ivar decorated_callback: The decorated func/class.
:ivar callback: The callable callback function.
:ivar parent: The parent class that will handle the response from callback.
:ivar str path: The route path to func/class.
"""
__slots__ = ("parent", "callback", "org_callback", "path", "is_playable", "is_folder")
__slots__ = ("parent", "callback", "decorated_callback", "path", "is_playable", "is_folder")

def __init__(self, callback, parent, path):
# Register a class callback
if inspect.isclass(callback):
if hasattr(callback, "run"):
self.parent = parent = callback
self.callback = callback.run
callback.test = staticmethod(self.unittest_caller)
else:
raise NameError("missing required 'run' method for class: '{}'".format(callback.__name__))
else:
# Register a function callback
self.parent = parent
self.callback = callback
callback.test = self.unittest_caller

def __init__(self, parent, callback, org_callback, path):
self.decorated_callback = callback
self.is_playable = parent.is_playable
self.is_folder = parent.is_folder
self.org_callback = org_callback
self.callback = callback
self.parent = parent
self.path = path

def args_to_kwargs(self, args, kwargs):
Expand Down Expand Up @@ -228,32 +239,18 @@ def register(self, callback, parent):
:param parent: Parent class that will handle the callback, used when callback is a function.
:returns: The callback function with extra attributes added, 'route', 'testcall'.
"""
if callback.__name__.lower() == "root":
path = callback.__name__.lower()
else:
# Construct route path
path = callback.__name__.lower()
if path != "root":
path = "/{}/{}".format(callback.__module__.strip("_").replace(".", "/"), callback.__name__).lower()

# Register callback only if it's route path is unique
if path in self.registered_routes:
raise ValueError("encountered duplicate route: '{}'".format(path))

# Register a class callback
elif inspect.isclass(callback):
if hasattr(callback, "run"):
# Set the callback as the parent and the run method as the 'run' function
route = Route(callback, callback.run, callback, path)
tester = staticmethod(route.unittest_caller)
else:
raise NameError("missing required 'run' method for class: '{}'".format(callback.__name__))
else:
# Register a function callback
route = Route(parent, callback, callback, path)
tester = route.unittest_caller

# Return original function undecorated
self.registered_routes[path] = route
callback.route = route
callback.test = tester
return callback
self.registered_routes[path] = route = Route(callback, parent, path)
callback.route = route
return callback

def register_delayed(self, func, args, kwargs):
callback = (func, args, kwargs)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_callback(_, one=1, two="2", return_data=None):
return return_data

path = test_callback.__name__.lower()
self.route = support.Route(route.Route, test_callback, test_callback, path)
self.route = support.Route(test_callback, route.Route, path)

def test_arg_names(self):
args = self.route.arg_names()
Expand Down

0 comments on commit f15bf9e

Please sign in to comment.