[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 |
---|
[7d6351e] | 19 | from lxml import etree |
---|
[fe11a3fa] | 20 | # Py2exe compatibility: import _elementpath to ensure that py2exe finds it |
---|
[7d6351e] | 21 | from lxml import _elementpath |
---|
[28caa03] | 22 | |
---|
| 23 | ## Format version for the XML settings file |
---|
[fd5ac0d] | 24 | VERSION = 'sasloader/1.0' |
---|
[28caa03] | 25 | |
---|
[7d6351e] | 26 | |
---|
[bffc2ad] | 27 | def read_associations(loader, settings='defaults.xml'): |
---|
[28caa03] | 28 | """ |
---|
[0997158f] | 29 | Read the specified settings file to associate |
---|
| 30 | default readers to file extension. |
---|
| 31 | |
---|
| 32 | :param loader: Loader object |
---|
| 33 | :param settings: path to the XML settings file [string] |
---|
[28caa03] | 34 | """ |
---|
| 35 | reader_dir = os.path.dirname(__file__) |
---|
[bffc2ad] | 36 | path = os.path.join(reader_dir, settings) |
---|
| 37 | |
---|
| 38 | # If we can't find the file in the installation |
---|
| 39 | # directory, look into the execution directory. |
---|
| 40 | if not os.path.isfile(path): |
---|
| 41 | path = os.path.join(os.getcwd(), settings) |
---|
[ed61f2a1] | 42 | if not os.path.isfile(path): |
---|
[c09ace41] | 43 | path = os.path.join(sys.path[0], settings) |
---|
[618e438] | 44 | if not os.path.isfile(path): |
---|
| 45 | path = settings |
---|
[f576de0] | 46 | if not os.path.isfile(path): |
---|
| 47 | path = "./%s" % settings |
---|
[28caa03] | 48 | if os.path.isfile(path): |
---|
[379e15b] | 49 | tree = etree.parse(path, parser=etree.ETCompatXMLParser()) |
---|
[28caa03] | 50 | |
---|
| 51 | # Check the format version number |
---|
[7d6351e] | 52 | # Specifying the namespace will take care of the file format version |
---|
[379e15b] | 53 | root = tree.getroot() |
---|
[28caa03] | 54 | |
---|
| 55 | # Read in the file extension associations |
---|
[fd5ac0d] | 56 | entry_list = root.xpath('/ns:SasLoader/ns:FileType', |
---|
[a7a5886] | 57 | namespaces={'ns': VERSION}) |
---|
[379e15b] | 58 | |
---|
| 59 | # For each FileType entry, get the associated reader and extension |
---|
[28caa03] | 60 | for entry in entry_list: |
---|
[379e15b] | 61 | reader = entry.get('reader') |
---|
[7d6351e] | 62 | ext = entry.get('extension') |
---|
[379e15b] | 63 | |
---|
| 64 | if reader is not None and ext is not None: |
---|
[28caa03] | 65 | # Associate the extension with a particular reader |
---|
[7d6351e] | 66 | # TODO: Modify the Register code to be case-insensitive |
---|
[a7a5886] | 67 | # and remove the extra line below. |
---|
[28caa03] | 68 | try: |
---|
[379e15b] | 69 | exec "import %s" % reader |
---|
[a7a5886] | 70 | exec "loader.associate_file_type('%s', %s)" % (ext.lower(), |
---|
| 71 | reader) |
---|
| 72 | exec "loader.associate_file_type('%s', %s)" % (ext.upper(), |
---|
| 73 | reader) |
---|
[28caa03] | 74 | except: |
---|
[a7a5886] | 75 | msg = "read_associations: skipping association" |
---|
[f4e507b] | 76 | msg += " for %s\n %s" % (ext.lower(), sys.exc_value) |
---|
[a7a5886] | 77 | logging.error(msg) |
---|
[86e2f5a] | 78 | else: |
---|
[496c5bb] | 79 | print "Could not find reader association settings\n %s [%s]" % (__file__, os.getcwd()) |
---|
[28caa03] | 80 | |
---|
| 81 | |
---|
| 82 | def register_readers(registry_function): |
---|
| 83 | """ |
---|
[0997158f] | 84 | Function called by the registry/loader object to register |
---|
| 85 | all default readers using a call back function. |
---|
[28caa03] | 86 | |
---|
[0997158f] | 87 | :WARNING: this method is now obsolete |
---|
| 88 | |
---|
| 89 | :param registry_function: function to be called to register each reader |
---|
[28caa03] | 90 | """ |
---|
[379e15b] | 91 | logging.info("register_readers is now obsolete: use read_associations()") |
---|
[28caa03] | 92 | import abs_reader |
---|
| 93 | import ascii_reader |
---|
| 94 | import cansas_reader |
---|
| 95 | import danse_reader |
---|
| 96 | import hfir1d_reader |
---|
| 97 | import IgorReader |
---|
[ded62ce] | 98 | import red2d_reader |
---|
[3241dd2] | 99 | #import tiff_reader |
---|
[a6d1676] | 100 | import nexus_reader |
---|
[28caa03] | 101 | |
---|
| 102 | registry_function(abs_reader) |
---|
| 103 | registry_function(ascii_reader) |
---|
| 104 | registry_function(cansas_reader) |
---|
| 105 | registry_function(danse_reader) |
---|
| 106 | registry_function(hfir1d_reader) |
---|
| 107 | registry_function(IgorReader) |
---|
[ded62ce] | 108 | registry_function(red2d_reader) |
---|
[3241dd2] | 109 | #registry_function(tiff_reader) |
---|
[d225e32] | 110 | registry_function(nexus_reader) |
---|
[28caa03] | 111 | |
---|
[7d6351e] | 112 | return True |
---|