source: sasview/DataLoader/readers/associations.py @ 9181512

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 9181512 was 28caa03, checked in by Mathieu Doucet <doucetm@…>, 16 years ago

Improved hierarchical reader structure, put back reader plugin, minor fixes.

  • Property mode set to 100644
File size: 4.4 KB
Line 
1"""
2This software was developed by the University of Tennessee as part of the
3Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
4project funded by the US National Science Foundation.
5
6See the license text in license.txt
7
8copyright 2009, University of Tennessee
9"""
10
11"""
12    Module to associate default readers to file extensions.
13    The module reads an xml file to get the readers for each file extension.
14    The readers are tried in order they appear when reading a file.
15"""
16
17import os, sys
18from xml import xpath
19import xml.dom.minidom 
20import logging
21
22from xml.dom.minidom import parse
23
24## Format version for the XML settings file
25VERSION = '1.0'
26
27def 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
48
49def read_associations(loader, path='defaults.xml'):
50    """
51        Read the specified settings file to associate
52        default readers to file extension.
53       
54        @param loader: Loader object
55        @param path: path to the XML settings file
56    """
57    reader_dir = os.path.dirname(__file__)
58    path = os.path.join(reader_dir, path)
59   
60    if os.path.isfile(path):
61        dom = parse(path)
62       
63        # Check the format version number
64        nodes = xpath.Evaluate('SansLoader', dom)
65        if nodes[0].hasAttributes():
66            for i in range(nodes[0].attributes.length):
67                if nodes[0].attributes.item(i).nodeName=='version':
68                    if nodes[0].attributes.item(i).nodeValue != VERSION:
69                        raise ValueError, "associations: unrecognized SansLoader version number %s" % \
70                            nodes[0].attributes.item(i).nodeValue
71       
72        # Read in the file extension associations
73        entry_list = xpath.Evaluate('SansLoader/FileType', dom)
74        for entry in entry_list:
75            value, attr = get_node_text(entry)
76            if attr is not None \
77                and attr.has_key('reader') and attr.has_key('extension'):
78               
79                # Associate the extension with a particular reader
80                # TODO: Modify the Register code to be case-insensitive and remove the
81                #       extra line below.
82                try:
83                    exec "import %s" % attr['reader']
84                    exec "loader.associate_file_type('%s', %s)" % (attr['extension'].lower(), attr['reader'])
85                    exec "loader.associate_file_type('%s', %s)" % (attr['extension'].upper(), attr['reader'])
86                except:
87                    logging.error("read_associations: skipping association for %s\n  %s" % (attr['extension'], sys.exc_value))
88         
89         
90def register_readers(registry_function):
91    """
92        Function called by the registry/loader object to register
93        all default readers using a call back function.
94       
95        WARNING: this method is now obsolete
96   
97        @param registry_function: function to be called to register each reader
98    """
99    import abs_reader
100    import cansas_reader
101    import ascii_reader
102    import cansas_reader
103    import danse_reader
104    import hfir1d_reader
105    import IgorReader
106    import tiff_reader
107
108    registry_function(abs_reader)
109    registry_function(cansas_reader)
110    registry_function(ascii_reader)
111    registry_function(cansas_reader)
112    registry_function(danse_reader)
113    registry_function(hfir1d_reader)
114    registry_function(IgorReader)
115    registry_function(tiff_reader)
116   
117    return True           
118
119
120if __name__ == "__main__": 
121    logging.basicConfig(level=logging.INFO,
122                        format='%(asctime)s %(levelname)s %(message)s',
123                        filename='logger.log',
124                        filemode='w')
125    from DataLoader.loader import Loader
126    l = Loader()
127    read_associations(l)
128   
129   
130    print l.get_wildcards()
131   
132   
Note: See TracBrowser for help on using the repository browser.