From 3e1fc78786505e4f80902ad84ba92246dc5b5942 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Lebleu?= Date: Thu, 4 Dec 2014 15:45:14 +0100 Subject: [PATCH] [enh] Choose the property to list hook by and show info in hook_list --- actionsmap/yunohost.yml | 12 +++++++++ hook.py | 60 ++++++++++++++++++++++++++++++++++------- locales/en.json | 1 + locales/fr.json | 1 + 4 files changed, 64 insertions(+), 10 deletions(-) diff --git a/actionsmap/yunohost.yml b/actionsmap/yunohost.yml index da7f31450..436104f1d 100644 --- a/actionsmap/yunohost.yml +++ b/actionsmap/yunohost.yml @@ -1141,6 +1141,18 @@ hook: arguments: action: help: Action name + -l: + full: --list-by + help: Property to list hook by + choices: + - name + - priority + - folder + default: name + -i: + full: --show-info + help: Show hook information + action: store_true ### hook_callback() callback: diff --git a/hook.py b/hook.py index 371cdbd40..d71accf0f 100644 --- a/hook.py +++ b/hook.py @@ -78,40 +78,80 @@ def hook_remove(app): except OSError: pass -def hook_list(action): +def hook_list(action, list_by='name', show_info=False): """ List available hooks for an action Keyword argument: action -- Action name + list_by -- Property to list hook by + show_info -- Show hook information """ - hooks = {} + result = {} - def _append_folder(folder): + # Process the property to list hook by + if list_by == 'priority': + if show_info: + def _append_hook(d, priority, name, path): + # Use the priority as key and a dict of hooks names + # with their info as value + value = { 'path': path } + try: + d[priority][name] = value + except KeyError: + d[priority] = { name: value } + else: + def _append_hook(d, priority, name, path): + # Use the priority as key and the name as value + try: + d[priority].add(name) + except KeyError: + d[priority] = set([name]) + elif list_by == 'name' or list_by == 'folder': + if show_info: + def _append_hook(d, priority, name, path): + # Use the name as key and hook info as value + d[name] = { 'priority': priority, 'path': path } + else: + if list_by == 'name': + result = set() + def _append_hook(d, priority, name, path): + # Add only the name + d.add(name) + else: + raise MoulinetteError(errno.EINVAL, m18n.n('hook_list_by_invalid')) + + def _append_folder(d, folder): + # Iterate over and add hook from a folder for f in os.listdir(folder + action): path = '%s%s/%s' % (folder, action, f) priority, name = _extract_filename_parts(f) - try: - hooks[priority][name] = path - except KeyError: - hooks[priority] = {name: path} + _append_hook(d, priority, name, path) try: # Append system hooks first - _append_folder(hook_folder) + if list_by == 'folder': + result['system'] = dict() if show_info else set() + _append_folder(result['system'], hook_folder) + else: + _append_folder(result, hook_folder) except OSError: logger.debug("system hook folder not found for action '%s' in %s", action, hook_folder) try: # Append custom hooks - _append_folder(custom_hook_folder) + if list_by == 'folder': + result['custom'] = dict() if show_info else set() + _append_folder(result['custom'], custom_hook_folder) + else: + _append_folder(result, custom_hook_folder) except OSError: logger.debug("custom hook folder not found for action '%s' in %s", action, custom_hook_folder) - return hooks + return { 'hooks': result } def hook_callback(action, args=None): diff --git a/locales/en.json b/locales/en.json index 17c9b7bfd..e5327fda0 100644 --- a/locales/en.json +++ b/locales/en.json @@ -77,6 +77,7 @@ "upnp_disabled" : "uPnP successfully disabled", "firewall_reloaded" : "Firewall successfully reloaded", + "hook_list_by_invalid" : "Invalid property to list hook by", "hook_choice_invalid" : "Invalid choice '{:s}'", "hook_argument_missing" : "Missing argument '{:s}'", diff --git a/locales/fr.json b/locales/fr.json index ecb0a4978..7c94f9584 100644 --- a/locales/fr.json +++ b/locales/fr.json @@ -77,6 +77,7 @@ "upnp_disabled" : "uPnP désactivé avec succès", "firewall_reloaded" : "Pare-feu rechargé avec succès", + "hook_list_by_invalid" : "Propriété pour lister les scripts incorrecte", "hook_choice_invalid" : "Choix incorrect : '{:s}'", "hook_argument_missing" : "Argument manquant : '{:s}'",