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