[7d6351e] | 1 | """ |
---|
| 2 | Module to associate default readers to file extensions. |
---|
| 3 | The module reads an xml file to get the readers for each file extension. |
---|
| 4 | The 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] | 16 | import os |
---|
| 17 | import sys |
---|
[28caa03] | 18 | import logging |
---|
[5dfdfa7] | 19 | import json |
---|
[28caa03] | 20 | |
---|
[463e7ffc] | 21 | logger = logging.getLogger(__name__) |
---|
[c155a16] | 22 | |
---|
[5dfdfa7] | 23 | FILE_NAME = 'defaults.json' |
---|
[28caa03] | 24 | |
---|
[5dfdfa7] | 25 | def read_associations(loader, settings=FILE_NAME): |
---|
[28caa03] | 26 | """ |
---|
[0997158f] | 27 | Read the specified settings file to associate |
---|
| 28 | default readers to file extension. |
---|
| 29 | |
---|
| 30 | :param loader: Loader object |
---|
[5dfdfa7] | 31 | :param settings: path to the json settings file [string] |
---|
[28caa03] | 32 | """ |
---|
| 33 | reader_dir = os.path.dirname(__file__) |
---|
[bffc2ad] | 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) |
---|
[ed61f2a1] | 40 | if not os.path.isfile(path): |
---|
[c09ace41] | 41 | path = os.path.join(sys.path[0], settings) |
---|
[618e438] | 42 | if not os.path.isfile(path): |
---|
| 43 | path = settings |
---|
[f576de0] | 44 | if not os.path.isfile(path): |
---|
| 45 | path = "./%s" % settings |
---|
[28caa03] | 46 | if os.path.isfile(path): |
---|
[5dfdfa7] | 47 | with open(path) as fh: |
---|
| 48 | json_tree = json.load(fh) |
---|
[28caa03] | 49 | |
---|
| 50 | # Read in the file extension associations |
---|
[5dfdfa7] | 51 | entry_list = json_tree['SasLoader']['FileType'] |
---|
[379e15b] | 52 | |
---|
| 53 | # For each FileType entry, get the associated reader and extension |
---|
[28caa03] | 54 | for entry in entry_list: |
---|
[5dfdfa7] | 55 | reader = entry['-reader'] |
---|
| 56 | ext = entry['-extension'] |
---|
[379e15b] | 57 | |
---|
| 58 | if reader is not None and ext is not None: |
---|
[28caa03] | 59 | # Associate the extension with a particular reader |
---|
[7d6351e] | 60 | # TODO: Modify the Register code to be case-insensitive |
---|
[a7a5886] | 61 | # and remove the extra line below. |
---|
[28caa03] | 62 | try: |
---|
[379e15b] | 63 | exec "import %s" % reader |
---|
[a7a5886] | 64 | exec "loader.associate_file_type('%s', %s)" % (ext.lower(), |
---|
| 65 | reader) |
---|
| 66 | exec "loader.associate_file_type('%s', %s)" % (ext.upper(), |
---|
| 67 | reader) |
---|
[28caa03] | 68 | except: |
---|
[a7a5886] | 69 | msg = "read_associations: skipping association" |
---|
[f4e507b] | 70 | msg += " for %s\n %s" % (ext.lower(), sys.exc_value) |
---|
[c155a16] | 71 | logger.error(msg) |
---|
[86e2f5a] | 72 | else: |
---|
[496c5bb] | 73 | print "Could not find reader association settings\n %s [%s]" % (__file__, os.getcwd()) |
---|
[28caa03] | 74 | |
---|
| 75 | |
---|
| 76 | def register_readers(registry_function): |
---|
| 77 | """ |
---|
[0997158f] | 78 | Function called by the registry/loader object to register |
---|
| 79 | all default readers using a call back function. |
---|
[28caa03] | 80 | |
---|
[0997158f] | 81 | :WARNING: this method is now obsolete |
---|
| 82 | |
---|
| 83 | :param registry_function: function to be called to register each reader |
---|
[28caa03] | 84 | """ |
---|
[c155a16] | 85 | logger.info("register_readers is now obsolete: use read_associations()") |
---|
[28caa03] | 86 | import abs_reader |
---|
| 87 | import ascii_reader |
---|
| 88 | import cansas_reader |
---|
| 89 | import danse_reader |
---|
| 90 | import hfir1d_reader |
---|
| 91 | import IgorReader |
---|
[ded62ce] | 92 | import red2d_reader |
---|
[3241dd2] | 93 | #import tiff_reader |
---|
[a6d1676] | 94 | import nexus_reader |
---|
[5e326a6] | 95 | import sesans_reader |
---|
[e5c09cf] | 96 | import cansas_reader_HDF5 |
---|
| 97 | import anton_paar_saxs_reader |
---|
[5e326a6] | 98 | registry_function(sesans_reader) |
---|
[28caa03] | 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) |
---|
[ded62ce] | 105 | registry_function(red2d_reader) |
---|
[3241dd2] | 106 | #registry_function(tiff_reader) |
---|
[d225e32] | 107 | registry_function(nexus_reader) |
---|
[e5c09cf] | 108 | registry_function(cansas_reader_HDF5) |
---|
| 109 | registry_function(anton_paar_saxs_reader) |
---|
[7d6351e] | 110 | return True |
---|