source: sasview/DataLoader/readers/associations.py @ 2f569b3

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 2f569b3 was fe11a3fa, checked in by Mathieu Doucet <doucetm@…>, 15 years ago

dataloader: fix py2exe compatibility issue

  • Property mode set to 100644
File size: 3.9 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
18import logging
19from lxml import etree
20# Py2exe compatibility: import _elementpath to ensure that py2exe finds it
21from lxml import _elementpath 
22
23## Format version for the XML settings file
24VERSION = 'sansloader/1.0'
25
26def read_associations(loader, settings='defaults.xml'):
27    """
28        Read the specified settings file to associate
29        default readers to file extension.
30       
31        @param loader: Loader object
32        @param settings: path to the XML settings file [string]
33    """
34    reader_dir = os.path.dirname(__file__)
35    path = os.path.join(reader_dir, settings)
36   
37    # If we can't find the file in the installation
38    # directory, look into the execution directory.
39    if not os.path.isfile(path):
40        path = os.path.join(os.getcwd(), settings)
41   
42    if os.path.isfile(path):
43        tree = etree.parse(path, parser=etree.ETCompatXMLParser())
44       
45        # Check the format version number
46        # Specifying the namespace will take care of the file format version
47        root = tree.getroot()
48       
49        # Read in the file extension associations
50        entry_list = root.xpath('/ns:SansLoader/ns:FileType', namespaces={'ns': VERSION})
51
52        # For each FileType entry, get the associated reader and extension
53        for entry in entry_list:
54            reader = entry.get('reader')
55            ext    = entry.get('extension')
56           
57            if reader is not None and ext is not None:
58                # Associate the extension with a particular reader
59                # TODO: Modify the Register code to be case-insensitive and remove the
60                #       extra line below.
61                try:
62                    exec "import %s" % reader
63                    exec "loader.associate_file_type('%s', %s)" % (ext.lower(), reader)
64                    exec "loader.associate_file_type('%s', %s)" % (ext.upper(), reader)
65                except:
66                    logging.error("read_associations: skipping association for %s\n  %s" % (attr['extension'], sys.exc_value))
67         
68         
69def register_readers(registry_function):
70    """
71        Function called by the registry/loader object to register
72        all default readers using a call back function.
73       
74        WARNING: this method is now obsolete
75   
76        @param registry_function: function to be called to register each reader
77    """
78    logging.info("register_readers is now obsolete: use read_associations()")
79    import abs_reader
80    import cansas_reader
81    import ascii_reader
82    import cansas_reader
83    import danse_reader
84    import hfir1d_reader
85    import IgorReader
86    import tiff_reader
87
88    registry_function(abs_reader)
89    registry_function(cansas_reader)
90    registry_function(ascii_reader)
91    registry_function(cansas_reader)
92    registry_function(danse_reader)
93    registry_function(hfir1d_reader)
94    registry_function(IgorReader)
95    registry_function(tiff_reader)
96   
97    return True           
98
99
100if __name__ == "__main__": 
101    logging.basicConfig(level=logging.INFO,
102                        format='%(asctime)s %(levelname)s %(message)s',
103                        filename='logger.log',
104                        filemode='w')
105    from DataLoader.loader import Loader
106    l = Loader()
107    read_associations(l)
108   
109   
110    print l.get_wildcards()
111   
112   
Note: See TracBrowser for help on using the repository browser.