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