1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 from elisa.core.tests import elisa_test_case
17 import sys, os
18 import inspect
19 from twisted.internet import defer
20
22
23 component_class = None
24 component_path = '.'
25 logged = False
26
27 - def __init__(self, methodName='runTest'):
38
40 component = None
41 if self.component_class:
42 if isinstance(self.component_class, basestring):
43
44
45 sys_path = os.getcwd()
46 if self.component_path == '.':
47 sys_path = inspect.getsourcefile(self.__class__)
48 sys_path = os.path.dirname(sys_path)
49 elif self.component_path.startswith('..'):
50 sys_path = inspect.getsourcefile(self.__class__)
51 sys_path = os.path.dirname(sys_path)
52 sys_path = os.path.dirname(sys_path[:-1])
53 index = self.component_path.find(os.path.sep)
54 if index > -1:
55 dirname = self.component_path[index+1:]
56 sys_path = os.path.join(sys_path, dirname)
57
58 self.debug('Appending %r to sys.path', sys_path)
59 sys.path.insert(0, sys_path)
60
61
62 path = self.component_class
63 mod_class = path.split(':')
64
65 try:
66 module = __import__(mod_class[0], globals(),
67 locals(), [mod_class[1]])
68 except ImportError:
69 pass
70 else:
71 Class = getattr(module, mod_class[1])
72
73 self.component_class = Class
74 component = Class()
75
76
77 sys.path.remove(sys_path)
78
79 else:
80 component = self.component_class()
81
82
83
84 from elisa.core import common
85 app = common.application
86 if app:
87 self.debug("Initializing %r component", component.name)
88 component.load_config(app.config)
89 dfr = defer.maybeDeferred(component.initialize)
90 dfr.addCallback(lambda result: component)
91
92 return dfr
93 else:
94 self.debug("Application not set, skipping component initialization")
95 return component
96
103
106