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$" 
 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
53 -def reflectCallCatching(err, moduleName, methodName, *args, **kwargs):
54 """ 55 @param err: The type of error to throw 56 @type err: Exception 57 @param moduleName: name of the module to load 58 @type moduleName: string 59 @param methodName: name of the function to call 60 @type methodName: string 61 62 Invokes a function in a given module, marshalling all errors to be 63 of a certain type. 64 """ 65 66 log.debug('reflectcall', 'Loading moduleName %s' % moduleName) 67 68 try: 69 module = reflect.namedModule(moduleName) 70 except ValueError: 71 raise err("module %s could not be found" % moduleName) 72 except SyntaxError, e: 73 raise err("module %s has a syntax error in %s:%d" 74 % (moduleName, e.filename, e.lineno)) 75 except ImportError, e: 76 # FIXME: basically this is the same as the generic one below... 77 raise err("module %s could not be imported (%s)" 78 % (moduleName, 79 log.getExceptionMessage(e, filename='flumotion'))) 80 except Exception, e: 81 raise err("module %s could not be imported (%s)" 82 % (moduleName, 83 log.getExceptionMessage(e, filename='flumotion'))) 84 85 if not hasattr(module, methodName): 86 raise err("module %s has no method named %s" 87 % (moduleName, methodName)) 88 89 log.debug('reflectcall', 'calling method %s.%s' 90 % (moduleName, methodName)) 91 92 try: 93 ret = getattr(module, methodName)(*args, **kwargs) 94 except err: 95 # already nicely formatted, so fall through 96 log.debug('reflectcall', 'letting error fall through') 97 raise 98 except Exception, e: 99 msg = log.getExceptionMessage(e) 100 log.warning('reflectcall', msg) 101 log.warning('reflectcall', 'raising error') 102 raise err(msg) 103 104 log.debug('reflectcall', 'returning %r' % ret) 105 106 return ret
107 108
109 -def createComponent(moduleName, methodName, config):
110 """ 111 @param moduleName: name of the module to create the component from 112 @type moduleName: string 113 @param methodName: the factory method to use to create the component 114 @type methodName: string 115 @param config: the component's config dict 116 @type config: dict 117 118 Invokes the entry point for a component in the given module using the 119 given factory method, thus creating the component. 120 121 @rtype: L{flumotion.component.component.BaseComponent} 122 """ 123 return reflectCallCatching(errors.ComponentCreateError, 124 moduleName, methodName, config)
125