[fix] Pep8

This commit is contained in:
ljf 2018-08-10 15:59:36 +02:00
parent 6c18e51b82
commit a43b3dfa18

View file

@ -40,7 +40,7 @@ from moulinette.utils.filesystem import read_file
CATEGORIES_PATH = '/var/log/yunohost/categories/' CATEGORIES_PATH = '/var/log/yunohost/categories/'
OPERATIONS_PATH = '/var/log/yunohost/categories/operation/' OPERATIONS_PATH = '/var/log/yunohost/categories/operation/'
CATEGORIES = ['operation', 'history', 'package', 'system', 'access', 'service', \ CATEGORIES = ['operation', 'history', 'package', 'system', 'access', 'service',
'app'] 'app']
METADATA_FILE_EXT = '.yml' METADATA_FILE_EXT = '.yml'
LOG_FILE_EXT = '.log' LOG_FILE_EXT = '.log'
@ -48,6 +48,7 @@ RELATED_CATEGORIES = ['app', 'domain', 'service', 'user']
logger = getActionLogger('yunohost.log') logger = getActionLogger('yunohost.log')
def log_list(category=[], limit=None): def log_list(category=[], limit=None):
""" """
List available logs List available logs
@ -73,7 +74,8 @@ def log_list(category=[], limit=None):
continue continue
logs = filter(lambda x: x.endswith(METADATA_FILE_EXT), os.listdir(category_path)) logs = filter(lambda x: x.endswith(METADATA_FILE_EXT),
os.listdir(category_path))
logs = reversed(sorted(logs)) logs = reversed(sorted(logs))
if limit is not None: if limit is not None:
@ -93,7 +95,8 @@ def log_list(category=[], limit=None):
} }
entry["description"] = _get_description_from_name(base_filename) entry["description"] = _get_description_from_name(base_filename)
try: try:
log_datetime = datetime.strptime(" ".join(log[:2]), "%Y%m%d %H%M%S") log_datetime = datetime.strptime(" ".join(log[:2]),
"%Y%m%d %H%M%S")
except ValueError: except ValueError:
pass pass
else: else:
@ -101,7 +104,8 @@ def log_list(category=[], limit=None):
result[category].append(entry) result[category].append(entry)
# Reverse the order of log when in cli, more comfortable to read (avoid unecessary scrolling) # Reverse the order of log when in cli, more comfortable to read (avoid
# unecessary scrolling)
if not is_api: if not is_api:
for category in result: for category in result:
result[category] = list(reversed(result[category])) result[category] = list(reversed(result[category]))
@ -131,7 +135,7 @@ def log_display(path, number=50, share=False):
if os.path.exists(abs_path) or os.path.exists(abs_path + METADATA_FILE_EXT): if os.path.exists(abs_path) or os.path.exists(abs_path + METADATA_FILE_EXT):
break break
if os.path.exists(abs_path) and not path.endswith(METADATA_FILE_EXT) : if os.path.exists(abs_path) and not path.endswith(METADATA_FILE_EXT):
log_path = abs_path log_path = abs_path
if abs_path.endswith(METADATA_FILE_EXT) or abs_path.endswith(LOG_FILE_EXT): if abs_path.endswith(METADATA_FILE_EXT) or abs_path.endswith(LOG_FILE_EXT):
@ -167,7 +171,7 @@ def log_display(path, number=50, share=False):
logger.info(m18n.n("log_available_on_yunopaste", url=url)) logger.info(m18n.n("log_available_on_yunopaste", url=url))
if msettings.get('interface') == 'api': if msettings.get('interface') == 'api':
return { "url" : url } return {"url": url}
else: else:
return return
@ -191,18 +195,18 @@ def log_display(path, number=50, share=False):
if os.path.exists(log_path): if os.path.exists(log_path):
from yunohost.service import _tail from yunohost.service import _tail
logs = _tail(log_path, int(number)) logs = _tail(log_path, int(number))
#logs = [{"datetime": x.split(": ", 1)[0].replace("_", " "), "line": x.split(": ", 1)[1]} for x in logs if x]
infos['log_path'] = log_path infos['log_path'] = log_path
infos['logs'] = logs infos['logs'] = logs
return infos return infos
def is_unit_operation(entities=['app', 'domain', 'service', 'user'], def is_unit_operation(entities=['app', 'domain', 'service', 'user'],
exclude=['auth', 'password'], operation_key=None): 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 the record of a unit operations.
Argument: Argument:
entities A list of entity types related to the unit operation. The entity entities A list of entity types related to the unit operation. The entity
@ -279,14 +283,15 @@ def is_unit_operation(entities=['app', 'domain', 'service', 'user'],
return func_wrapper return func_wrapper
return decorate return decorate
class UnitOperation(object): class UnitOperation(object):
""" """
Instances of this class represents unit operation the yunohost admin as done. Instances of this class represents unit operation done on the ynh instance.
Each time an action of the yunohost cli/api change the system, one or several Each time an action of the yunohost cli/api change the system, one or
unit operations should be registered. several unit operations should be registered.
This class record logs and some metadata like context or start time/end time. This class record logs and metadata like context or start time/end time.
""" """
def __init__(self, operation, related_to=None, **kwargs): def __init__(self, operation, related_to=None, **kwargs):
@ -351,9 +356,9 @@ class UnitOperation(object):
name += [self.operation] name += [self.operation]
if hasattr(self, "name_parameter_override"): if hasattr(self, "name_parameter_override"):
# This is for special cases where the operation is not really unitary # This is for special cases where the operation is not really
# For instance, the regen conf cannot be logged "per service" because of # unitary. For instance, the regen conf cannot be logged "per
# the way it's built # service" because of the way it's built
name.append(self.name_parameter_override) name.append(self.name_parameter_override)
elif self.related_to: elif self.related_to:
# We use the name of the first related thing # We use the name of the first related thing
@ -422,7 +427,8 @@ class UnitOperation(object):
msg = "<strong>" + m18n.n('log_link_to_failed_log', msg = "<strong>" + m18n.n('log_link_to_failed_log',
name=self.name, desc=desc) + "</strong>" name=self.name, desc=desc) + "</strong>"
else: else:
msg = m18n.n('log_help_to_get_failed_log', name=self.name, desc=desc) msg = m18n.n('log_help_to_get_failed_log', name=self.name,
desc=desc)
logger.info(msg) logger.info(msg)
self.flush() self.flush()
return msg return msg
@ -435,7 +441,12 @@ class UnitOperation(object):
""" """
self.error(m18n.n('log_operation_unit_unclosed_properly')) self.error(m18n.n('log_operation_unit_unclosed_properly'))
def _get_description_from_name(name): def _get_description_from_name(name):
"""
Return the translated description from the filename
"""
parts = name.split("-", 3) parts = name.split("-", 3)
try: try:
try: try: