Package flumotion :: Package common :: Module reflectcall
[hide private]

Source Code for Module flumotion.common.reflectcall

  1  # -*- Mode: Python -*- 
  2  # vi:si:et:sw=4:sts=4:ts=4 
  3  # 
  4  # Flumotion - a streaming media server 
  5  # Copyright (C) 2004,2005,2006,2007 Fluendo, S.L. (www.fluendo.com). 
  6  # All rights reserved. 
  7   
  8  # This file may be distributed and/or modified under the terms of 
  9  # the GNU General Public License version 2 as published by 
 10  # the Free Software Foundation. 
 11  # This file is distributed without any warranty; without even the implied 
 12  # warranty of merchantability or fitness for a particular purpose. 
 13  # See "LICENSE.GPL" in the source distribution for more information. 
 14   
 15  # Licensees having purchased or holding a valid Flumotion Advanced 
 16  # Streaming Server license may use this file in accordance with the 
 17  # Flumotion Advanced Streaming Server Commercial License Agreement. 
 18  # See "LICENSE.Flumotion" in the source distribution for more information. 
 19   
 20  # Headers in this file shall remain intact. 
 21   
 22  """getting coherent errors when calling procedures in named modules 
 23  """ 
 24   
 25  from twisted.python import reflect 
 26   
 27  from flumotion.common import errors, log 
 28   
 29  __version__ = "$Rev: 6964 $" 
 30   
 31   
32 -def reflectCall(moduleName, methodName, *args, **kwargs):
33 """ 34 @param moduleName: name of the module to load 35 @type moduleName: string 36 @param methodName: name of the function to call 37 @type methodName: string 38 39 Invokes a function in a given module. 40 """ 41 42 log.debug('reflectcall', 'Loading moduleName %s', moduleName) 43 44 module = reflect.namedModule(moduleName) 45 46 log.debug('reflectcall', 'calling method %s.%s', moduleName, 47 methodName) 48 49 proc = getattr(module, methodName) 50 return proc(*args, **kwargs)
51
52 -def reflectCallCatching(err, moduleName, methodName, *args, **kwargs):
53 """ 54 @param err: The type of error to throw 55 @type err: Exception 56 @param moduleName: name of the module to load 57 @type moduleName: string 58 @param methodName: name of the function to call 59 @type methodName: string 60 61 Invokes a function in a given module, marshalling all errors to be 62 of a certain type. 63 """ 64 65 log.debug('reflectcall', 'Loading moduleName %s' % moduleName) 66 67 try: 68 module = reflect.namedModule(moduleName) 69 except ValueError: 70 raise err("module %s could not be found" % moduleName) 71 except SyntaxError, e: 72 raise err("module %s has a syntax error in %s:%d" 73 % (moduleName, e.filename, e.lineno)) 74 except ImportError, e: 75 # FIXME: basically this is the same as the generic one below... 76 raise err("module %s could not be imported (%s)" 77 % (moduleName, 78 log.getExceptionMessage(e, filename='flumotion'))) 79 except Exception, e: 80 raise err("module %s could not be imported (%s)" 81 % (moduleName, 82 log.getExceptionMessage(e, filename='flumotion'))) 83 84 if not hasattr(module, methodName): 85 raise err("module %s has no method named %s" 86 % (moduleName, methodName)) 87 88 log.debug('reflectcall', 'calling method %s.%s' 89 % (moduleName, methodName)) 90 91 try: 92 ret = getattr(module, methodName)(*args, **kwargs) 93 except err: 94 # already nicely formatted, so fall through 95 log.debug('reflectcall', 'letting error fall through') 96 raise 97 except Exception, e: 98 msg = log.getExceptionMessage(e) 99 log.warning('reflectcall', msg) 100 log.warning('reflectcall', 'raising error') 101 raise err(msg) 102 103 log.debug('reflectcall', 'returning %r' % ret) 104 105 return ret
106
107 -def createComponent(moduleName, methodName, config):
108 """ 109 @param moduleName: name of the module to create the component from 110 @type moduleName: string 111 @param methodName: the factory method to use to create the component 112 @type methodName: string 113 @param config: the component's config dict 114 @type config: dict 115 116 Invokes the entry point for a component in the given module using the 117 given factory method, thus creating the component. 118 119 @rtype: L{flumotion.component.component.BaseComponent} 120 """ 121 return reflectCallCatching(errors.ComponentCreateError, 122 moduleName, methodName, config)
123