Changeset 79 for epics2xml/epics2xml.py


Ignore:
Timestamp:
Nov 6, 2009 10:35:02 AM (14 years ago)
Author:
jemian
Message:

Refs #2: progress ...

File:
1 edited

Legend:

Unmodified
Added
Removed
  • epics2xml/epics2xml.py

    r78 r79  
    2727from xml.dom import minidom
    2828import datetime
    29 import copy
    30 import pvConnect
     29#import copy
     30#import pvConnect
    3131
    3232
     
    4646
    4747    def GetConfigFile(self):
    48         '''@return: name of XML settings file'''
    49         return self.settingsFile
     48        '''@return: name of XML configuration file'''
     49        return self.configFile
    5050
    5151    def SetConfigFile(self, thefile):
    5252        '''set the name of XML configuration file
    5353            @param thefile: [string] name of XML file with the configuration'''
    54         self.settingsFile = thefile
     54        self.configFile = thefile
    5555
    5656    def Clear(self):
     
    5858        self.db = {}
    5959
     60    def ReadConfigFile(self):
     61        '''read the configuration from a file into an internal dictionary (self.db)
     62
     63            @note: this method uses xml.dom.minidom (built into all Pythons)
     64            @see: http://docs.python.org/library/xml.dom.minidom.html
     65        '''
     66        try:
     67            doc = minidom.parse(self.configFile) # parse an XML file by name
     68            assert doc.documentElement.tagName == self.rootElement
     69        except IOError:
     70            return 'Could not read the XML file: ' + self.configFile
     71        except AssertionError:
     72            return 'XML root element is not ' + self.rootElement
     73        #... read all attributes from the root element
     74        docElem = doc.documentElement
     75        self.Clear()
     76        version = self._get_attribute(docElem, "version", "not-specified")
     77        try:
     78            # only handle v1.0 resource configuration files
     79            assert version == u'1.0'
     80        except AssertionError:
     81            doc.unlink()
     82            return 'Cannot handle file version:', version
     83        # file verified now
     84
    6085    def ReadXmlFile(self):
    6186        '''read the settings from a file into an internal dictionary (self.db)
     
    6590        '''
    6691        try:
    67             doc = minidom.parse(self.settingsFile) # parse an XML file by name
     92            doc = minidom.parse(self.configFile) # parse an XML file by name
    6893            assert doc.documentElement.tagName == self.rootElement
    6994        except IOError:
    70             return 'Could not read the XML file: ' + self.settingsFile
     95            return 'Could not read the XML file: ' + self.configFile
    7196        except AssertionError:
    7297            return 'XML root element is not ' + self.rootElement
     
    134159           @see: http://www.boddie.org.uk/python/XML_intro.html
    135160        '''
    136         out = open(self.settingsFile, 'w')
     161        out = open(self.configFile, 'w')
    137162        out.write(repr(self))
    138163        out.close()
     
    141166        #
    142167       
    143         return 'Saved settings to ' + self.settingsFile
     168        return 'Saved settings to ' + self.configFile
    144169
    145170    def _SetAttr(self, node, attribute, value):
     
    227252
    228253if __name__ == '__main__':
     254    cf = Epics2Xml("config.xml")
     255    cf.ReadConfigFile()
    229256#    rc = Settings("examples/test-settings.xml")
    230257#    rc.ReadXmlFile()
Note: See TracChangeset for help on using the changeset viewer.