1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 """getting coherent errors when calling procedures in named modules
19 """
20
21 from twisted.python import reflect
22
23 from flumotion.common import errors, log
24
25 __version__ = "$Rev$"
26
27
28 -def reflectCall(moduleName, methodName, *args, **kwargs):
29 """
30 @param moduleName: name of the module to load
31 @type moduleName: string
32 @param methodName: name of the function to call
33 @type methodName: string
34
35 Invokes a function in a given module.
36 """
37
38 log.debug('reflectcall', 'Loading moduleName %s', moduleName)
39
40 module = reflect.namedModule(moduleName)
41
42 log.debug('reflectcall', 'calling method %s.%s', moduleName,
43 methodName)
44
45 proc = getattr(module, methodName)
46 return proc(*args, **kwargs)
47
48
50 """
51 @param err: The type of error to throw
52 @type err: Exception
53 @param moduleName: name of the module to load
54 @type moduleName: string
55 @param methodName: name of the function to call
56 @type methodName: string
57
58 Invokes a function in a given module, marshalling all errors to be
59 of a certain type.
60 """
61
62 log.debug('reflectcall', 'Loading moduleName %s' % moduleName)
63
64 try:
65 module = reflect.namedModule(moduleName)
66 except ValueError:
67 raise err("module %s could not be found" % moduleName)
68 except SyntaxError, e:
69 raise err("module %s has a syntax error in %s:%d"
70 % (moduleName, e.filename, e.lineno))
71 except ImportError, e:
72
73 raise err("module %s could not be imported (%s)"
74 % (moduleName,
75 log.getExceptionMessage(e, filename='flumotion')))
76 except Exception, e:
77 raise err("module %s could not be imported (%s)"
78 % (moduleName,
79 log.getExceptionMessage(e, filename='flumotion')))
80
81 if not hasattr(module, methodName):
82 raise err("module %s has no method named %s"
83 % (moduleName, methodName))
84
85 log.debug('reflectcall', 'calling method %s.%s'
86 % (moduleName, methodName))
87
88 try:
89 ret = getattr(module, methodName)(*args, **kwargs)
90 except err:
91
92 log.debug('reflectcall', 'letting error fall through')
93 raise
94 except Exception, e:
95 msg = log.getExceptionMessage(e)
96 log.warning('reflectcall', msg)
97 log.warning('reflectcall', 'raising error')
98 raise err(msg)
99
100 log.debug('reflectcall', 'returning %r' % ret)
101
102 return ret
103
104
106 """
107 @param moduleName: name of the module to create the component from
108 @type moduleName: string
109 @param methodName: the factory method to use to create the component
110 @type methodName: string
111 @param config: the component's config dict
112 @type config: dict
113
114 Invokes the entry point for a component in the given module using the
115 given factory method, thus creating the component.
116
117 @rtype: L{flumotion.component.component.BaseComponent}
118 """
119 return reflectCallCatching(errors.ComponentCreateError,
120 moduleName, methodName, config)
121