Changeset 5ce7f17 in sasview for src/sas/dataloader/readers
- Timestamp:
- Mar 27, 2015 11:05:11 AM (10 years ago)
- 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
- Location:
- src/sas/dataloader/readers
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/dataloader/readers/cansas_reader.py
r682c432 r5ce7f17 322 322 value_unit = local_unit 323 323 except: 324 print sys.exc_info()325 324 err_msg = "CanSAS reader: unknown error converting " 326 325 err_msg += "\"{0}\" unit [{1}]" … … 990 989 self.append(node, entry_node) 991 990 992 def _check_origin(self, entry_node, doc ):991 def _check_origin(self, entry_node, doc, frm): 993 992 """ 994 993 Return the document, and the SASentry node associated with … … 1000 999 :param doc: entire xml tree 1001 1000 """ 1002 frm = inspect.stack()[1] 1001 if not frm: 1002 frm = inspect.stack()[1] 1003 1003 mod_name = frm[1].replace("\\", "/").replace(".pyc", "") 1004 1004 mod_name = mod_name.replace(".py", "") … … 1011 1011 node_list = doc.getElementsByTagName(node_name) 1012 1012 entry_node = node_list.item(0) 1013 return entry_node1013 return doc, entry_node 1014 1014 1015 1015 def _to_xml_doc(self, datainfo): … … 1061 1061 # If the calling function was not the cansas reader, return a minidom 1062 1062 # 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) 1064 1065 return doc, entry_node 1065 1066 -
src/sas/dataloader/readers/xml_reader.py
r79492222 r5ce7f17 1 1 """ 2 2 Generic XML read and write utility 3 3 4 4 Usage: Either extend xml_reader or add as a class variable. 5 5 """ … … 7 7 #This software was developed by the University of Tennessee as part of the 8 8 #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 12 12 #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. 14 14 #copyright 2008,2009 University of Tennessee 15 15 ############################################################################# … … 25 25 Generic XML read and write class. Mostly helper functions. 26 26 Makes reading/writing XML a bit easier than calling lxml libraries directly. 27 27 28 28 :Dependencies: 29 29 This class requires lxml 2.3 or higher. 30 30 """ 31 31 32 32 xml = None 33 33 xmldoc = None … … 37 37 encoding = None 38 38 processing_instructions = None 39 40 def __init__(self, xml = None, schema =None):39 40 def __init__(self, xml=None, schema=None): 41 41 self.xml = xml 42 42 self.schema = schema … … 51 51 else: 52 52 self.schemadoc = None 53 53 54 54 def reader(self): 55 55 """ … … 57 57 """ 58 58 if self.validate_xml(): 59 self.xmldoc = etree.parse(self.xml, parser =PARSER)59 self.xmldoc = etree.parse(self.xml, parser=PARSER) 60 60 else: 61 61 raise etree.XMLSchemaValidateError(self, self.find_invalid_xml()) 62 62 return self.xmldoc 63 63 64 64 def set_xml_file(self, xml): 65 65 """ … … 68 68 try: 69 69 self.xml = xml 70 self.xmldoc = etree.parse(self.xml, parser =PARSER)70 self.xmldoc = etree.parse(self.xml, parser=PARSER) 71 71 self.xmlroot = self.xmldoc.getroot() 72 72 except etree.XMLSyntaxError as xml_error: … … 76 76 self.xmldoc = None 77 77 self.xmlroot = None 78 78 79 79 def set_schema(self, schema): 80 80 """ … … 83 83 try: 84 84 self.schema = schema 85 self.schemadoc = etree.parse(self.schema, parser =PARSER)85 self.schemadoc = etree.parse(self.schema, parser=PARSER) 86 86 except etree.XMLSyntaxError as xml_error: 87 87 logging.info(xml_error) … … 89 89 self.schema = None 90 90 self.schemadoc = None 91 91 92 92 def validate_xml(self): 93 93 """ … … 100 100 valid = schema_check.validate(self.xmldoc) 101 101 return valid 102 102 103 103 def find_invalid_xml(self): 104 104 """ … … 113 113 first_error = str(err) 114 114 return first_error 115 115 116 116 def parse_schema_and_doc(self): 117 117 """ … … 120 120 self.set_xml_file(self.xml) 121 121 self.set_schema(self.schema) 122 122 123 123 def to_string(self, elem, pretty_print=False, encoding=None): 124 124 """ 125 125 Converts an etree element into a string 126 126 """ 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 130 130 def break_processing_instructions(self, string, dic): 131 131 """ 132 132 Method to break a processing instruction string apart and add to a dict 133 133 134 134 :param string: A processing instruction as a string 135 135 :param dic: The dictionary to save the PIs to … … 142 142 dic[new_pi_name] = attr 143 143 return dic 144 144 145 145 def set_processing_instructions(self): 146 146 """ … … 164 164 del dic['xml'] 165 165 self.processing_instructions = dic 166 166 167 167 def set_encoding(self, attr_str): 168 168 """ 169 169 Find the encoding in the xml declaration and save it as a string 170 170 171 171 :param attr_str: All attributes as a string 172 172 e.g. "foo1="bar1" foo2="bar2" foo3="bar3" ... foo_n="bar_n"" 173 173 """ 174 174 attr_str = attr_str.replace(" = ", "=") 175 attr_list = attr_str.split( 175 attr_list = attr_str.split() 176 176 for item in attr_list: 177 177 name_value = item.split("\"=") … … 182 182 return 183 183 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): 186 186 """ 187 187 Create a unique key value for any dictionary to prevent overwriting … … 198 198 name = self._create_unique_key(dictionary, name, numb) 199 199 return name 200 200 201 201 def create_tree(self, root): 202 202 """ 203 203 Create an element tree for processing from an etree element 204 204 205 205 :param root: etree Element(s) 206 206 """ 207 207 return etree.ElementTree(root) 208 208 209 209 def create_element_from_string(self, xml_string): 210 210 """ 211 211 Create an element from an XML string 212 212 213 213 :param xml_string: A string of xml 214 214 """ 215 215 return etree.fromstring(xml_string) 216 216 217 217 def create_element(self, name, attrib=None, nsmap=None): 218 218 """ 219 219 Create an XML element for writing to file 220 220 221 221 :param name: The name of the element to be created 222 222 """ … … 224 224 attrib = {} 225 225 return etree.Element(name, attrib, nsmap) 226 226 227 227 def write_text(self, elem, text): 228 228 """ 229 229 Write text to an etree Element 230 230 231 231 :param elem: etree.Element object 232 232 :param text: text to write to the element … … 234 234 elem.text = text 235 235 return elem 236 236 237 237 def write_attribute(self, elem, attr_name, attr_value): 238 238 """ 239 239 Write attributes to an Element 240 240 241 241 :param elem: etree.Element object 242 242 :param attr_name: attribute name to write … … 245 245 attr = elem.attrib 246 246 attr[attr_name] = attr_value 247 247 248 248 def return_processing_instructions(self): 249 249 """ 250 250 Get all processing instructions saved when loading the document 251 251 252 252 :param tree: etree.ElementTree object to write PIs to 253 253 """ … … 259 259 pi_list.append(pi_item) 260 260 return pi_list 261 261 262 262 def append(self, element, tree): 263 263 """ 264 264 Append an etree Element to an ElementTree. 265 265 266 266 :param element: etree Element to append 267 267 :param tree: ElementTree object to append to … … 269 269 tree = tree.append(element) 270 270 return tree 271 271 272 272 def ebuilder(self, parent, elementname, text=None, attrib=None): 273 273 """ 274 274 Use lxml E builder class with arbitrary inputs. 275 275 276 276 :param parnet: The parent element to append a child to 277 277 :param elementname: The name of the child in string form … … 285 285 parent = parent.append(elem) 286 286 return parent 287
Note: See TracChangeset
for help on using the changeset viewer.