source: sasview/sansdataloader/src/sans/dataloader/readers/associations.py @ 33add9e

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 33add9e was 7d6351e, checked in by Mathieu Doucet <doucetm@…>, 13 years ago

Pep-8-ification

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