Package dics :: Module dics
[hide private]
[frames] | no frames]

Source Code for Module dics.dics

  1  #!/usr/bin/env python 
  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  #gui commented out 
 27  #import gui 
 28  import detmech 
 29   
 30  #  
 31  import ptest 
 32   
33 -class instrument:
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 """
39 - def __init__(self):
40 """ 41 Parse the config file dics.ini and create the required instances 42 of subsystem objects. 43 """ 44 # Set up logging from the details in the config file. 45 logging.config.fileConfig('dics.ini') 46 self.logger = logging.getLogger('dazle') 47 48 # Read the config file 49 self.logger.debug('Opening config file dics.ini...') 50 try: 51 configFile = file('dics.ini') 52 except IOError: 53 # If dics.ini isn't there then we're screwed. 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 # Create any specified tc08 temperature logger instances. 61 #self.__create('tc08.tc08') 62 # Create any specified cortex-based ICU daisy chain instances. 63 self.__create('cortex.cortex') 64 # Create any specified linear mechanism instances 65 self.__create('linear.linear') 66 # Create any specified detmech instances 67 self.__create('detmech.detmech') 68 # Create any specified filterwheel instances. 69 self.__create('filterwheel.filterwheel') 70 # Create any specified target field instances 71 self.__create('rotator.target') 72 # Create any specified telescope instances 73 self.__create('rotator.telescope') 74 # Create any specified rotator mechanism instances 75 self.__create('rotator.rotator') 76 # Create any specified dcs interface instances. 77 self.__create('dcs.dcs')
78
79 - def __del__(self):
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 # Look for objects that want to write to the config, and 86 # for those that do update their entries in the config object 87 for ref in self.__dict__.values(): 88 try: 89 ref.updateConfig(self.config) 90 except AttributeError: 91 # This object doesn't want to write to config 92 pass 93 94 # Open the config file for writing. 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 # Now write the up to date config to the config file before exit 104 self.config.write(configFile) 105 self.logger.info('Config saved.')
106
107 - def __create(self, klass):
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 # Parse the klass string. 113 parts = klass.split('.') 114 class_name = parts[-1] 115 class_ref = reduce(getattr, parts[1:], globals()[parts[0]]) 116 117 # First check there is a config, and it has specified instances of 118 # the given class. 119 if isinstance(self.config, ConfigParserRT.SafeConfigParser): 120 if self.config.has_option(class_name, 'instances'): 121 # Get a list of the names to use for the instances 122 # and loop over the names creating the instances 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 # Extract the settings for the instance and turn 129 # the rather useless list of tuples format that 130 # the config module gives into a far more 131 # useful dictionary. 132 args_dict = dict(self.config.items(name)) 133 args_dict['name'] = name 134 # Now look through the values in the dictionary, 135 # and if one is a string equal to the name of 136 # an attribute of this object replace the string 137 # with a reference to the attribute. 138 for (key, value) in args_dict.iteritems(): 139 args_dict[key] = getattr(self, value, value) 140 # Now create the required instance. 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 # No instances listed in file for this class. 149 self.logger.warning(\ 150 'Trying to create %s instances but none in config file.'\ 151 % class_name) 152 else: 153 # For reson there's no config object. Something is screwed up bad 154 self.logger.critical(\ 155 'Trying to create %s instances but no config object found!' \ 156 % class_name)
157
158 - def gui(self):
159 self.g = gui.gui(self) 160 self.g.main()
161 162 163 #if __name__ == '__main__': 164 # If executed create dazle, an instance of the instrument class, 165 # then start up an XML-RPC server to expose its methods and 166 # attributes. 167 # dazle = instrument() 168 # server = SimpleXMLRPCServer.SimpleXMLRPCServer(('optics24.ast.cam.ac.uk',\ 169 # 1042)) 170 # server.register_instance(dazle) 171 # server.register_introspection_functions() 172 # server.serve_forever() 173