source: sasview/DataLoader/readers/associations.py @ 0cfeff4

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 0cfeff4 was 379e15b, checked in by Mathieu Doucet <doucetm@…>, 15 years ago

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

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