[wip][routing] get_fct_name update
This commit is contained in:
parent
5f7d66d4d6
commit
a78e6ebc75
@ -1,16 +1,16 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import importlib
|
import importlib
|
||||||
import typing as t
|
import typing as typ
|
||||||
|
|
||||||
VERBS = ('GET', 'POST', 'PUT', 'PATCH', 'DELETE')
|
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
|
Returns the predictable name of the function for a route
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
- http_verb (str): The Route's HTTP method (GET, POST, ...)
|
- 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:
|
Returns:
|
||||||
str: The *unique* function name for a route and it's verb
|
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:
|
Examples:
|
||||||
|
|
||||||
>>> get_fct_name('foo', 'bar')
|
>>> get_fct_name('get', '')
|
||||||
Traceback (most recent call last):
|
'get'
|
||||||
...
|
|
||||||
Exception: Malformed path
|
|
||||||
|
|
||||||
>>> get_fct_name('get', '/')
|
>>> get_fct_name('GET', '')
|
||||||
'get_'
|
'get'
|
||||||
|
|
||||||
>>> get_fct_name('GET', '/')
|
>>> get_fct_name('POST', 'foo')
|
||||||
'get_'
|
|
||||||
|
|
||||||
>>> get_fct_name('POST', '/foo')
|
|
||||||
'post_foo'
|
'post_foo'
|
||||||
|
|
||||||
>>> get_fct_name('POST', '/foo/bar')
|
>>> get_fct_name('POST', 'bar')
|
||||||
'post_foo_bar'
|
'post_bar'
|
||||||
|
|
||||||
>>> get_fct_name('DEL', '/foo/{boo}/{far}/bar')
|
>>> get_fct_name('DEL', 'foo/{boo}')
|
||||||
'del_foo_BOO_FAR_bar'
|
|
||||||
|
|
||||||
>>> get_fct_name('DEL', '/foo/{boo:zoo}')
|
|
||||||
'del_foo_BOO'
|
'del_foo_BOO'
|
||||||
|
|
||||||
|
>>> get_fct_name('DEL', '{boo:zoo}/far')
|
||||||
|
'del_BOO_far'
|
||||||
"""
|
"""
|
||||||
fct_name = [http_verb.lower()]
|
fct_name = [http_verb.lower()]
|
||||||
for elt in path:
|
for elt in path.split('/'):
|
||||||
if elt and elt[0] == '{':
|
if elt and elt[0] == '{':
|
||||||
fct_name.append(elt[1:-1].split(':')[0].upper())
|
fct_name.append(elt[1:-1].split(':')[0].upper())
|
||||||
else:
|
elif elt:
|
||||||
fct_name.append(elt)
|
fct_name.append(elt)
|
||||||
|
|
||||||
return '_'.join(fct_name)
|
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)')
|
print(f'No ACL for route [{verb}] "/".join(path)')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
fct_name = get_fct_name(verb, [path[-1]])
|
fct_name = get_fct_name(verb, path[-1])
|
||||||
fct = getattr(m_router, fct_name)
|
fct = getattr(m_router, fct_name)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
print(f'{fct_name} is not defined in {m_router.__name__}')
|
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
|
routes = m_router.ROUTES
|
||||||
|
|
||||||
pathlen = len(path)
|
|
||||||
for subpath, route_params in routes.items():
|
for subpath, route_params in routes.items():
|
||||||
path.append(subpath)
|
path.append(subpath)
|
||||||
|
|
||||||
@ -100,8 +94,7 @@ def gen_router_routes(m_router, path=[]):
|
|||||||
|
|
||||||
path.pop()
|
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):
|
def gen_domain_routes(domain):
|
||||||
m_domain = importlib.import_module(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)
|
m_router = importlib.import_module('.routers', domain)
|
||||||
|
|
||||||
return gen_router_routes(m_router, [domain])
|
return gen_router_routes(m_router, [domain])
|
||||||
|
Loading…
Reference in New Issue
Block a user