[wip][routing] get_fct_name update

This commit is contained in:
Maxime Alves LIRMM 2020-09-22 15:46:58 +02:00
parent 5f7d66d4d6
commit a78e6ebc75
1 changed files with 18 additions and 28 deletions

View File

@ -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])