1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
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
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
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
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