[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.
"""
for subpath, params in HalfDomain.read_router(m_router).items():
for subpath, params in HalfDomain.read_router(m_router):
path.append(subpath)
for verb in VERBS:
@ -265,9 +265,8 @@ class HalfDomain(Starlette):
m_path = None
try:
if not hasattr(m_router, 'ROUTES'):
routes = {'':{}}
acls = getattr(m_router, 'ACLS') if hasattr(m_router, 'ACLS') else None
acls = getattr(m_router, 'ACLS', None)
routes = getattr(m_router, 'ROUTES', None)
if acls is not None:
for method in acls.keys():
@ -275,30 +274,36 @@ class HalfDomain(Starlette):
raise Exception(
'This method is not handled: {}'.format(method))
routes[''][method] = []
routes[''][method] = acls[method].copy()
yield '', dict([(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:
routes = getattr(m_router, 'ROUTES')
for method in VERBS:
if not hasattr(m_router, method):
continue
try:
ROUTER_SCHEMA.validate(routes)
except SchemaError as exc:
logger.error(routes)
raise exc
yield '', getattr(m_router, method)
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:
# TODO: Proper exception handling
raise exc