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