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