[enh] Replace string strange list

This commit is contained in:
ljf 2018-08-10 15:40:00 +02:00
parent 625e870008
commit 6c18e51b82
3 changed files with 28 additions and 26 deletions

View file

@ -197,42 +197,41 @@ def log_display(path, number=50, share=False):
return infos 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 Configure quickly a unit operation
This decorator help you to configure quickly the record of a unit operations. This decorator help you to configure quickly the record of a unit operations.
Argument: Argument:
entities A list seperated by coma of entity types related to the unit entities A list of entity types related to the unit operation. The entity
operation. The entity type is searched inside argument's names of the type is searched inside argument's names of the decorated function. If
decorated function. If something match, the argument value is added as something match, the argument value is added as related entity. If the
related entity. 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 exclude Remove some arguments from the context. By default, arguments
called 'password' and 'auth' are removed. If an argument is an object, you 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 need to exclude it or create manually the unit operation without this
decorator. decorator.
operation_key Key describing the unit operation. If you want to display a operation_key A key to describe the unit operation log used to create the
well formed description you should add a translation key like this filename and search a translation. Please ensure that this key prefixed by
"log_" + operation_key in locales files. 'log_' is present in locales/en.json otherwise it won't be translatable.
""" """
def decorate(func): def decorate(func):
def func_wrapper(*args, **kwargs): 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 op_key = operation_key
related_to = []
if op_key is None: if op_key is None:
op_key = func.__name__ op_key = func.__name__
# In case the function is called directly from an other part of the # If the function is called directly from an other part of the code
# 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: if len(args) > 0:
from inspect import getargspec from inspect import getargspec
keys = getargspec(func).args keys = getargspec(func).args
@ -243,10 +242,13 @@ def is_unit_operation(entities='app,domain,service,user', exclude='auth,password
args = () args = ()
# Search related entity in arguments of the decorated function # Search related entity in arguments of the decorated function
for entity in entities_list: related_to = []
entity = entity.split(':') for entity in entities:
entity_type = entity[-1] if isinstance(entity, tuple):
entity = entity[0] entity_type = entity[1]
entity = entity[0]
else:
entity_type = entity
if entity in kwargs and kwargs[entity] is not None: if entity in kwargs and kwargs[entity] is not None:
if isinstance(kwargs[entity], basestring): if isinstance(kwargs[entity], basestring):
@ -258,13 +260,13 @@ def is_unit_operation(entities='app,domain,service,user', exclude='auth,password
context = kwargs.copy() context = kwargs.copy()
# Exclude unappropriate data from the context # Exclude unappropriate data from the context
for field in exclude_list: for field in exclude:
if field in context: if field in context:
context.pop(field, None) context.pop(field, None)
uo = UnitOperation(op_key, related_to, args=context) uo = UnitOperation(op_key, related_to, args=context)
try: 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 # in argument to let the developper start the record itself
args = (uo,) + args args = (uo,) + args
result = func(*args, **kwargs) result = func(*args, **kwargs)

View file

@ -345,7 +345,7 @@ def service_log(name, number=50):
return result 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, def service_regen_conf(uo, names=[], with_diff=False, force=False, dry_run=False,
list_pending=False): list_pending=False):
""" """

View file

@ -99,7 +99,7 @@ def user_list(auth, fields=None):
return {'users': users} return {'users': users}
@is_unit_operation('username:user') @is_unit_operation([('username', 'user')])
def user_create(uo, auth, username, firstname, lastname, mail, password, def user_create(uo, auth, username, firstname, lastname, mail, password,
mailbox_quota="0"): 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')) 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): def user_delete(uo, auth, username, purge=False):
""" """
Delete user Delete user
@ -260,7 +260,7 @@ def user_delete(uo, auth, username, purge=False):
logger.success(m18n.n('user_deleted')) 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, def user_update(uo, auth, username, firstname=None, lastname=None, mail=None,
change_password=None, add_mailforward=None, remove_mailforward=None, change_password=None, add_mailforward=None, remove_mailforward=None,
add_mailalias=None, remove_mailalias=None, mailbox_quota=None): add_mailalias=None, remove_mailalias=None, mailbox_quota=None):