Changeset 379e15b in sasview for DataLoader/readers
- Timestamp:
- Aug 15, 2009 12:10:19 PM (15 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:
- b0d0723
- Parents:
- 4bb91e6
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
DataLoader/readers/associations.py
rbffc2ad r379e15b 16 16 17 17 import os, sys 18 from xml import xpath19 import xml.dom.minidom20 18 import logging 21 22 from xml.dom.minidom import parse 19 from lxml import etree 23 20 24 21 ## Format version for the XML settings file 25 VERSION = '1.0' 26 27 def get_node_text(node): 28 """ 29 Get the text context of a node 30 31 @param node: node to read from 32 @return: content, attribute list 33 """ 34 content = None 35 attr = {} 36 for item in node.childNodes: 37 if item.nodeName.find('text')>=0 \ 38 and len(item.nodeValue.strip())>0: 39 content = item.nodeValue.strip() 40 break 41 42 if node.hasAttributes(): 43 for i in range(node.attributes.length): 44 attr[node.attributes.item(i).nodeName] \ 45 = node.attributes.item(i).nodeValue 46 47 return content, attr 22 VERSION = 'sansloader/1.0' 48 23 49 24 def read_associations(loader, settings='defaults.xml'): … … 64 39 65 40 if os.path.isfile(path): 66 dom = parse(path)41 tree = etree.parse(path, parser=etree.ETCompatXMLParser()) 67 42 68 43 # Check the format version number 69 nodes = xpath.Evaluate('SansLoader', dom) 70 if nodes[0].hasAttributes(): 71 for i in range(nodes[0].attributes.length): 72 if nodes[0].attributes.item(i).nodeName=='version': 73 if nodes[0].attributes.item(i).nodeValue != VERSION: 74 raise ValueError, "associations: unrecognized SansLoader version number %s" % \ 75 nodes[0].attributes.item(i).nodeValue 44 # Specifying the namespace will take care of the file format version 45 root = tree.getroot() 76 46 77 47 # Read in the file extension associations 78 entry_list = xpath.Evaluate('SansLoader/FileType', dom) 48 entry_list = root.xpath('/ns:SansLoader/ns:FileType', namespaces={'ns': VERSION}) 49 50 # For each FileType entry, get the associated reader and extension 79 51 for entry in entry_list: 80 value, attr = get_node_text(entry)81 if attr is not None \82 and attr.has_key('reader') and attr.has_key('extension'):83 52 reader = entry.get('reader') 53 ext = entry.get('extension') 54 55 if reader is not None and ext is not None: 84 56 # Associate the extension with a particular reader 85 57 # TODO: Modify the Register code to be case-insensitive and remove the 86 58 # extra line below. 87 59 try: 88 exec "import %s" % attr['reader']89 exec "loader.associate_file_type('%s', %s)" % ( attr['extension'].lower(), attr['reader'])90 exec "loader.associate_file_type('%s', %s)" % ( attr['extension'].upper(), attr['reader'])60 exec "import %s" % reader 61 exec "loader.associate_file_type('%s', %s)" % (ext.lower(), reader) 62 exec "loader.associate_file_type('%s', %s)" % (ext.upper(), reader) 91 63 except: 92 64 logging.error("read_associations: skipping association for %s\n %s" % (attr['extension'], sys.exc_value)) … … 102 74 @param registry_function: function to be called to register each reader 103 75 """ 76 logging.info("register_readers is now obsolete: use read_associations()") 104 77 import abs_reader 105 78 import cansas_reader
Note: See TracChangeset
for help on using the changeset viewer.