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