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