source: sasview/sansdataloader/src/sans/dataloader/readers/associations.py @ 618e438

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 618e438 was 618e438, checked in by Mathieu Doucet <doucetm@…>, 12 years ago

Re #5 Fix setup.py

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