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