1
2 """
3 DICS: DAZLE Instrument Control Python module
4
5 useage:
6 import dics
7 dazle=dics.instrument()
8
9 History
10 2005XXXX original by
11 20060605 rgm added this documentation
12 20060606
13
14 """
15 import ConfigParserRT
16 import cortex
17 import dcs
18 import filterwheel
19 import linear
20 import logging
21 import logging.config
22 import mechanism
23 import rotator
24 import SimpleXMLRPCServer
25 import tc08
26
27
28 import detmech
29
30
31 import ptest
32
34 """
35 Class representing the instrument as a whole. Provides methods
36 for the highest level operations and has as members objects
37 representing the sub-systems of the instruments.
38 """
40 """
41 Parse the config file dics.ini and create the required instances
42 of subsystem objects.
43 """
44
45 logging.config.fileConfig('dics.ini')
46 self.logger = logging.getLogger('dazle')
47
48
49 self.logger.debug('Opening config file dics.ini...')
50 try:
51 configFile = file('dics.ini')
52 except IOError:
53
54 self.logger.critical('Failed to open config file dics.ini!')
55 return
56 self.config = ConfigParserRT.SafeConfigParser()
57 self.config.readfp(configFile)
58 configFile.close()
59
60
61
62
63 self.__create('cortex.cortex')
64
65 self.__create('linear.linear')
66
67 self.__create('detmech.detmech')
68
69 self.__create('filterwheel.filterwheel')
70
71 self.__create('rotator.target')
72
73 self.__create('rotator.telescope')
74
75 self.__create('rotator.rotator')
76
77 self.__create('dcs.dcs')
78
80 """
81 Several types of subsystems need to store status/config information
82 in the dics.ini file when the program is shut down.
83 """
84 self.logger.debug('__del__ called')
85
86
87 for ref in self.__dict__.values():
88 try:
89 ref.updateConfig(self.config)
90 except AttributeError:
91
92 pass
93
94
95 self.logger.info('Opening config file dics.ini...')
96 try:
97 configFile = file('dics.ini', 'w')
98 except IOError:
99 self.logger.critical('Failed to open config file dics.ini!')
100 self.logger.critical('Unable to write status/config data.')
101 return
102
103
104 self.config.write(configFile)
105 self.logger.info('Config saved.')
106
108 """
109 Creates all instances of the class passed as an argument that are
110 specified in the ConfigParser.SafeConfigParser object self.config.
111 """
112
113 parts = klass.split('.')
114 class_name = parts[-1]
115 class_ref = reduce(getattr, parts[1:], globals()[parts[0]])
116
117
118
119 if isinstance(self.config, ConfigParserRT.SafeConfigParser):
120 if self.config.has_option(class_name, 'instances'):
121
122
123 for name in \
124 self.config.get(class_name, 'instances').split(','):
125 if self.config.has_section(name):
126 self.logger.info('Creating %s object %s' % \
127 (class_name, name))
128
129
130
131
132 args_dict = dict(self.config.items(name))
133 args_dict['name'] = name
134
135
136
137
138 for (key, value) in args_dict.iteritems():
139 args_dict[key] = getattr(self, value, value)
140
141 setattr(self, name, class_ref(**args_dict))
142
143 else:
144 self.logger.warning(\
145 '%s instance %s listed but has no section.' \
146 % (class_name, name))
147 else:
148
149 self.logger.warning(\
150 'Trying to create %s instances but none in config file.'\
151 % class_name)
152 else:
153
154 self.logger.critical(\
155 'Trying to create %s instances but no config object found!' \
156 % class_name)
157
161
162
163
164
165
166
167
168
169
170
171
172
173