source: sasview/src/sas/sascalc/dataloader/readers/associations.py @ 19296dc

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.2.2ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 19296dc was a1b8fee, checked in by andyfaff, 8 years ago

MAINT: from future import print_function

  • Property mode set to 100644
File size: 4.1 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#############################################################################
16from __future__ import print_function
17
18import os
19import sys
20import logging
21import json
22
23logger = logging.getLogger(__name__)
24
25FILE_NAME = 'defaults.json'
26
27def read_associations(loader, settings=FILE_NAME):
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 json 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        with open(path) as fh:
50            json_tree = json.load(fh)
51       
52        # Read in the file extension associations
53        entry_list = json_tree['SasLoader']['FileType']
54
55        # For each FileType entry, get the associated reader and extension
56        for entry in entry_list:
57            reader = entry['-reader']
58            ext = entry['-extension']
59           
60            if reader is not None and ext is not None:
61                # Associate the extension with a particular reader
62                # TODO: Modify the Register code to be case-insensitive
63                # and remove the extra line below.
64                try:
65                    exec "import %s" % reader
66                    exec "loader.associate_file_type('%s', %s)" % (ext.lower(),
67                                                                    reader)
68                    exec "loader.associate_file_type('%s', %s)" % (ext.upper(),
69                                                                    reader)
70                except:
71                    msg = "read_associations: skipping association"
72                    msg += " for %s\n  %s" % (ext.lower(), sys.exc_value)
73                    logger.error(msg)
74    else:
75        print("Could not find reader association settings\n  %s [%s]" % (__file__, os.getcwd()))
76         
77         
78def register_readers(registry_function):
79    """
80    Function called by the registry/loader object to register
81    all default readers using a call back function.
82   
83    :WARNING: this method is now obsolete
84
85    :param registry_function: function to be called to register each reader
86    """
87    logger.info("register_readers is now obsolete: use read_associations()")
88    import abs_reader
89    import ascii_reader
90    import cansas_reader
91    import danse_reader
92    import hfir1d_reader
93    import IgorReader
94    import red2d_reader
95    #import tiff_reader
96    import nexus_reader
97    import sesans_reader
98    import cansas_reader_HDF5
99    import anton_paar_saxs_reader
100    registry_function(sesans_reader)
101    registry_function(abs_reader)
102    registry_function(ascii_reader)
103    registry_function(cansas_reader)
104    registry_function(danse_reader)
105    registry_function(hfir1d_reader)
106    registry_function(IgorReader)
107    registry_function(red2d_reader)
108    #registry_function(tiff_reader)
109    registry_function(nexus_reader)
110    registry_function(cansas_reader_HDF5)
111    registry_function(anton_paar_saxs_reader)
112    return True
Note: See TracBrowser for help on using the repository browser.