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