From 6c18e51b823ebd986e6e9c7484f340ddac06570a Mon Sep 17 00:00:00 2001 From: ljf Date: Fri, 10 Aug 2018 15:40:00 +0200 Subject: [PATCH] [enh] Replace string strange list --- src/yunohost/log.py | 46 +++++++++++++++++++++-------------------- src/yunohost/service.py | 2 +- src/yunohost/user.py | 6 +++--- 3 files changed, 28 insertions(+), 26 deletions(-) diff --git a/src/yunohost/log.py b/src/yunohost/log.py index 735ce75a3..707a0b356 100644 --- a/src/yunohost/log.py +++ b/src/yunohost/log.py @@ -197,42 +197,41 @@ def log_display(path, number=50, share=False): return infos -def is_unit_operation(entities='app,domain,service,user', exclude='auth,password', operation_key=None): +def is_unit_operation(entities=['app', 'domain', 'service', 'user'], + exclude=['auth', 'password'], operation_key=None): """ Configure quickly a unit operation This decorator help you to configure quickly the record of a unit operations. Argument: - entities A list seperated by coma of entity types related to the unit - operation. The entity type is searched inside argument's names of the - decorated function. If something match, the argument value is added as - related entity. + entities A list of entity types related to the unit operation. The entity + type is searched inside argument's names of the decorated function. If + something match, the argument value is added as related entity. If the + argument name is different you can specify it with a tuple + (argname, entity_type) instead of just put the entity type. exclude Remove some arguments from the context. By default, arguments called 'password' and 'auth' are removed. If an argument is an object, you need to exclude it or create manually the unit operation without this decorator. - operation_key Key describing the unit operation. If you want to display a - well formed description you should add a translation key like this - "log_" + operation_key in locales files. + operation_key A key to describe the unit operation log used to create the + filename and search a translation. Please ensure that this key prefixed by + 'log_' is present in locales/en.json otherwise it won't be translatable. """ def decorate(func): def func_wrapper(*args, **kwargs): - # For a strange reason we can't use directly the arguments from - # is_unit_operation function. We need to store them in a var before. - entities_list = entities.split(',') - exclude_list = exclude.split(',') op_key = operation_key - related_to = [] - if op_key is None: op_key = func.__name__ - # In case the function is called directly from an other part of the - # code + # If the function is called directly from an other part of the code + # and not by the moulinette framework, we need to complete kwargs + # dictionnary with the args list. + # Indeed, we use convention naming in this decorator and we need to + # know name of each args (so we need to use kwargs instead of args) if len(args) > 0: from inspect import getargspec keys = getargspec(func).args @@ -243,10 +242,13 @@ def is_unit_operation(entities='app,domain,service,user', exclude='auth,password args = () # Search related entity in arguments of the decorated function - for entity in entities_list: - entity = entity.split(':') - entity_type = entity[-1] - entity = entity[0] + related_to = [] + for entity in entities: + if isinstance(entity, tuple): + entity_type = entity[1] + entity = entity[0] + else: + entity_type = entity if entity in kwargs and kwargs[entity] is not None: if isinstance(kwargs[entity], basestring): @@ -258,13 +260,13 @@ def is_unit_operation(entities='app,domain,service,user', exclude='auth,password context = kwargs.copy() # Exclude unappropriate data from the context - for field in exclude_list: + for field in exclude: if field in context: context.pop(field, None) uo = UnitOperation(op_key, related_to, args=context) try: - # Start the actual function, and give the unit operation + # Start the actual function, and give the unit operation # in argument to let the developper start the record itself args = (uo,) + args result = func(*args, **kwargs) diff --git a/src/yunohost/service.py b/src/yunohost/service.py index 71b554785..ba9c4450e 100644 --- a/src/yunohost/service.py +++ b/src/yunohost/service.py @@ -345,7 +345,7 @@ def service_log(name, number=50): return result -@is_unit_operation('names:service') +@is_unit_operation([('names', 'service')]) def service_regen_conf(uo, names=[], with_diff=False, force=False, dry_run=False, list_pending=False): """ diff --git a/src/yunohost/user.py b/src/yunohost/user.py index e92256994..9fb3f184a 100644 --- a/src/yunohost/user.py +++ b/src/yunohost/user.py @@ -99,7 +99,7 @@ def user_list(auth, fields=None): return {'users': users} -@is_unit_operation('username:user') +@is_unit_operation([('username', 'user')]) def user_create(uo, auth, username, firstname, lastname, mail, password, mailbox_quota="0"): """ @@ -223,7 +223,7 @@ def user_create(uo, auth, username, firstname, lastname, mail, password, raise MoulinetteError(169, m18n.n('user_creation_failed')) -@is_unit_operation('username:user') +@is_unit_operation([('username', 'user')]) def user_delete(uo, auth, username, purge=False): """ Delete user @@ -260,7 +260,7 @@ def user_delete(uo, auth, username, purge=False): logger.success(m18n.n('user_deleted')) -@is_unit_operation('username:user', exclude='auth,change_password') +@is_unit_operation([('username', 'user')], exclude=['auth', 'change_password']) def user_update(uo, auth, username, firstname=None, lastname=None, mail=None, change_password=None, add_mailforward=None, remove_mailforward=None, add_mailalias=None, remove_mailalias=None, mailbox_quota=None):