[cli] handle dbupdate without argument

This commit is contained in:
Maxime Alves LIRMM@home 2020-07-28 16:03:34 +02:00
parent be1e5ed722
commit e289f6ad6b
1 changed files with 31 additions and 9 deletions

View File

@ -1,7 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from halfapi.conf import (PROJECT_NAME, HOST, PORT, from halfapi.conf import (PROJECT_NAME, HOST, PORT,
PRODUCTION, PRODUCTION, BASE_DIR, DOMAINS)
BASE_DIR)
from halfapi.db import ( from halfapi.db import (
Domain, Domain,
@ -60,6 +59,21 @@ def delete_domain(domain):
@click.option('--domain', default=None) @click.option('--domain', default=None)
@cli.command() @cli.command()
def dbupdate(domain): def dbupdate(domain):
if domain is None:
click.echo('No domain name given, will update all active domains')
for domain in DOMAINS:
dbupdate_fct(domain)
sys.exit(0)
return dbupdate_fct(domain)
def dbupdate_fct(domain=None):
if domain is None:
click.echo('Missing domain', err=True)
sys.exit(1)
else:
click.echo(f'Will update routes for {domain}')
def add_acl_fct(fct): def add_acl_fct(fct):
acl = AclFunction() acl = AclFunction()
@ -90,11 +104,11 @@ def dbupdate(domain):
if path[0] != '/': if path[0] != '/':
raise Exception('Malformed path') raise Exception('Malformed path')
elts = [] if len(path) == 1 else path[1:].split('/') elts = path[1:].split('/')
fct_name = [http_verb.lower()] fct_name = [http_verb.lower()]
for elt in elts: for elt in elts:
if 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: else:
fct_name.append(elt) fct_name.append(elt)
@ -139,17 +153,27 @@ def dbupdate(domain):
acl_set = set() acl_set = set()
try: try:
# module retrieval # module retrieval
dom_mod = importlib.import_module(domain) dom_mod = importlib.import_module(domain)
except ImportError:
click.echo(f"Can't import *{domain}*", err=True)
return False
try:
add_domain() add_domain()
# add sub routers # add sub routers
try:
ROUTERS = dom_mod.ROUTERS ROUTERS = dom_mod.ROUTERS
except AttributeError:
click.echo(f'The domain {domain} has no *ROUTERS* variable', err=True)
for router_name in dom_mod.ROUTERS: for router_name in dom_mod.ROUTERS:
try:
router_mod = importlib.import_module(f'.routers.{router_name}', domain) router_mod = importlib.import_module(f'.routers.{router_name}', domain)
except ImportError:
click.echo(f'The domain {domain} has no *{router_name}* router', err=True)
add_router(router_name) add_router(router_name)
pprint(router_mod.ROUTES) pprint(router_mod.ROUTES)
@ -158,8 +182,6 @@ def dbupdate(domain):
add_route(http_verb, route_path, router_name, acls) add_route(http_verb, route_path, router_name, acls)
except ImportError:
click.echo(f'The domain {domain} has no *ROUTES* variable', err=True)
except Exception as e: except Exception as e:
click.echo(e, err=True) click.echo(e, err=True)