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 | """ |
---|
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 | ############################################################################# |
---|
16 | import sys |
---|
17 | import logging |
---|
18 | |
---|
19 | logger = logging.getLogger(__name__) |
---|
20 | |
---|
21 | FILE_ASSOCIATIONS = { |
---|
22 | ".xml": "cansas_reader", |
---|
23 | ".ses": "sesans_reader", |
---|
24 | ".h5": "cansas_reader_HDF5", |
---|
25 | ".nxs": "cansas_reader_HDF5", |
---|
26 | ".txt": "ascii_reader", |
---|
27 | ".dat": "red2d_reader", |
---|
28 | ".abs": "abs_reader", |
---|
29 | ".cor": "abs_reader", |
---|
30 | ".sans": "danse_reader", |
---|
31 | ".pdh": "anton_paar_saxs_reader" |
---|
32 | } |
---|
33 | |
---|
34 | |
---|
35 | def read_associations(loader, settings=FILE_ASSOCIATIONS): |
---|
36 | """ |
---|
37 | Read the specified settings file to associate |
---|
38 | default readers to file extension. |
---|
39 | |
---|
40 | :param loader: Loader object |
---|
41 | :param settings: path to the json settings file [string] |
---|
42 | """ |
---|
43 | # For each FileType entry, get the associated reader and extension |
---|
44 | for ext, reader in settings.items(): |
---|
45 | if reader is not None and ext is not None: |
---|
46 | # Associate the extension with a particular reader |
---|
47 | # TODO: Modify the Register code to be case-insensitive |
---|
48 | # FIXME: Remove exec statements |
---|
49 | # and remove the extra line below. |
---|
50 | try: |
---|
51 | exec("from . import %s" % reader) |
---|
52 | exec("loader.associate_file_type('%s', %s)" |
---|
53 | % (ext.lower(), reader)) |
---|
54 | exec("loader.associate_file_type('%s', %s)" |
---|
55 | % (ext.upper(), reader)) |
---|
56 | except Exception as exc: |
---|
57 | msg = "read_associations: skipping association" |
---|
58 | msg += " for %s\n %s" % (ext.lower(), exc) |
---|
59 | logger.error(msg) |
---|