[lib][domain/routes] clean code, use "yield from"

This commit is contained in:
Maxime Alves LIRMM 2020-10-04 18:09:23 +02:00
parent deb41be3e8
commit b6e511a96d
2 changed files with 13 additions and 35 deletions

View File

@ -90,16 +90,13 @@ def gen_router_routes(m_router, path=None):
for subpath, route_params in routes.items(): for subpath, route_params in routes.items():
path.append(subpath) path.append(subpath)
for r_path, d_route in gen_routes(route_params, path, m_router): yield from gen_routes(route_params, path, m_router)
yield r_path, d_route
subroutes = route_params.get('SUBROUTES', []) subroutes = route_params.get('SUBROUTES', [])
for subroute in subroutes: for subroute in subroutes:
path.append(subroute) path.append(subroute)
submod = importlib.import_module(f'.{subroute}', m_router.__name__) submod = importlib.import_module(f'.{subroute}', m_router.__name__)
for r_path, d_route in gen_router_routes(submod, path): yield from gen_router_routes(submod, path)
yield r_path, d_route
path.pop() path.pop()

View File

@ -1,19 +1,14 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from functools import wraps from functools import wraps
import importlib from typing import Callable, List, Dict, Generator
import sys
from typing import Callable, List, Tuple, Dict, Generator
from types import ModuleType, FunctionType from types import ModuleType, FunctionType
from halfapi.conf import (PROJECT_NAME, DB_NAME, HOST, PORT,
PRODUCTION, DOMAINS)
from halfapi.lib.responses import *
from halfapi.lib.domain import gen_domain_routes, VERBS
from starlette.exceptions import HTTPException from starlette.exceptions import HTTPException
from starlette.routing import Mount, Route from starlette.routing import Route
from starlette.requests import Request from starlette.requests import Request
from halfapi.lib.domain import gen_domain_routes, VERBS
class DomainNotFoundError(Exception): class DomainNotFoundError(Exception):
pass pass
@ -38,11 +33,6 @@ def route_acl_decorator(fct: Callable, params: List[Dict]):
async def caller(req: Request, *args, **kwargs): async def caller(req: Request, *args, **kwargs):
for param in params: for param in params:
if param.get('acl'): if param.get('acl'):
"""
We merge the 'acl' and 'keys' kwargs values to let the
decorated function know which ACL function answered
True, and other parameters that you'd need
"""
passed = param['acl'](req, *args, **kwargs) passed = param['acl'](req, *args, **kwargs)
if isinstance(passed, FunctionType): if isinstance(passed, FunctionType):
passed = param['acl']()(req, *args, **kwargs) passed = param['acl']()(req, *args, **kwargs)
@ -61,12 +51,6 @@ def route_acl_decorator(fct: Callable, params: List[Dict]):
return caller return caller
###
# testing purpose only
def acl_mock(fct):
return lambda r, *a, **kw: True
#
##
def gen_starlette_routes(m_dom: ModuleType) -> Generator: def gen_starlette_routes(m_dom: ModuleType) -> Generator:
""" """
@ -79,7 +63,6 @@ def gen_starlette_routes(m_dom: ModuleType) -> Generator:
Generator(Route) Generator(Route)
""" """
m_dom_acl = importlib.import_module('.acl', m_dom.__name__)
for path, d_route in gen_domain_routes(m_dom.__name__): for path, d_route in gen_domain_routes(m_dom.__name__):
for verb in VERBS: for verb in VERBS:
@ -107,12 +90,10 @@ def api_routes(m_dom: ModuleType) -> Generator:
Generator(Dict) Generator(Dict)
""" """
m_dom_acl = importlib.import_module('.acl', m_dom.__name__)
d_acls = {} d_acls = {}
def str_acl(params): def str_acl(params):
l_params = [] l_params = []
access = None
for param in params: for param in params:
if 'acl' not in param.keys(): if 'acl' not in param.keys():
@ -139,14 +120,14 @@ def api_routes(m_dom: ModuleType) -> Generator:
def api_acls(request): def api_acls(request):
from .. import app from .. import app # FIXME: Find a way to get d_acl without having to import
res = {} res = {}
for domain in app.d_acl.keys(): for domain, d_domain_acl in app.d_acl.items():
res[domain] = {} res[domain] = {}
for acl_name, fct in app.d_acl[domain].items(): for acl_name, fct in d_domain_acl.items():
ok = fct(request) fct_result = fct(request)
if isinstance(ok, FunctionType): if isinstance(fct_result, FunctionType):
ok = fct()(request) fct_result = fct()(request)
res[domain][acl_name] = ok res[domain][acl_name] = fct_result
return res return res