Ignore:
Timestamp:
Apr 3, 2014 5:22:58 AM (10 years ago)
Author:
Jeff Krzywon <jeffery.krzywon@…>
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
caf273b
Parents:
307fa4f
Message:

Fix for ticket #204 - processing instructions were not being set properly when saving as cansas data file.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sans/dataloader/readers/xml_reader.py

    r76cd1ae rea67541  
    1414 
    1515from lxml import etree 
    16 parser = etree.ETCompatXMLParser() 
     16parser = etree.ETCompatXMLParser(remove_comments=True, remove_pis=False) 
    1717 
    1818class XMLreader(): 
     
    2323    schema = None 
    2424    schemadoc = None 
     25    processingInstructions = None 
    2526     
    2627    def __init__(self, xml = None, schema = None, root = None): 
    2728        self.xml = xml 
    2829        self.schema = schema 
     30        self.processingInstructions = {} 
    2931        if xml is not None: 
    3032            self.setXMLFile(xml, root) 
     
    4850     
    4951    def setXMLFile(self, xml, root = None): 
     52        """ 
     53        Set the XML file and parse 
     54        """ 
    5055        try: 
    5156            self.xml = xml 
     
    5863     
    5964    def setSchema(self, schema): 
     65        """ 
     66        Set the schema file and parse 
     67        """ 
    6068        try: 
    6169            self.schema = schema 
     
    7886    def findInvalidXML(self): 
    7987        """ 
    80         Finds the first offending element that should not be present in an XML file 
     88        Finds the first offending element that should not be present in XML file 
    8189        """ 
    8290        firstError = "" 
     
    96104        self.setSchema(self.schema) 
    97105         
     106    def toString(self, etreeElement): 
     107        """ 
     108        Converts and etree element into a string 
     109        """ 
     110        return etree.tostring(etreeElement) 
     111     
     112    def setProcessingInstructions(self): 
     113        """ 
     114        Take out all processing instructions and create a dictionary from them 
     115        """ 
     116        dic = {} 
     117        pi = self.xmlroot.getprevious() 
     118        while pi is not None: 
     119            attr = {} 
     120            pi_name = "" 
     121            pi_string = self.toString(pi) 
     122            if isinstance(pi_string, str): 
     123                pi_string = pi_string.replace("<?", "").replace("?>", "") 
     124                split = pi_string.split(" ", 1) 
     125                pi_name = split[0] 
     126                attr = split[1] 
     127            new_pi_name = self._create_unique_key(dic, pi_name) 
     128            dic[new_pi_name] = attr 
     129            pi = pi.getprevious() 
     130        self.processingInstructions = dic 
     131         
     132    def _create_unique_key(self, dictionary, name, i = 0): 
     133        """ 
     134        Create a unique key value for any dictionary to prevent overwriting 
     135        Recurses until a unique key value is found. 
     136         
     137        :param dictionary: A dictionary with any number of entries 
     138        :param name: The index of the item to be added to dictionary 
     139        :param i: The number to be appended to the name, starts at 0 
     140        """ 
     141        if dictionary.get(name) is not None: 
     142            i += 1 
     143            name = name.split("_")[0] 
     144            name += "_{0}".format(i) 
     145            name = self._create_unique_key(dictionary, name, i) 
     146        return name 
     147     
     148         
Note: See TracChangeset for help on using the changeset viewer.