diff options
| author | Nachi Ueno <ueno.nachi@lab.ntt.co.jp> | 2011-08-23 20:04:53 +0000 |
|---|---|---|
| committer | Tarmac <> | 2011-08-23 20:04:53 +0000 |
| commit | 474d394ebb077bb2cfc778d67ab4b1e3ccc7ceb0 (patch) | |
| tree | c3ef409ab9a567f8646a8278433e2b41256a3753 /nova/utils.py | |
| parent | 731d4e7f7631e8a8a303bef7779e3f0e513332ae (diff) | |
| parent | 6bbef7627200f6c6ef27b5ae5c9b114e8e6d0f52 (diff) | |
I added notifications decorator for each API call using monkey_patching.
By this merge, users can get API call notification from any modules.
Diffstat (limited to 'nova/utils.py')
| -rw-r--r-- | nova/utils.py | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/nova/utils.py b/nova/utils.py index fc4bbd53b..21e6221b2 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -35,6 +35,7 @@ import sys import time import types import uuid +import pyclbr from xml.sax import saxutils from eventlet import event @@ -860,3 +861,43 @@ def is_valid_ipv4(address): except ValueError: return False return True + + +def monkey_patch(): + """ If the Flags.monkey_patch set as True, + this functuion patches a decorator + for all functions in specified modules. + You can set decorators for each modules + using FLAGS.monkey_patch_modules. + The format is "Module path:Decorator function". + Example: 'nova.api.ec2.cloud:nova.notifier.api.notify_decorator' + + Parameters of the decorator is as follows. + (See nova.notifier.api.notify_decorator) + + name - name of the function + function - object of the function + """ + # If FLAGS.monkey_patch is not True, this function do nothing. + if not FLAGS.monkey_patch: + return + # Get list of modules and decorators + for module_and_decorator in FLAGS.monkey_patch_modules: + module, decorator_name = module_and_decorator.split(':') + # import decorator function + decorator = import_class(decorator_name) + __import__(module) + # Retrieve module information using pyclbr + module_data = pyclbr.readmodule_ex(module) + for key in module_data.keys(): + # set the decorator for the class methods + if isinstance(module_data[key], pyclbr.Class): + clz = import_class("%s.%s" % (module, key)) + for method, func in inspect.getmembers(clz, inspect.ismethod): + setattr(clz, method,\ + decorator("%s.%s.%s" % (module, key, method), func)) + # set the decorator for the function + if isinstance(module_data[key], pyclbr.Function): + func = import_class("%s.%s" % (module, key)) + setattr(sys.modules[module], key,\ + decorator("%s.%s" % (module, key), func)) |
