[wip] do not use subroutes and HalfDomain.read_routes is a generator

This commit is contained in:
maxime 2022-08-19 12:01:43 +02:00
parent 910e1e1497
commit ef4a4c2b98
1 changed files with 34 additions and 29 deletions

View File

@ -212,7 +212,7 @@ class HalfDomain(Starlette):
that decorates the endpoint function. that decorates the endpoint function.
""" """
for subpath, params in HalfDomain.read_router(m_router).items(): for subpath, params in HalfDomain.read_router(m_router):
path.append(subpath) path.append(subpath)
for verb in VERBS: for verb in VERBS:
@ -265,9 +265,8 @@ class HalfDomain(Starlette):
m_path = None m_path = None
try: try:
if not hasattr(m_router, 'ROUTES'): acls = getattr(m_router, 'ACLS', None)
routes = {'':{}} routes = getattr(m_router, 'ROUTES', None)
acls = getattr(m_router, 'ACLS') if hasattr(m_router, 'ACLS') else None
if acls is not None: if acls is not None:
for method in acls.keys(): for method in acls.keys():
@ -275,30 +274,36 @@ class HalfDomain(Starlette):
raise Exception( raise Exception(
'This method is not handled: {}'.format(method)) 'This method is not handled: {}'.format(method))
routes[''][method] = [] yield '', dict([(method, acls[method].copy())])
routes[''][method] = acls[method].copy()
routes['']['SUBROUTES'] = []
if hasattr(m_router, '__path__'):
""" Module is a package
"""
m_path = getattr(m_router, '__path__')
if isinstance(m_path, list) and len(m_path) == 1:
routes['']['SUBROUTES'] = [
elt.name
for elt in os.scandir(m_path[0])
if elt.is_dir()
]
else: else:
routes = getattr(m_router, 'ROUTES') for method in VERBS:
if not hasattr(m_router, method):
continue
try: yield '', getattr(m_router, method)
ROUTER_SCHEMA.validate(routes)
except SchemaError as exc:
logger.error(routes)
raise exc
return routes # routes['']['SUBROUTES'] = []
# if hasattr(m_router, '__path__'):
# """ Module is a package
# """
# m_path = getattr(m_router, '__path__')
# if isinstance(m_path, list) and len(m_path) == 1:
# routes['']['SUBROUTES'] = [
# elt.name
# for elt in os.scandir(m_path[0])
# if elt.is_dir()
# ]
# try:
# ROUTER_SCHEMA.validate(routes)
# except SchemaError as exc:
# logger.error(routes)
# raise exc
if routes is not None:
for key, value in routes.items():
yield key, value
except ImportError as exc: except ImportError as exc:
# TODO: Proper exception handling # TODO: Proper exception handling
raise exc raise exc