[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 | |
---|
| 49 | def read_associations(loader, path='defaults.xml'): |
---|
| 50 | """ |
---|
| 51 | Read the specified settings file to associate |
---|
| 52 | default readers to file extension. |
---|
| 53 | |
---|
| 54 | @param loader: Loader object |
---|
| 55 | @param path: path to the XML settings file |
---|
| 56 | """ |
---|
| 57 | reader_dir = os.path.dirname(__file__) |
---|
| 58 | path = os.path.join(reader_dir, path) |
---|
| 59 | |
---|
| 60 | if os.path.isfile(path): |
---|
| 61 | dom = parse(path) |
---|
| 62 | |
---|
| 63 | # Check the format version number |
---|
| 64 | nodes = xpath.Evaluate('SansLoader', dom) |
---|
| 65 | if nodes[0].hasAttributes(): |
---|
| 66 | for i in range(nodes[0].attributes.length): |
---|
| 67 | if nodes[0].attributes.item(i).nodeName=='version': |
---|
| 68 | if nodes[0].attributes.item(i).nodeValue != VERSION: |
---|
| 69 | raise ValueError, "associations: unrecognized SansLoader version number %s" % \ |
---|
| 70 | nodes[0].attributes.item(i).nodeValue |
---|
| 71 | |
---|
| 72 | # Read in the file extension associations |
---|
| 73 | entry_list = xpath.Evaluate('SansLoader/FileType', dom) |
---|
| 74 | for entry in entry_list: |
---|
| 75 | value, attr = get_node_text(entry) |
---|
| 76 | if attr is not None \ |
---|
| 77 | and attr.has_key('reader') and attr.has_key('extension'): |
---|
| 78 | |
---|
| 79 | # Associate the extension with a particular reader |
---|
| 80 | # TODO: Modify the Register code to be case-insensitive and remove the |
---|
| 81 | # extra line below. |
---|
| 82 | try: |
---|
| 83 | exec "import %s" % attr['reader'] |
---|
| 84 | exec "loader.associate_file_type('%s', %s)" % (attr['extension'].lower(), attr['reader']) |
---|
| 85 | exec "loader.associate_file_type('%s', %s)" % (attr['extension'].upper(), attr['reader']) |
---|
| 86 | except: |
---|
| 87 | logging.error("read_associations: skipping association for %s\n %s" % (attr['extension'], sys.exc_value)) |
---|
| 88 | |
---|
| 89 | |
---|
| 90 | def register_readers(registry_function): |
---|
| 91 | """ |
---|
| 92 | Function called by the registry/loader object to register |
---|
| 93 | all default readers using a call back function. |
---|
| 94 | |
---|
| 95 | WARNING: this method is now obsolete |
---|
| 96 | |
---|
| 97 | @param registry_function: function to be called to register each reader |
---|
| 98 | """ |
---|
| 99 | import abs_reader |
---|
| 100 | import cansas_reader |
---|
| 101 | import ascii_reader |
---|
| 102 | import cansas_reader |
---|
| 103 | import danse_reader |
---|
| 104 | import hfir1d_reader |
---|
| 105 | import IgorReader |
---|
| 106 | import tiff_reader |
---|
| 107 | |
---|
| 108 | registry_function(abs_reader) |
---|
| 109 | registry_function(cansas_reader) |
---|
| 110 | registry_function(ascii_reader) |
---|
| 111 | registry_function(cansas_reader) |
---|
| 112 | registry_function(danse_reader) |
---|
| 113 | registry_function(hfir1d_reader) |
---|
| 114 | registry_function(IgorReader) |
---|
| 115 | registry_function(tiff_reader) |
---|
| 116 | |
---|
| 117 | return True |
---|
| 118 | |
---|
| 119 | |
---|
| 120 | if __name__ == "__main__": |
---|
| 121 | logging.basicConfig(level=logging.INFO, |
---|
| 122 | format='%(asctime)s %(levelname)s %(message)s', |
---|
| 123 | filename='logger.log', |
---|
| 124 | filemode='w') |
---|
| 125 | from DataLoader.loader import Loader |
---|
| 126 | l = Loader() |
---|
| 127 | read_associations(l) |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | print l.get_wildcards() |
---|
| 131 | |
---|
| 132 | |
---|