[enh] Add a hook_info action

This commit is contained in:
Jérôme Lebleu 2015-10-06 12:43:10 +02:00
parent c16203bd2a
commit 369dec6775
2 changed files with 52 additions and 1 deletions

View file

@ -1292,6 +1292,16 @@ hook:
app:
help: Scripts related to app will be removed
### hook_info()
info:
action_help: Get information about a given hook
api: GET /hooks/<action>/<name>
arguments:
action:
help: Action name
name:
help: Hook name
### hook_list()
list:
action_help: List available hooks for an action

View file

@ -29,6 +29,7 @@ import re
import json
import errno
import subprocess
from glob import iglob
from moulinette.core import MoulinetteError
from moulinette.utils.log import getActionLogger
@ -77,6 +78,46 @@ def hook_remove(app):
except OSError: pass
def hook_info(action, name):
"""
Get information about a given hook
Keyword argument:
action -- Action name
name -- Hook name
"""
hooks = []
priorities = set()
# Search in custom folder first
for h in iglob('{:s}{:s}/*-{:s}'.format(
custom_hook_folder, action, name)):
priority, _ = _extract_filename_parts(os.path.basename(h))
priorities.add(priority)
hooks.append({
'priority': priority,
'path': h,
})
# Append non-overwritten system hooks
for h in iglob('{:s}{:s}/*-{:s}'.format(
hook_folder, action, name)):
priority, _ = _extract_filename_parts(os.path.basename(h))
if priority not in priorities:
hooks.append({
'priority': priority,
'path': h,
})
if not hooks:
raise MoulinetteError(errno.EINVAL, m18n.n('hook_name_unknown', name))
return {
'action': action,
'name': name,
'hooks': hooks,
}
def hook_list(action, list_by='name', show_info=False):
"""
List available hooks for an action
@ -194,7 +235,7 @@ def hook_callback(action, hooks=[], args=None):
if key == n or key.startswith("%s_" % n) \
and key not in all_hooks:
all_hooks.append(key)
# Iterate over given hooks names list
for n in all_hooks:
try: