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  """ 
 23  getting coherent errors when calling procedures in named modules 
 24  """ 
 25   
 26  from twisted.python import reflect 
 27   
 28  from flumotion.common import errors, log 
 29   
 30   
31 -def reflectCallCatching(err, moduleName, methodName, *args, **kwargs):
32 """ 33 @param err: The type of error to throw 34 @type err: Exception 35 @param moduleName: name of the module to load 36 @type moduleName: string 37 @param methodName: name of the function to call 38 @type methodName: string 39 40 Invokes a function in a given module, marshalling all errors to be 41 of a certain type. 42 """ 43 44 log.debug('reflectcall', 'Loading moduleName %s' % moduleName) 45 46 try: 47 module = reflect.namedModule(moduleName) 48 except ValueError: 49 raise err("module %s could not be found" % moduleName) 50 except SyntaxError, e: 51 raise err("module %s has a syntax error in %s:%d" 52 % (moduleName, e.filename, e.lineno)) 53 except ImportError, e: 54 # FIXME: basically this is the same as the generic one below... 55 raise err("module %s could not be imported (%s)" 56 % (moduleName, 57 log.getExceptionMessage(e, filename='flumotion'))) 58 except Exception, e: 59 raise err("module %s could not be imported (%s)" 60 % (moduleName, 61 log.getExceptionMessage(e, filename='flumotion'))) 62 63 if not hasattr(module, methodName): 64 raise err("module %s has no method named %s" 65 % (moduleName, methodName)) 66 67 log.debug('reflectcall', 'calling method %s.%s' 68 % (moduleName, methodName)) 69 70 try: 71 ret = getattr(module, methodName)(*args, **kwargs) 72 except err: 73 # already nicely formatted, so fall through 74 log.debug('reflectcall', 'letting error fall through') 75 raise 76 except Exception, e: 77 msg = log.getExceptionMessage(e) 78 log.warning('reflectcall', msg) 79 log.warning('reflectcall', 'raising error') 80 raise err(msg) 81 82 log.debug('reflectcall', 'returning %r' % ret) 83 84 return ret
85
86 -def createComponent(moduleName, methodName):
87 """ 88 @param moduleName: name of the module to create the component from 89 @type moduleName: string 90 @param methodName: the factory method to use to create the component 91 @type methodName: string 92 93 Invokes the entry point for a component in the given module using the 94 given factory method, thus creating the component. 95 96 @rtype: L{flumotion.component.component.BaseComponent} 97 """ 98 return reflectCallCatching(errors.ComponentCreateError, 99 moduleName, methodName)
100