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

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 e5cbbce was 959eb01, checked in by ajj, 8 years ago

normalising line endings

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