Changeset 7a5d066 in sasview for src/sas/sascalc/dataloader


Ignore:
Timestamp:
Apr 20, 2017 3:00:56 PM (8 years ago)
Author:
krzywon
Branches:
master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.2.2, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
Children:
5d8f9b3
Parents:
080d88e
git-author:
Jeff Krzywon <krzywon@…> (04/20/17 15:00:56)
git-committer:
krzywon <krzywon@…> (04/20/17 15:00:56)
Message:

Replace defaults.json file with constant to eliminate json dependency

Location:
src/sas/sascalc/dataloader/readers
Files:
1 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sascalc/dataloader/readers/__init__.py

    r959eb01 r7a5d066  
    1 # Backward compatibility with the previous implementation of the default readers 
    2 from associations import register_readers 
     1# Method to associate extensions to default readers 
     2from associations import read_associations 
    33 
    4 # Method to associate extensions to default readers  
    5 from associations import read_associations 
    64 
    75# Method to return the location of the XML settings file 
  • src/sas/sascalc/dataloader/readers/associations.py

    r959eb01 r7a5d066  
    1414#copyright 2009, University of Tennessee 
    1515############################################################################# 
    16 import os 
    1716import sys 
    1817import logging 
    19 import json 
    2018 
    2119logger = logging.getLogger(__name__) 
    2220 
    23 FILE_NAME = 'defaults.json' 
     21FILE_ASSOCIATIONS = { 
     22    ".xml": "cansas_reader", 
     23    ".ses": "sesans_reader", 
     24    ".h5": "cansas_reader_HDF5", 
     25    ".txt": "ascii_reader", 
     26    ".asc": "IgorReader", 
     27    ".dat": "red2d_reader", 
     28    ".abs": "abs_reader", 
     29    ".d1d": "hfir1d_reader", 
     30    ".sans": "danse_reader", 
     31    ".nxs": "nexus_reader", 
     32    ".pdh": "anton_paar_saxs_reader" 
     33} 
    2434 
    25 def read_associations(loader, settings=FILE_NAME): 
     35 
     36def read_associations(loader, settings=FILE_ASSOCIATIONS): 
    2637    """ 
    2738    Read the specified settings file to associate 
     
    3142    :param settings: path to the json settings file [string] 
    3243    """ 
    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           
    76 def 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 
     44    # For each FileType entry, get the associated reader and extension 
     45    for ext, reader in settings.iteritems(): 
     46        if reader is not None and ext is not None: 
     47            # Associate the extension with a particular reader 
     48            # TODO: Modify the Register code to be case-insensitive 
     49            # FIXME: Remove exec statements 
     50            # and remove the extra line below. 
     51            try: 
     52                exec "import %s" % reader 
     53                exec "loader.associate_file_type('%s', %s)" % (ext.lower(), 
     54                                                                reader) 
     55                exec "loader.associate_file_type('%s', %s)" % (ext.upper(), 
     56                                                                reader) 
     57            except: 
     58                msg = "read_associations: skipping association" 
     59                msg += " for %s\n  %s" % (ext.lower(), sys.exc_value) 
     60                logger.error(msg) 
Note: See TracChangeset for help on using the changeset viewer.