[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 | from xml import xpath |
---|
| 19 | import xml.dom.minidom |
---|
| 20 | import logging |
---|
| 21 | |
---|
| 22 | from xml.dom.minidom import parse |
---|
| 23 | |
---|
| 24 | ## Format version for the XML settings file |
---|
| 25 | VERSION = '1.0' |
---|
| 26 | |
---|
| 27 | def get_node_text(node): |
---|
| 28 | """ |
---|
| 29 | Get the text context of a node |
---|
| 30 | |
---|
| 31 | @param node: node to read from |
---|
| 32 | @return: content, attribute list |
---|
| 33 | """ |
---|
| 34 | content = None |
---|
| 35 | attr = {} |
---|
| 36 | for item in node.childNodes: |
---|
| 37 | if item.nodeName.find('text')>=0 \ |
---|
| 38 | and len(item.nodeValue.strip())>0: |
---|
| 39 | content = item.nodeValue.strip() |
---|
| 40 | break |
---|
| 41 | |
---|
| 42 | if node.hasAttributes(): |
---|
| 43 | for i in range(node.attributes.length): |
---|
| 44 | attr[node.attributes.item(i).nodeName] \ |
---|
| 45 | = node.attributes.item(i).nodeValue |
---|
| 46 | |
---|
| 47 | return content, attr |
---|
| 48 | |
---|
[bffc2ad] | 49 | def read_associations(loader, settings='defaults.xml'): |
---|
[28caa03] | 50 | """ |
---|
| 51 | Read the specified settings file to associate |
---|
| 52 | default readers to file extension. |
---|
| 53 | |
---|
| 54 | @param loader: Loader object |
---|
[bffc2ad] | 55 | @param settings: path to the XML settings file [string] |
---|
[28caa03] | 56 | """ |
---|
| 57 | reader_dir = os.path.dirname(__file__) |
---|
[bffc2ad] | 58 | path = os.path.join(reader_dir, settings) |
---|
| 59 | |
---|
| 60 | # If we can't find the file in the installation |
---|
| 61 | # directory, look into the execution directory. |
---|
| 62 | if not os.path.isfile(path): |
---|
| 63 | path = os.path.join(os.getcwd(), settings) |
---|
[28caa03] | 64 | |
---|
| 65 | if os.path.isfile(path): |
---|
| 66 | dom = parse(path) |
---|
| 67 | |
---|
| 68 | # Check the format version number |
---|
| 69 | nodes = xpath.Evaluate('SansLoader', dom) |
---|
| 70 | if nodes[0].hasAttributes(): |
---|
| 71 | for i in range(nodes[0].attributes.length): |
---|
| 72 | if nodes[0].attributes.item(i).nodeName=='version': |
---|
| 73 | if nodes[0].attributes.item(i).nodeValue != VERSION: |
---|
| 74 | raise ValueError, "associations: unrecognized SansLoader version number %s" % \ |
---|
| 75 | nodes[0].attributes.item(i).nodeValue |
---|
| 76 | |
---|
| 77 | # Read in the file extension associations |
---|
| 78 | entry_list = xpath.Evaluate('SansLoader/FileType', dom) |
---|
| 79 | for entry in entry_list: |
---|
| 80 | value, attr = get_node_text(entry) |
---|
| 81 | if attr is not None \ |
---|
| 82 | and attr.has_key('reader') and attr.has_key('extension'): |
---|
| 83 | |
---|
| 84 | # Associate the extension with a particular reader |
---|
| 85 | # TODO: Modify the Register code to be case-insensitive and remove the |
---|
| 86 | # extra line below. |
---|
| 87 | try: |
---|
| 88 | exec "import %s" % attr['reader'] |
---|
| 89 | exec "loader.associate_file_type('%s', %s)" % (attr['extension'].lower(), attr['reader']) |
---|
| 90 | exec "loader.associate_file_type('%s', %s)" % (attr['extension'].upper(), attr['reader']) |
---|
| 91 | except: |
---|
| 92 | logging.error("read_associations: skipping association for %s\n %s" % (attr['extension'], sys.exc_value)) |
---|
| 93 | |
---|
| 94 | |
---|
| 95 | def register_readers(registry_function): |
---|
| 96 | """ |
---|
| 97 | Function called by the registry/loader object to register |
---|
| 98 | all default readers using a call back function. |
---|
| 99 | |
---|
| 100 | WARNING: this method is now obsolete |
---|
| 101 | |
---|
| 102 | @param registry_function: function to be called to register each reader |
---|
| 103 | """ |
---|
| 104 | import abs_reader |
---|
| 105 | import cansas_reader |
---|
| 106 | import ascii_reader |
---|
| 107 | import cansas_reader |
---|
| 108 | import danse_reader |
---|
| 109 | import hfir1d_reader |
---|
| 110 | import IgorReader |
---|
| 111 | import tiff_reader |
---|
| 112 | |
---|
| 113 | registry_function(abs_reader) |
---|
| 114 | registry_function(cansas_reader) |
---|
| 115 | registry_function(ascii_reader) |
---|
| 116 | registry_function(cansas_reader) |
---|
| 117 | registry_function(danse_reader) |
---|
| 118 | registry_function(hfir1d_reader) |
---|
| 119 | registry_function(IgorReader) |
---|
| 120 | registry_function(tiff_reader) |
---|
| 121 | |
---|
| 122 | return True |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | if __name__ == "__main__": |
---|
| 126 | logging.basicConfig(level=logging.INFO, |
---|
| 127 | format='%(asctime)s %(levelname)s %(message)s', |
---|
| 128 | filename='logger.log', |
---|
| 129 | filemode='w') |
---|
| 130 | from DataLoader.loader import Loader |
---|
| 131 | l = Loader() |
---|
| 132 | read_associations(l) |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | print l.get_wildcards() |
---|
| 136 | |
---|
| 137 | |
---|