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

Last change on this file since a99a4045 was e5c09cf, checked in by wojciech, 8 years ago

Added readers to registry function, so they are bundeled to MacBuild

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