From c06bbe99734f2fea35cfb4bdd854814c9119b617 Mon Sep 17 00:00:00 2001 From: Nachi Ueno Date: Fri, 19 Aug 2011 12:30:55 -0700 Subject: Added monkey patching notification code function w --- nova/utils.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'nova/utils.py') diff --git a/nova/utils.py b/nova/utils.py index 7276b6bd5..8024a3517 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 @@ -634,7 +635,7 @@ def synchronized(name, external=False): Different methods can share the same lock: @synchronized('mylock') - def foo(self, *args): + Gdef foo(self, *args): ... @synchronized('mylock') @@ -873,3 +874,24 @@ class Bootstrapper(object): for key in FLAGS: value = FLAGS.get(key, None) logging.audit(_("%(key)s : %(value)s" % locals())) + + +def monkey_patch(): + if not FLAGS.monkey_patch: + return + for module_and_decorator in FLAGS.monkey_patch_modules: + module, decorator_name = module_and_decorator.split(':') + decorator = import_class(decorator_name) + __import__(module) + module_data = pyclbr.readmodule_ex(module) + for key in module_data.keys(): + 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" % (module, key), func)) + if isinstance(module_data[key], pyclbr.Function): + func = import_class("%s.%s" % (module, key)) + setattr(sys.modules[module], key,\ + setattr(sys.modules[module], key, \ + decorator("%s.%s" % (module, key), func))) -- cgit From e10aa40bd6c2f96b2f5bba8b38b9605f019328e9 Mon Sep 17 00:00:00 2001 From: Nachi Ueno Date: Fri, 19 Aug 2011 14:22:53 -0700 Subject: Fixed typo --- nova/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'nova/utils.py') diff --git a/nova/utils.py b/nova/utils.py index 171035068..d7e14b1b0 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -636,7 +636,7 @@ def synchronized(name, external=False): Different methods can share the same lock: @synchronized('mylock') - Gdef foo(self, *args): + def foo(self, *args): ... @synchronized('mylock') -- cgit From 5a288485215a13f3892ae17a46b9644ed84fc089 Mon Sep 17 00:00:00 2001 From: Nachi Ueno Date: Mon, 22 Aug 2011 14:24:37 -0700 Subject: Added Test Code, doc string, and fixed pip-requiresw --- nova/utils.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'nova/utils.py') diff --git a/nova/utils.py b/nova/utils.py index d7e14b1b0..be2ba68f9 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -842,21 +842,41 @@ def bool_from_str(val): 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 moudles 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) + # Retrive 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" % (module, key), func)) + 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,\ - setattr(sys.modules[module], key, \ - decorator("%s.%s" % (module, key), func))) + decorator("%s.%s" % (module, key), func)) -- cgit From 5ae44219fd82d843cc5e715c318d9e80ab20b1a2 Mon Sep 17 00:00:00 2001 From: Nachi Ueno Date: Tue, 23 Aug 2011 08:07:25 -0700 Subject: Fixed typo and docstring and example class name --- nova/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'nova/utils.py') diff --git a/nova/utils.py b/nova/utils.py index 44a0d4398..edf67384d 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -866,13 +866,13 @@ def monkey_patch(): # If FLAGS.monkey_patch is not True, this function do nothing. if not FLAGS.monkey_patch: return - # Get list of moudles and decorators + # 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) - # Retrive module information using pyclbr + # Retrieve module information using pyclbr module_data = pyclbr.readmodule_ex(module) for key in module_data.keys(): # set the decorator for the class methods -- cgit From f380b65cdce439d440b68b0f4a65be45d13ce453 Mon Sep 17 00:00:00 2001 From: Nachi Ueno Date: Tue, 23 Aug 2011 08:51:44 -0700 Subject: Removed blank line --- nova/utils.py | 1 - 1 file changed, 1 deletion(-) (limited to 'nova/utils.py') diff --git a/nova/utils.py b/nova/utils.py index 5c7d52c70..21e6221b2 100644 --- a/nova/utils.py +++ b/nova/utils.py @@ -851,7 +851,6 @@ def is_valid_ipv4(address): """valid the address strictly as per format xxx.xxx.xxx.xxx. where xxx is a value between 0 and 255. """ - parts = address.split(".") if len(parts) != 4: return False -- cgit