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

Last change on this file since 62acdd5 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
RevLine 
[7d6351e]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"""
[0997158f]6############################################################################
7#This software was developed by the University of Tennessee as part of the
8#Distributed Data Analysis of Neutron Scattering Experiments (DANSE)
[7d6351e]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
[0997158f]12#following sentence:
[7d6351e]13#This work benefited from DANSE software developed under NSF award DMR-0520547.
[0997158f]14#copyright 2009, University of Tennessee
15#############################################################################
[a7a5886]16import os
17import sys
[28caa03]18import logging
[5dfdfa7]19import json
[28caa03]20
[5dfdfa7]21FILE_NAME = 'defaults.json'
[28caa03]22
[5dfdfa7]23def read_associations(loader, settings=FILE_NAME):
[28caa03]24    """
[0997158f]25    Read the specified settings file to associate
26    default readers to file extension.
27   
28    :param loader: Loader object
[5dfdfa7]29    :param settings: path to the json settings file [string]
[28caa03]30    """
31    reader_dir = os.path.dirname(__file__)
[bffc2ad]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)
[ed61f2a1]38    if not os.path.isfile(path):
[c09ace41]39        path = os.path.join(sys.path[0], settings)
[618e438]40    if not os.path.isfile(path):
41        path = settings
[f576de0]42    if not os.path.isfile(path):
43        path = "./%s" % settings
[28caa03]44    if os.path.isfile(path):
[5dfdfa7]45        with open(path) as fh:
46            json_tree = json.load(fh)
[28caa03]47       
48        # Read in the file extension associations
[5dfdfa7]49        entry_list = json_tree['SasLoader']['FileType']
[379e15b]50
51        # For each FileType entry, get the associated reader and extension
[28caa03]52        for entry in entry_list:
[5dfdfa7]53            reader = entry['-reader']
54            ext = entry['-extension']
[379e15b]55           
56            if reader is not None and ext is not None:
[28caa03]57                # Associate the extension with a particular reader
[7d6351e]58                # TODO: Modify the Register code to be case-insensitive
[a7a5886]59                # and remove the extra line below.
[28caa03]60                try:
[379e15b]61                    exec "import %s" % reader
[a7a5886]62                    exec "loader.associate_file_type('%s', %s)" % (ext.lower(),
63                                                                    reader)
64                    exec "loader.associate_file_type('%s', %s)" % (ext.upper(),
65                                                                    reader)
[28caa03]66                except:
[a7a5886]67                    msg = "read_associations: skipping association"
[f4e507b]68                    msg += " for %s\n  %s" % (ext.lower(), sys.exc_value)
[a7a5886]69                    logging.error(msg)
[86e2f5a]70    else:
[496c5bb]71        print "Could not find reader association settings\n  %s [%s]" % (__file__, os.getcwd())
[28caa03]72         
73         
74def register_readers(registry_function):
75    """
[0997158f]76    Function called by the registry/loader object to register
77    all default readers using a call back function.
[28caa03]78   
[0997158f]79    :WARNING: this method is now obsolete
80
81    :param registry_function: function to be called to register each reader
[28caa03]82    """
[379e15b]83    logging.info("register_readers is now obsolete: use read_associations()")
[28caa03]84    import abs_reader
85    import ascii_reader
86    import cansas_reader
87    import danse_reader
88    import hfir1d_reader
89    import IgorReader
[ded62ce]90    import red2d_reader
[3241dd2]91    #import tiff_reader
[a6d1676]92    import nexus_reader
[5e326a6]93    import sesans_reader
[e5c09cf]94    import cansas_reader_HDF5
95    import anton_paar_saxs_reader
[5e326a6]96    registry_function(sesans_reader)
[28caa03]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)
[ded62ce]103    registry_function(red2d_reader)
[3241dd2]104    #registry_function(tiff_reader)
[d225e32]105    registry_function(nexus_reader)
[e5c09cf]106    registry_function(cansas_reader_HDF5)
107    registry_function(anton_paar_saxs_reader)
[7d6351e]108    return True
Note: See TracBrowser for help on using the repository browser.