From a78e6ebc75de17bc1bcf9a5125b7499cc0e5663e Mon Sep 17 00:00:00 2001 From: Maxime Alves LIRMM Date: Tue, 22 Sep 2020 15:46:58 +0200 Subject: [PATCH] =?UTF-8?q?[wip][routing]=C2=A0get=5Ffct=5Fname=20update?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- halfapi/lib/domain.py | 46 +++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) diff --git a/halfapi/lib/domain.py b/halfapi/lib/domain.py index 4a14bff..a99be7c 100644 --- a/halfapi/lib/domain.py +++ b/halfapi/lib/domain.py @@ -1,16 +1,16 @@ #!/usr/bin/env python3 import importlib -import typing as t +import typing as typ VERBS = ('GET', 'POST', 'PUT', 'PATCH', 'DELETE') -def get_fct_name(http_verb, path: t.List): +def get_fct_name(http_verb, path: str): """ Returns the predictable name of the function for a route Parameters: - http_verb (str): The Route's HTTP method (GET, POST, ...) - - path (str): A path beginning by '/' for the route + - path (str): The functions path Returns: str: The *unique* function name for a route and it's verb @@ -18,34 +18,29 @@ def get_fct_name(http_verb, path: t.List): Examples: - >>> get_fct_name('foo', 'bar') - Traceback (most recent call last): - ... - Exception: Malformed path + >>> get_fct_name('get', '') + 'get' - >>> get_fct_name('get', '/') - 'get_' + >>> get_fct_name('GET', '') + 'get' - >>> get_fct_name('GET', '/') - 'get_' - - >>> get_fct_name('POST', '/foo') + >>> get_fct_name('POST', 'foo') 'post_foo' - >>> get_fct_name('POST', '/foo/bar') - 'post_foo_bar' + >>> get_fct_name('POST', 'bar') + 'post_bar' - >>> get_fct_name('DEL', '/foo/{boo}/{far}/bar') - 'del_foo_BOO_FAR_bar' - - >>> get_fct_name('DEL', '/foo/{boo:zoo}') + >>> get_fct_name('DEL', 'foo/{boo}') 'del_foo_BOO' + + >>> get_fct_name('DEL', '{boo:zoo}/far') + 'del_BOO_far' """ fct_name = [http_verb.lower()] - for elt in path: + for elt in path.split('/'): if elt and elt[0] == '{': fct_name.append(elt[1:-1].split(':')[0].upper()) - else: + elif elt: fct_name.append(elt) return '_'.join(fct_name) @@ -59,7 +54,7 @@ def gen_routes(route_params, path, m_router): print(f'No ACL for route [{verb}] "/".join(path)') try: - fct_name = get_fct_name(verb, [path[-1]]) + fct_name = get_fct_name(verb, path[-1]) fct = getattr(m_router, fct_name) except AttributeError: print(f'{fct_name} is not defined in {m_router.__name__}') @@ -84,7 +79,6 @@ def gen_router_routes(m_router, path=[]): routes = m_router.ROUTES - pathlen = len(path) for subpath, route_params in routes.items(): path.append(subpath) @@ -100,8 +94,7 @@ def gen_router_routes(m_router, path=[]): path.pop() - if pathlen < len(path): - path.pop() + path.pop() @@ -109,9 +102,6 @@ def gen_router_routes(m_router, path=[]): def gen_domain_routes(domain): m_domain = importlib.import_module(domain) - if not hasattr(m_domain, 'routers'): - raise Exception(f'No *routers* module in *{domain}*') - m_router = importlib.import_module('.routers', domain) return gen_router_routes(m_router, [domain])