source: sasview/src/sans/dataloader/readers/xml_reader.py @ 76cd1ae

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 76cd1ae was 76cd1ae, checked in by Jeff Krzywon <jeffery.krzywon@…>, 10 years ago

Fix for the datainfo window not loading and bug fixes for the cansas data reader.

Fixes/changes:
(1) datainfo window is now loading for every data file I can test
(2) transmission spectrum information (but not data) is listed in datainfo window
(3) more than one transmission spectra can be loaded for each Data1D object
(4) fixed a bug in the cansas reader that allowed any file to be loaded as data if and only if another data file was already loaded
(5) fixed the cansas writer to include transmission spectrum data and output data in strict canSAS format
(6) increased the pylint score of cansas_constants.py to above 7
(7) increased the pylint score for all files I have modified

  • Property mode set to 100644
File size: 3.1 KB
Line 
1"""
2    Generic XML reader
3"""
4############################################################################
5#This software was developed by the University of Tennessee as part of the
6#Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
7#project funded by the US National Science Foundation.
8#If you use DANSE applications to do scientific research that leads to
9#publication, we ask that you acknowledge the use of the software with the
10#following sentence:
11#This work benefited from DANSE software developed under NSF award DMR-0520547.
12#copyright 2008,2009 University of Tennessee
13#############################################################################
14
15from lxml import etree
16parser = etree.ETCompatXMLParser()
17
18class XMLreader():
19   
20    xml = None
21    xmldoc = None
22    xmlroot = None
23    schema = None
24    schemadoc = None
25   
26    def __init__(self, xml = None, schema = None, root = None):
27        self.xml = xml
28        self.schema = schema
29        if xml is not None:
30            self.setXMLFile(xml, root)
31        else:
32            self.xmldoc = None
33            self.xmlroot = None
34        if schema is not None:
35            self.setSchema(schema)
36        else:
37            self.schemadoc = None
38   
39    def reader(self):
40        """
41        Read in an XML file into memory and return an lxml dictionary
42        """
43        if self.validateXML():
44            self.xmldoc = etree.parse(self.xml, parser = parser)
45        else:
46            raise etree.ValidationError(self, self.findInvalidXML())
47        return self.xmldoc
48   
49    def setXMLFile(self, xml, root = None):
50        try:
51            self.xml = xml
52            self.xmldoc = etree.parse(self.xml, parser = parser)
53            self.xmlroot = self.xmldoc.getroot()
54        except Exception:
55            self.xml = None
56            self.xmldoc = None
57            self.xmlroot = None
58   
59    def setSchema(self, schema):
60        try:
61            self.schema = schema
62            self.schemadoc = etree.parse(self.schema, parser = parser)
63        except Exception:
64            self.schema = None
65            self.schemadoc = None
66   
67    def validateXML(self):
68        """
69        Checks to see if the XML file meets the schema
70        """
71        valid = True
72        if self.schema is not None:
73            self.parseSchemaAndDoc()
74            schemaCheck = etree.XMLSchema(self.schemadoc)
75            valid = schemaCheck.validate(self.xmldoc)
76        return valid
77   
78    def findInvalidXML(self):
79        """
80        Finds the first offending element that should not be present in an XML file
81        """
82        firstError = ""
83        self.parseSchemaAndDoc()
84        schema = etree.XMLSchema(self.schemadoc)
85        try:
86            firstError = schema.assertValid(self.xmldoc)
87        except etree.DocumentInvalid as e:
88            firstError = str(e)
89        return firstError
90   
91    def parseSchemaAndDoc(self):
92        """
93        Creates a dictionary of the parsed schema and xml files.
94        """
95        self.setXMLFile(self.xml)
96        self.setSchema(self.schema)
97       
Note: See TracBrowser for help on using the repository browser.