Changeset 5ce7f17 in sasview for src/sas/dataloader/readers


Ignore:
Timestamp:
Mar 27, 2015 11:05:11 AM (10 years ago)
Author:
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:
cce0ad3
Parents:
a862cea0
Message:

Fix for ticket #395 - Saving a project now saves all fits and loading a
project no longer throws an error by trying to find deprecated fitting
engines.

Location:
src/sas/dataloader/readers
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/sas/dataloader/readers/cansas_reader.py

    r682c432 r5ce7f17  
    322322                value_unit = local_unit 
    323323            except: 
    324                 print sys.exc_info() 
    325324                err_msg = "CanSAS reader: unknown error converting " 
    326325                err_msg += "\"{0}\" unit [{1}]" 
     
    990989                self.append(node, entry_node) 
    991990 
    992     def _check_origin(self, entry_node, doc): 
     991    def _check_origin(self, entry_node, doc, frm): 
    993992        """ 
    994993        Return the document, and the SASentry node associated with 
     
    1000999        :param doc: entire xml tree 
    10011000        """ 
    1002         frm = inspect.stack()[1] 
     1001        if not frm: 
     1002            frm = inspect.stack()[1] 
    10031003        mod_name = frm[1].replace("\\", "/").replace(".pyc", "") 
    10041004        mod_name = mod_name.replace(".py", "") 
     
    10111011            node_list = doc.getElementsByTagName(node_name) 
    10121012            entry_node = node_list.item(0) 
    1013         return entry_node 
     1013        return doc, entry_node 
    10141014 
    10151015    def _to_xml_doc(self, datainfo): 
     
    10611061        # If the calling function was not the cansas reader, return a minidom 
    10621062        #      object rather than an lxml object. 
    1063         entry_node = self._check_origin(entry_node, doc) 
     1063        frm = inspect.stack()[1] 
     1064        doc, entry_node = self._check_origin(entry_node, doc, frm) 
    10641065        return doc, entry_node 
    10651066 
  • src/sas/dataloader/readers/xml_reader.py

    r79492222 r5ce7f17  
    11""" 
    22    Generic XML read and write utility 
    3      
     3 
    44    Usage: Either extend xml_reader or add as a class variable. 
    55""" 
     
    77#This software was developed by the University of Tennessee as part of the 
    88#Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 
    9 #project funded by the US National Science Foundation.  
    10 #If you use DANSE applications to do scientific research that leads to  
    11 #publication, we ask that you acknowledge the use of the software with the  
     9#project funded by the US National Science Foundation. 
     10#If you use DANSE applications to do scientific research that leads to 
     11#publication, we ask that you acknowledge the use of the software with the 
    1212#following sentence: 
    13 #This work benefited from DANSE software developed under NSF award DMR-0520547.  
     13#This work benefited from DANSE software developed under NSF award DMR-0520547. 
    1414#copyright 2008,2009 University of Tennessee 
    1515############################################################################# 
     
    2525    Generic XML read and write class. Mostly helper functions. 
    2626    Makes reading/writing XML a bit easier than calling lxml libraries directly. 
    27      
     27 
    2828    :Dependencies: 
    2929        This class requires lxml 2.3 or higher. 
    3030    """ 
    31      
     31 
    3232    xml = None 
    3333    xmldoc = None 
     
    3737    encoding = None 
    3838    processing_instructions = None 
    39      
    40     def __init__(self, xml = None, schema = None): 
     39 
     40    def __init__(self, xml=None, schema=None): 
    4141        self.xml = xml 
    4242        self.schema = schema 
     
    5151        else: 
    5252            self.schemadoc = None 
    53      
     53 
    5454    def reader(self): 
    5555        """ 
     
    5757        """ 
    5858        if self.validate_xml(): 
    59             self.xmldoc = etree.parse(self.xml, parser = PARSER) 
     59            self.xmldoc = etree.parse(self.xml, parser=PARSER) 
    6060        else: 
    6161            raise etree.XMLSchemaValidateError(self, self.find_invalid_xml()) 
    6262        return self.xmldoc 
    63      
     63 
    6464    def set_xml_file(self, xml): 
    6565        """ 
     
    6868        try: 
    6969            self.xml = xml 
    70             self.xmldoc = etree.parse(self.xml, parser = PARSER) 
     70            self.xmldoc = etree.parse(self.xml, parser=PARSER) 
    7171            self.xmlroot = self.xmldoc.getroot() 
    7272        except etree.XMLSyntaxError as xml_error: 
     
    7676            self.xmldoc = None 
    7777            self.xmlroot = None 
    78      
     78 
    7979    def set_schema(self, schema): 
    8080        """ 
     
    8383        try: 
    8484            self.schema = schema 
    85             self.schemadoc = etree.parse(self.schema, parser = PARSER) 
     85            self.schemadoc = etree.parse(self.schema, parser=PARSER) 
    8686        except etree.XMLSyntaxError as xml_error: 
    8787            logging.info(xml_error) 
     
    8989            self.schema = None 
    9090            self.schemadoc = None 
    91      
     91 
    9292    def validate_xml(self): 
    9393        """ 
     
    100100            valid = schema_check.validate(self.xmldoc) 
    101101        return valid 
    102      
     102 
    103103    def find_invalid_xml(self): 
    104104        """ 
     
    113113            first_error = str(err) 
    114114        return first_error 
    115      
     115 
    116116    def parse_schema_and_doc(self): 
    117117        """ 
     
    120120        self.set_xml_file(self.xml) 
    121121        self.set_schema(self.schema) 
    122          
     122 
    123123    def to_string(self, elem, pretty_print=False, encoding=None): 
    124124        """ 
    125125        Converts an etree element into a string 
    126126        """ 
    127         return etree.tostring(elem, pretty_print = pretty_print, \ 
    128                               encoding = encoding) 
    129      
     127        return etree.tostring(elem, pretty_print=pretty_print, \ 
     128                              encoding=encoding) 
     129 
    130130    def break_processing_instructions(self, string, dic): 
    131131        """ 
    132132        Method to break a processing instruction string apart and add to a dict 
    133          
     133 
    134134        :param string: A processing instruction as a string 
    135135        :param dic: The dictionary to save the PIs to 
     
    142142        dic[new_pi_name] = attr 
    143143        return dic 
    144      
     144 
    145145    def set_processing_instructions(self): 
    146146        """ 
     
    164164            del dic['xml'] 
    165165        self.processing_instructions = dic 
    166          
     166 
    167167    def set_encoding(self, attr_str): 
    168168        """ 
    169169        Find the encoding in the xml declaration and save it as a string 
    170          
     170 
    171171        :param attr_str: All attributes as a string 
    172172            e.g. "foo1="bar1" foo2="bar2" foo3="bar3" ... foo_n="bar_n"" 
    173173        """ 
    174174        attr_str = attr_str.replace(" = ", "=") 
    175         attr_list = attr_str.split( ) 
     175        attr_list = attr_str.split() 
    176176        for item in attr_list: 
    177177            name_value = item.split("\"=") 
     
    182182                return 
    183183        self.encoding = None 
    184          
    185     def _create_unique_key(self, dictionary, name, numb = 0): 
     184 
     185    def _create_unique_key(self, dictionary, name, numb=0): 
    186186        """ 
    187187        Create a unique key value for any dictionary to prevent overwriting 
     
    198198            name = self._create_unique_key(dictionary, name, numb) 
    199199        return name 
    200      
     200 
    201201    def create_tree(self, root): 
    202202        """ 
    203203        Create an element tree for processing from an etree element 
    204          
     204 
    205205        :param root: etree Element(s)  
    206206        """ 
    207207        return etree.ElementTree(root) 
    208      
     208 
    209209    def create_element_from_string(self, xml_string): 
    210210        """ 
    211211        Create an element from an XML string 
    212          
     212 
    213213        :param xml_string: A string of xml 
    214214        """ 
    215215        return etree.fromstring(xml_string) 
    216      
     216 
    217217    def create_element(self, name, attrib=None, nsmap=None): 
    218218        """ 
    219219        Create an XML element for writing to file 
    220          
     220 
    221221        :param name: The name of the element to be created 
    222222        """ 
     
    224224            attrib = {} 
    225225        return etree.Element(name, attrib, nsmap) 
    226      
     226 
    227227    def write_text(self, elem, text): 
    228228        """ 
    229229        Write text to an etree Element 
    230          
     230 
    231231        :param elem: etree.Element object 
    232232        :param text: text to write to the element 
     
    234234        elem.text = text 
    235235        return elem 
    236      
     236 
    237237    def write_attribute(self, elem, attr_name, attr_value): 
    238238        """ 
    239239        Write attributes to an Element 
    240          
     240 
    241241        :param elem: etree.Element object 
    242242        :param attr_name: attribute name to write 
     
    245245        attr = elem.attrib 
    246246        attr[attr_name] = attr_value 
    247          
     247 
    248248    def return_processing_instructions(self): 
    249249        """ 
    250250        Get all processing instructions saved when loading the document 
    251          
     251 
    252252        :param tree: etree.ElementTree object to write PIs to 
    253253        """ 
     
    259259                pi_list.append(pi_item) 
    260260        return pi_list 
    261      
     261 
    262262    def append(self, element, tree): 
    263263        """ 
    264264        Append an etree Element to an ElementTree. 
    265          
     265 
    266266        :param element: etree Element to append 
    267267        :param tree: ElementTree object to append to 
     
    269269        tree = tree.append(element) 
    270270        return tree 
    271      
     271 
    272272    def ebuilder(self, parent, elementname, text=None, attrib=None): 
    273273        """ 
    274274        Use lxml E builder class with arbitrary inputs. 
    275          
     275 
    276276        :param parnet: The parent element to append a child to 
    277277        :param elementname: The name of the child in string form 
     
    285285        parent = parent.append(elem) 
    286286        return parent 
    287          
Note: See TracChangeset for help on using the changeset viewer.