source: sasview/DataLoader/readers/associations.py @ 7b78bb6

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

dataloader: added function to get location of settings file, added functionality to default to CWD when looking for that settings file

  • Property mode set to 100644
File size: 4.6 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, settings='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 settings: path to the XML settings file [string]
56    """
57    reader_dir = os.path.dirname(__file__)
58    path = os.path.join(reader_dir, settings)
59   
60    # If we can't find the file in the installation
61    # directory, look into the execution directory.
62    if not os.path.isfile(path):
63        path = os.path.join(os.getcwd(), settings)
64   
65    if os.path.isfile(path):
66        dom = parse(path)
67       
68        # 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
76       
77        # Read in the file extension associations
78        entry_list = xpath.Evaluate('SansLoader/FileType', dom)
79        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               
84                # Associate the extension with a particular reader
85                # TODO: Modify the Register code to be case-insensitive and remove the
86                #       extra line below.
87                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'])
91                except:
92                    logging.error("read_associations: skipping association for %s\n  %s" % (attr['extension'], sys.exc_value))
93         
94         
95def register_readers(registry_function):
96    """
97        Function called by the registry/loader object to register
98        all default readers using a call back function.
99       
100        WARNING: this method is now obsolete
101   
102        @param registry_function: function to be called to register each reader
103    """
104    import abs_reader
105    import cansas_reader
106    import ascii_reader
107    import cansas_reader
108    import danse_reader
109    import hfir1d_reader
110    import IgorReader
111    import tiff_reader
112
113    registry_function(abs_reader)
114    registry_function(cansas_reader)
115    registry_function(ascii_reader)
116    registry_function(cansas_reader)
117    registry_function(danse_reader)
118    registry_function(hfir1d_reader)
119    registry_function(IgorReader)
120    registry_function(tiff_reader)
121   
122    return True           
123
124
125if __name__ == "__main__": 
126    logging.basicConfig(level=logging.INFO,
127                        format='%(asctime)s %(levelname)s %(message)s',
128                        filename='logger.log',
129                        filemode='w')
130    from DataLoader.loader import Loader
131    l = Loader()
132    read_associations(l)
133   
134   
135    print l.get_wildcards()
136   
137   
Note: See TracBrowser for help on using the repository browser.