mirror of
https://github.com/ynput/ayon-core.git
synced 2026-01-01 08:24:53 +01:00
changed registering troute info are not stored to callback
This commit is contained in:
parent
cdd6bd9c52
commit
cc9dd16d98
3 changed files with 38 additions and 41 deletions
|
|
@ -10,21 +10,9 @@ from .lib import (
|
||||||
|
|
||||||
def route(path, url_prefix="", methods=[]):
|
def route(path, url_prefix="", methods=[]):
|
||||||
def decorator(callback):
|
def decorator(callback):
|
||||||
@wraps(callback)
|
RestApiFactory.register_route(path, callback, url_prefix, methods)
|
||||||
def wrapper(*args, **kwargs):
|
callback.restapi = True
|
||||||
return callback(*args, **kwargs)
|
return callback
|
||||||
|
|
||||||
func = wrapper
|
|
||||||
func.restapi = True
|
|
||||||
func.path = path
|
|
||||||
func.methods = methods
|
|
||||||
func.url_prefix = url_prefix
|
|
||||||
if hasattr(callback, "__self__"):
|
|
||||||
func.__self__ = callback.__self__
|
|
||||||
func.callback = callback
|
|
||||||
|
|
||||||
RestApiFactory.register_route(func)
|
|
||||||
return func
|
|
||||||
return decorator
|
return decorator
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -112,8 +112,7 @@ def prepare_methods(methods, callback=None):
|
||||||
|
|
||||||
return _methods
|
return _methods
|
||||||
|
|
||||||
def prepare_callback_info(_callback):
|
def prepare_callback_info(callback):
|
||||||
callback = _callback.callback
|
|
||||||
callback_info = inspect.getfullargspec(callback)
|
callback_info = inspect.getfullargspec(callback)
|
||||||
|
|
||||||
callback_args = callback_info.args
|
callback_args = callback_info.args
|
||||||
|
|
@ -121,7 +120,7 @@ def prepare_callback_info(_callback):
|
||||||
if callback_args:
|
if callback_args:
|
||||||
callback_args_len = len(callback_args)
|
callback_args_len = len(callback_args)
|
||||||
if (
|
if (
|
||||||
type(_callback).__name__ == "method"
|
type(callback).__name__ == "method"
|
||||||
):
|
):
|
||||||
callback_args_len -= 1
|
callback_args_len -= 1
|
||||||
|
|
||||||
|
|
@ -158,9 +157,9 @@ class _RestApiFactory:
|
||||||
def has_handlers(self):
|
def has_handlers(self):
|
||||||
return (self.has_routes or self.prepared_statics)
|
return (self.has_routes or self.prepared_statics)
|
||||||
|
|
||||||
def _process_route(self, callback):
|
def _process_route(self, route):
|
||||||
return self.unprocessed_routes.pop(
|
return self.unprocessed_routes.pop(
|
||||||
self.unprocessed_routes.index(callback)
|
self.unprocessed_routes.index(route)
|
||||||
)
|
)
|
||||||
|
|
||||||
def _process_statics(self, item):
|
def _process_statics(self, item):
|
||||||
|
|
@ -168,11 +167,17 @@ class _RestApiFactory:
|
||||||
self.unprocessed_statics.index(item)
|
self.unprocessed_statics.index(item)
|
||||||
)
|
)
|
||||||
|
|
||||||
def register_route(self, item):
|
def register_route(self, path, callback, url_prefix, methods):
|
||||||
log.debug("Registering callback for item \"{}\"".format(
|
log.debug("Registering callback for item \"{}\"".format(
|
||||||
item.__qualname__
|
callback.__qualname__
|
||||||
))
|
))
|
||||||
self.unprocessed_routes.append(item)
|
route = {
|
||||||
|
"path": path,
|
||||||
|
"callback": callback,
|
||||||
|
"url_prefix": url_prefix,
|
||||||
|
"methods": methods
|
||||||
|
}
|
||||||
|
self.unprocessed_routes.append(route)
|
||||||
|
|
||||||
def register_obj(self, obj):
|
def register_obj(self, obj):
|
||||||
self.registered_objs.append(obj)
|
self.registered_objs.append(obj)
|
||||||
|
|
@ -181,10 +186,11 @@ class _RestApiFactory:
|
||||||
log.debug("Registering statics path \"{}\"".format(item))
|
log.debug("Registering statics path \"{}\"".format(item))
|
||||||
self.unprocessed_statics.append(item)
|
self.unprocessed_statics.append(item)
|
||||||
|
|
||||||
def _prepare_route(self, callback):
|
def _prepare_route(self, route):
|
||||||
methods = prepare_methods(callback.methods, callback)
|
callback = route["callback"]
|
||||||
url_prefix = prepare_prefix(callback.url_prefix)
|
methods = prepare_methods(route["methods"], callback)
|
||||||
fullpath = prepare_fullpath(callback.path, url_prefix)
|
url_prefix = prepare_prefix(route["url_prefix"])
|
||||||
|
fullpath = prepare_fullpath(route["path"], url_prefix)
|
||||||
regex, regex_keys = prepare_regex_from_path(fullpath)
|
regex, regex_keys = prepare_regex_from_path(fullpath)
|
||||||
callback_info = prepare_callback_info(callback)
|
callback_info = prepare_callback_info(callback)
|
||||||
|
|
||||||
|
|
@ -217,8 +223,14 @@ class _RestApiFactory:
|
||||||
]
|
]
|
||||||
for method_name in method_names:
|
for method_name in method_names:
|
||||||
method = obj.__getattribute__(method_name)
|
method = obj.__getattribute__(method_name)
|
||||||
|
if not hasattr(method, "restapi"):
|
||||||
|
continue
|
||||||
|
|
||||||
for callback in self.unprocessed_routes:
|
if not method.restapi:
|
||||||
|
continue
|
||||||
|
|
||||||
|
for route in self.unprocessed_routes:
|
||||||
|
callback = route["callback"]
|
||||||
if not (
|
if not (
|
||||||
callback.__qualname__ == method.__qualname__ and
|
callback.__qualname__ == method.__qualname__ and
|
||||||
callback.__module__ == method.__module__ and
|
callback.__module__ == method.__module__ and
|
||||||
|
|
@ -226,18 +238,13 @@ class _RestApiFactory:
|
||||||
):
|
):
|
||||||
continue
|
continue
|
||||||
|
|
||||||
self._process_route(callback)
|
route["callback"] = method
|
||||||
|
self._process_route(route)
|
||||||
if not hasattr(method, "restapi"):
|
self._prepare_route(route)
|
||||||
continue
|
|
||||||
|
|
||||||
if not method.restapi:
|
|
||||||
continue
|
|
||||||
|
|
||||||
self._prepare_route(method)
|
|
||||||
break
|
break
|
||||||
|
|
||||||
for callback in self.unprocessed_routes:
|
for route in self.unprocessed_routes:
|
||||||
|
callback = route["callback"]
|
||||||
is_class_method = len(callback.__qualname__.split(".")) != 1
|
is_class_method = len(callback.__qualname__.split(".")) != 1
|
||||||
if is_class_method:
|
if is_class_method:
|
||||||
missing_self = True
|
missing_self = True
|
||||||
|
|
@ -260,7 +267,7 @@ class _RestApiFactory:
|
||||||
))
|
))
|
||||||
continue
|
continue
|
||||||
|
|
||||||
self._prepare_route(callback)
|
self._prepare_route(route)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
self._prepare_route(callback)
|
self._prepare_route(route)
|
||||||
|
|
|
||||||
|
|
@ -39,7 +39,9 @@ class RestApiServer:
|
||||||
self.failed_icon = failed_icon
|
self.failed_icon = failed_icon
|
||||||
|
|
||||||
def register_callback(self, path, callback, url_prefix="", methods=[]):
|
def register_callback(self, path, callback, url_prefix="", methods=[]):
|
||||||
route(path, url_prefix, methods)(callback)
|
callback.restapi = True
|
||||||
|
RestApiFactory.register_route(path, callback, url_prefix, methods)
|
||||||
|
# route(path, url_prefix, methods)(callback)
|
||||||
|
|
||||||
def register_statics(self, url_prefix, dir_path):
|
def register_statics(self, url_prefix, dir_path):
|
||||||
register_statics(url_prefix, dir_path)
|
register_statics(url_prefix, dir_path)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue