/usr/lib/python2.7/dist-packages/cement/core/hook.py is in python-cement 2.10.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | """Cement core hooks module."""
import operator
import types
from ..core import exc, backend
from ..utils.misc import minimal_logger
LOG = minimal_logger(__name__)
class HookManager(object):
"""
Manages the hook system to define, get, run, etc hooks within the
the Cement Framework and applications Built on Cement (tm).
:param use_backend_globals: Whether to use backend globals (backward
compatibility and deprecated).
"""
def __init__(self, use_backend_globals=False):
if use_backend_globals is True:
self.__hooks__ = backend.__hooks__
else:
self.__hooks__ = {}
def define(self, name):
"""
Define a hook namespace that the application and plugins can register
hooks in.
:param name: The name of the hook, stored as hooks['name']
:raises: cement.core.exc.FrameworkError
Usage:
.. code-block:: python
from cement.core.foundation import CementApp
with CementApp('myapp') as app:
app.hook.define('my_hook_name')
"""
LOG.debug("defining hook '%s'" % name)
if name in self.__hooks__:
raise exc.FrameworkError("Hook name '%s' already defined!" % name)
self.__hooks__[name] = []
def defined(self, hook_name):
"""
Test whether a hook name is defined.
:param hook_name: The name of the hook.
I.e. ``my_hook_does_awesome_things``.
:returns: True if the hook is defined, False otherwise.
:rtype: ``boolean``
Usage:
.. code-block:: python
from cement.core.foundation import CementApp
with CementApp('myapp') as app:
app.hook.defined('some_hook_name'):
# do something about it
pass
"""
if hook_name in self.__hooks__:
return True
else:
return False
def register(self, name, func, weight=0):
"""
Register a function to a hook. The function will be called, in order
of weight, when the hook is run.
:param name: The name of the hook to register too.
I.e. ``pre_setup``, ``post_run``, etc.
:param func: The function to register to the hook. This is an
*un-instantiated*, non-instance method, simple function.
:param weight: The weight in which to order the hook function.
:type weight: ``int``
Usage:
.. code-block:: python
from cement.core.foundation import CementApp
def my_hook_func(app):
# do something with app?
return True
with CementApp('myapp') as app:
app.hook.define('my_hook_name')
app.hook.register('my_hook_name', my_hook_func)
"""
if name not in self.__hooks__:
LOG.debug("hook name '%s' is not defined! ignoring..." % name)
return False
LOG.debug("registering hook '%s' from %s into hooks['%s']" %
(func.__name__, func.__module__, name))
# Hooks are as follows: (weight, name, func)
self.__hooks__[name].append((int(weight), func.__name__, func))
def run(self, name, *args, **kwargs):
"""
Run all defined hooks in the namespace. Yields the result of each
hook function run.
:param name: The name of the hook function.
:param args: Additional arguments to be passed to the hook functions.
:param kwargs: Additional keyword arguments to be passed to the hook
functions.
:raises: FrameworkError
Usage:
.. code-block:: python
from cement.core.foundation import CementApp
def my_hook_func(app):
# do something with app?
return True
with CementApp('myapp') as app:
app.hook.define('my_hook_name')
app.hook.register('my_hook_name', my_hook_func)
for res in app.hook.run('my_hook_name', self):
# do something with the result?
pass
"""
if name not in self.__hooks__:
raise exc.FrameworkError("Hook name '%s' is not defined!" % name)
# Will order based on weight (the first item in the tuple)
self.__hooks__[name].sort(key=operator.itemgetter(0))
for hook in self.__hooks__[name]:
LOG.debug("running hook '%s' (%s) from %s" %
(name, hook[2], hook[2].__module__))
res = hook[2](*args, **kwargs)
# Check if result is a nested generator - needed to support e.g.
# asyncio
if isinstance(res, types.GeneratorType):
for _res in res:
yield _res
else:
yield res
# the following is only used for backward compat with < 2.7.x!
def define(name):
"""
DEPRECATION WARNING: This function is deprecated as of Cement 2.7.x and
will be removed in future versions of Cement.
Use ``CementApp.hook.define()`` instead.
---
Define a hook namespace that plugins can register hooks in.
:param name: The name of the hook, stored as hooks['name']
:raises: cement.core.exc.FrameworkError
Usage:
.. code-block:: python
from cement.core import hook
hook.define('myhookname_hook')
"""
# only log debug for now as this won't be removed until Cement 3.x and
# we don't have access to CementApp.Meta.ignore_deprecation_warnings here
LOG.debug(
'Cement Deprecation Warning: `hook.define()` has been deprecated, '
'and will be removed in future versions of Cement. You should now '
'use `CementApp.hook.define()` instead.'
)
LOG.debug("defining hook '%s'" % name)
if name in backend.__hooks__:
raise exc.FrameworkError("Hook name '%s' already defined!" % name)
backend.__hooks__[name] = []
def defined(hook_name):
"""
DEPRECATION WARNING: This function is deprecated as of Cement 2.7.x and
will be removed in future versions of Cement.
Use ``CementApp.hook.defined()`` instead.
---
Test whether a hook name is defined.
:param hook_name: The name of the hook.
I.e. ``my_hook_does_awesome_things``.
:returns: True if the hook is defined, False otherwise.
:rtype: ``boolean``
"""
# only log debug for now as this won't be removed until Cement 3.x and
# we don't have access to CementApp.Meta.ignore_deprecation_warnings here
LOG.debug(
'Cement Deprecation Warning: `hook.defined()` has been deprecated, '
'and will be removed in future versions of Cement. You should now '
'use `CementApp.hook.defined()` instead.'
)
if hook_name in backend.__hooks__:
return True
else:
return False
def register(name, func, weight=0):
"""
DEPRECATION WARNING: This function is deprecated as of Cement 2.7.x and
will be removed in future versions of Cement.
Use ``CementApp.hook.register()`` instead.
---
Register a function to a hook. The function will be called, in order of
weight, when the hook is run.
:param name: The name of the hook to register too. I.e. ``pre_setup``,
``post_run``, etc.
:param func: The function to register to the hook. This is an
*un-instantiated*, non-instance method, simple function.
:param weight: The weight in which to order the hook function.
:type weight: ``int``
Usage:
.. code-block:: python
from cement.core import hook
def my_hook(*args, **kwargs):
# do something here
res = 'Something to return'
return res
hook.register('post_setup', my_hook)
"""
# only log debug for now as this won't be removed until Cement 3.x and
# we don't have access to CementApp.Meta.ignore_deprecation_warnings here
LOG.debug(
'Cement Deprecation Warning: `hook.register()` has been deprecated, '
'and will be removed in future versions of Cement. You should now '
'use `CementApp.hook.register()` instead.'
)
if name not in backend.__hooks__:
LOG.debug("hook name '%s' is not defined! ignoring..." % name)
return False
LOG.debug("registering hook '%s' from %s into hooks['%s']" %
(func.__name__, func.__module__, name))
# Hooks are as follows: (weight, name, func)
backend.__hooks__[name].append((int(weight), func.__name__, func))
def run(name, *args, **kwargs):
"""
DEPRECATION WARNING: This function is deprecated as of Cement 2.7.x and
will be removed in future versions of Cement.
Use ``CementApp.hook.run()`` instead.
---
Run all defined hooks in the namespace. Yields the result of each hook
function run.
:param name: The name of the hook function.
:param args: Additional arguments to be passed to the hook functions.
:param kwargs: Additional keyword arguments to be passed to the hook
functions.
:raises: FrameworkError
Usage:
.. code-block:: python
from cement.core import hook
for result in hook.run('hook_name'):
# do something with result from each hook function
...
"""
# only log debug for now as this won't be removed until Cement 3.x and
# we don't have access to CementApp.Meta.ignore_deprecation_warnings here
LOG.debug(
'Cement Deprecation Warning: `hook.run()` has been deprecated, '
'and will be removed in future versions of Cement. You should now '
'use `CementApp.hook.run()` instead.'
)
if name not in backend.__hooks__:
raise exc.FrameworkError("Hook name '%s' is not defined!" % name)
# Will order based on weight (the first item in the tuple)
backend.__hooks__[name].sort(key=operator.itemgetter(0))
for hook in backend.__hooks__[name]:
LOG.debug("running hook '%s' (%s) from %s" %
(name, hook[2], hook[2].__module__))
res = hook[2](*args, **kwargs)
# Check if result is a nested generator - needed to support e.g.
# asyncio
if isinstance(res, types.GeneratorType):
for _res in res:
yield _res
else:
yield res
|