[28caa03] | 1 | """ |
---|
| 2 | This software was developed by the University of Tennessee as part of the |
---|
| 3 | Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 4 | project funded by the US National Science Foundation. |
---|
| 5 | |
---|
| 6 | See the license text in license.txt |
---|
| 7 | |
---|
| 8 | copyright 2009, University of Tennessee |
---|
| 9 | """ |
---|
| 10 | |
---|
| 11 | """ |
---|
| 12 | Module to associate default readers to file extensions. |
---|
| 13 | The module reads an xml file to get the readers for each file extension. |
---|
| 14 | The readers are tried in order they appear when reading a file. |
---|
| 15 | """ |
---|
| 16 | |
---|
| 17 | import os, sys |
---|
| 18 | import logging |
---|
[379e15b] | 19 | from lxml import etree |
---|
[28caa03] | 20 | |
---|
| 21 | ## Format version for the XML settings file |
---|
[379e15b] | 22 | VERSION = 'sansloader/1.0' |
---|
[28caa03] | 23 | |
---|
[bffc2ad] | 24 | def read_associations(loader, settings='defaults.xml'): |
---|
[28caa03] | 25 | """ |
---|
| 26 | Read the specified settings file to associate |
---|
| 27 | default readers to file extension. |
---|
| 28 | |
---|
| 29 | @param loader: Loader object |
---|
[bffc2ad] | 30 | @param settings: path to the XML settings file [string] |
---|
[28caa03] | 31 | """ |
---|
| 32 | reader_dir = os.path.dirname(__file__) |
---|
[bffc2ad] | 33 | path = os.path.join(reader_dir, settings) |
---|
| 34 | |
---|
| 35 | # If we can't find the file in the installation |
---|
| 36 | # directory, look into the execution directory. |
---|
| 37 | if not os.path.isfile(path): |
---|
| 38 | path = os.path.join(os.getcwd(), settings) |
---|
[28caa03] | 39 | |
---|
| 40 | if os.path.isfile(path): |
---|
[379e15b] | 41 | tree = etree.parse(path, parser=etree.ETCompatXMLParser()) |
---|
[28caa03] | 42 | |
---|
| 43 | # Check the format version number |
---|
[379e15b] | 44 | # Specifying the namespace will take care of the file format version |
---|
| 45 | root = tree.getroot() |
---|
[28caa03] | 46 | |
---|
| 47 | # Read in the file extension associations |
---|
[379e15b] | 48 | entry_list = root.xpath('/ns:SansLoader/ns:FileType', namespaces={'ns': VERSION}) |
---|
| 49 | |
---|
| 50 | # For each FileType entry, get the associated reader and extension |
---|
[28caa03] | 51 | for entry in entry_list: |
---|
[379e15b] | 52 | reader = entry.get('reader') |
---|
| 53 | ext = entry.get('extension') |
---|
| 54 | |
---|
| 55 | if reader is not None and ext is not None: |
---|
[28caa03] | 56 | # Associate the extension with a particular reader |
---|
| 57 | # TODO: Modify the Register code to be case-insensitive and remove the |
---|
| 58 | # extra line below. |
---|
| 59 | try: |
---|
[379e15b] | 60 | exec "import %s" % reader |
---|
| 61 | exec "loader.associate_file_type('%s', %s)" % (ext.lower(), reader) |
---|
| 62 | exec "loader.associate_file_type('%s', %s)" % (ext.upper(), reader) |
---|
[28caa03] | 63 | except: |
---|
| 64 | logging.error("read_associations: skipping association for %s\n %s" % (attr['extension'], sys.exc_value)) |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | def register_readers(registry_function): |
---|
| 68 | """ |
---|
| 69 | Function called by the registry/loader object to register |
---|
| 70 | all default readers using a call back function. |
---|
| 71 | |
---|
| 72 | WARNING: this method is now obsolete |
---|
| 73 | |
---|
| 74 | @param registry_function: function to be called to register each reader |
---|
| 75 | """ |
---|
[379e15b] | 76 | logging.info("register_readers is now obsolete: use read_associations()") |
---|
[28caa03] | 77 | import abs_reader |
---|
| 78 | import cansas_reader |
---|
| 79 | import ascii_reader |
---|
| 80 | import cansas_reader |
---|
| 81 | import danse_reader |
---|
| 82 | import hfir1d_reader |
---|
| 83 | import IgorReader |
---|
| 84 | import tiff_reader |
---|
| 85 | |
---|
| 86 | registry_function(abs_reader) |
---|
| 87 | registry_function(cansas_reader) |
---|
| 88 | registry_function(ascii_reader) |
---|
| 89 | registry_function(cansas_reader) |
---|
| 90 | registry_function(danse_reader) |
---|
| 91 | registry_function(hfir1d_reader) |
---|
| 92 | registry_function(IgorReader) |
---|
| 93 | registry_function(tiff_reader) |
---|
| 94 | |
---|
| 95 | return True |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | if __name__ == "__main__": |
---|
| 99 | logging.basicConfig(level=logging.INFO, |
---|
| 100 | format='%(asctime)s %(levelname)s %(message)s', |
---|
| 101 | filename='logger.log', |
---|
| 102 | filemode='w') |
---|
| 103 | from DataLoader.loader import Loader |
---|
| 104 | l = Loader() |
---|
| 105 | read_associations(l) |
---|
| 106 | |
---|
| 107 | |
---|
| 108 | print l.get_wildcards() |
---|
| 109 | |
---|
| 110 | |
---|