forked from quantifiedcode/quantifiedcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhooks.py
More file actions
52 lines (40 loc) · 1.54 KB
/
hooks.py
File metadata and controls
52 lines (40 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: utf-8 -*-
"""
Contains functions to call hooks provided by plugins.
"""
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import absolute_import
import logging
from collections import defaultdict
LOGGER = logging.getLogger(__name__)
class Hooks(object):
def __init__(self):
self.hooks = defaultdict(list)
def register(self, name, hook):
""" Registers a new hook for the given hook name.
:param name: name of the hook
:param hook: hook function
"""
LOGGER.debug("Registering new hook for {}: {}".format(name, hook))
if not hook in self.hooks[name]:
self.hooks[name].append(hook)
else:
LOGGER.debug("Hook already registered, skipping...")
def call(self, name, *args, **kwargs):
""" Calls all hooks under the given hook name.
:param name: name of the hooks to execute
:param args: arguments passed to the hook function
:param kwargs: keyword arguments passed to the hook function
"""
for hook in self.hooks[name]:
hook(*args, **kwargs)
def call_async(self, name, *args, **kwargs):
""" Calls all hooks under the given hook name.
The hooks MUST be celery tasks.
:param name: name of the hooks to execute
:param args: arguments passed to the hook function
:param kwargs: keyword arguments passed to the hook function
"""
kwargs['delay'] = True
self.call(name, *args, **kwargs)