Changeset 379e15b in sasview for DataLoader/readers


Ignore:
Timestamp:
Aug 15, 2009 12:10:19 PM (15 years ago)
Author:
Mathieu Doucet <doucetm@…>
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
Message:

dataloader: ported reader association code to lxml (ditching pyxml, which is no longer supported).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • DataLoader/readers/associations.py

    rbffc2ad r379e15b  
    1616 
    1717import os, sys 
    18 from xml import xpath 
    19 import xml.dom.minidom  
    2018import logging 
    21  
    22 from xml.dom.minidom import parse 
     19from lxml import etree    
    2320 
    2421## 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 
     22VERSION = 'sansloader/1.0' 
    4823 
    4924def read_associations(loader, settings='defaults.xml'): 
     
    6439     
    6540    if os.path.isfile(path): 
    66         dom = parse(path) 
     41        tree = etree.parse(path, parser=etree.ETCompatXMLParser()) 
    6742         
    6843        # 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() 
    7646         
    7747        # 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 
    7951        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: 
    8456                # Associate the extension with a particular reader 
    8557                # TODO: Modify the Register code to be case-insensitive and remove the 
    8658                #       extra line below. 
    8759                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) 
    9163                except: 
    9264                    logging.error("read_associations: skipping association for %s\n  %s" % (attr['extension'], sys.exc_value)) 
     
    10274        @param registry_function: function to be called to register each reader 
    10375    """ 
     76    logging.info("register_readers is now obsolete: use read_associations()") 
    10477    import abs_reader 
    10578    import cansas_reader 
Note: See TracChangeset for help on using the changeset viewer.