source: sasview/DataLoader/readers/associations.py @ 97dea6c

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 97dea6c was 0997158f, checked in by Gervaise Alina <gervyh@…>, 15 years ago

working on documentation

  • Property mode set to 100644
File size: 4.2 KB
Line 
1
2
3############################################################################
4#This software was developed by the University of Tennessee as part of the
5#Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
6#project funded by the US National Science Foundation.
7#If you use DANSE applications to do scientific research that leads to
8#publication, we ask that you acknowledge the use of the software with the
9#following sentence:
10#This work benefited from DANSE software developed under NSF award DMR-0520547.
11#copyright 2009, University of Tennessee
12#############################################################################
13
14
15"""
16Module to associate default readers to file extensions.
17The module reads an xml file to get the readers for each file extension.
18The readers are tried in order they appear when reading a file.
19"""
20
21import os, sys
22import logging
23from lxml import etree
24# Py2exe compatibility: import _elementpath to ensure that py2exe finds it
25from lxml import _elementpath 
26
27## Format version for the XML settings file
28VERSION = 'sansloader/1.0'
29
30def read_associations(loader, settings='defaults.xml'):
31    """
32    Read the specified settings file to associate
33    default readers to file extension.
34   
35    :param loader: Loader object
36    :param settings: path to the XML settings file [string]
37    """
38    reader_dir = os.path.dirname(__file__)
39    path = os.path.join(reader_dir, settings)
40   
41    # If we can't find the file in the installation
42    # directory, look into the execution directory.
43    if not os.path.isfile(path):
44        path = os.path.join(os.getcwd(), settings)
45   
46    if os.path.isfile(path):
47        tree = etree.parse(path, parser=etree.ETCompatXMLParser())
48       
49        # Check the format version number
50        # Specifying the namespace will take care of the file format version
51        root = tree.getroot()
52       
53        # Read in the file extension associations
54        entry_list = root.xpath('/ns:SansLoader/ns:FileType', namespaces={'ns': VERSION})
55
56        # For each FileType entry, get the associated reader and extension
57        for entry in entry_list:
58            reader = entry.get('reader')
59            ext    = entry.get('extension')
60           
61            if reader is not None and ext is not None:
62                # Associate the extension with a particular reader
63                # TODO: Modify the Register code to be case-insensitive and remove the
64                #       extra line below.
65                try:
66                    exec "import %s" % reader
67                    exec "loader.associate_file_type('%s', %s)" % (ext.lower(), reader)
68                    exec "loader.associate_file_type('%s', %s)" % (ext.upper(), reader)
69                except:
70                    logging.error("read_associations: skipping association for %s\n  %s" % (attr['extension'], sys.exc_value))
71         
72         
73def register_readers(registry_function):
74    """
75    Function called by the registry/loader object to register
76    all default readers using a call back function.
77   
78    :WARNING: this method is now obsolete
79
80    :param registry_function: function to be called to register each reader
81    """
82    logging.info("register_readers is now obsolete: use read_associations()")
83    import abs_reader
84    import cansas_reader
85    import ascii_reader
86    import cansas_reader
87    import danse_reader
88    import hfir1d_reader
89    import IgorReader
90    import red2d_reader
91    import tiff_reader
92
93    registry_function(abs_reader)
94    registry_function(cansas_reader)
95    registry_function(ascii_reader)
96    registry_function(cansas_reader)
97    registry_function(danse_reader)
98    registry_function(hfir1d_reader)
99    registry_function(IgorReader)
100    registry_function(red2d_reader)
101    registry_function(tiff_reader)
102   
103    return True           
104
105
106if __name__ == "__main__": 
107    logging.basicConfig(level=logging.INFO,
108                        format='%(asctime)s %(levelname)s %(message)s',
109                        filename='logger.log',
110                        filemode='w')
111    from DataLoader.loader import Loader
112    l = Loader()
113    read_associations(l)
114   
115   
116    print l.get_wildcards()
117   
118   
Note: See TracBrowser for help on using the repository browser.