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 |
---|
19 | from lxml import etree |
---|
20 | # Py2exe compatibility: import _elementpath to ensure that py2exe finds it |
---|
21 | from lxml import _elementpath |
---|
22 | |
---|
23 | ## Format version for the XML settings file |
---|
24 | VERSION = 'sansloader/1.0' |
---|
25 | |
---|
26 | def read_associations(loader, settings='defaults.xml'): |
---|
27 | """ |
---|
28 | Read the specified settings file to associate |
---|
29 | default readers to file extension. |
---|
30 | |
---|
31 | @param loader: Loader object |
---|
32 | @param settings: path to the XML settings file [string] |
---|
33 | """ |
---|
34 | reader_dir = os.path.dirname(__file__) |
---|
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) |
---|
41 | |
---|
42 | if os.path.isfile(path): |
---|
43 | tree = etree.parse(path, parser=etree.ETCompatXMLParser()) |
---|
44 | |
---|
45 | # Check the format version number |
---|
46 | # Specifying the namespace will take care of the file format version |
---|
47 | root = tree.getroot() |
---|
48 | |
---|
49 | # Read in the file extension associations |
---|
50 | entry_list = root.xpath('/ns:SansLoader/ns:FileType', namespaces={'ns': VERSION}) |
---|
51 | |
---|
52 | # For each FileType entry, get the associated reader and extension |
---|
53 | for entry in entry_list: |
---|
54 | reader = entry.get('reader') |
---|
55 | ext = entry.get('extension') |
---|
56 | |
---|
57 | if reader is not None and ext is not None: |
---|
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: |
---|
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) |
---|
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 | """ |
---|
78 | logging.info("register_readers is now obsolete: use read_associations()") |
---|
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 |
---|
86 | import tiff_reader |
---|
87 | |
---|
88 | registry_function(abs_reader) |
---|
89 | registry_function(cansas_reader) |
---|
90 | registry_function(ascii_reader) |
---|
91 | registry_function(cansas_reader) |
---|
92 | registry_function(danse_reader) |
---|
93 | registry_function(hfir1d_reader) |
---|
94 | registry_function(IgorReader) |
---|
95 | registry_function(tiff_reader) |
---|
96 | |
---|
97 | return True |
---|
98 | |
---|
99 | |
---|
100 | if __name__ == "__main__": |
---|
101 | logging.basicConfig(level=logging.INFO, |
---|
102 | format='%(asctime)s %(levelname)s %(message)s', |
---|
103 | filename='logger.log', |
---|
104 | filemode='w') |
---|
105 | from DataLoader.loader import Loader |
---|
106 | l = Loader() |
---|
107 | read_associations(l) |
---|
108 | |
---|
109 | |
---|
110 | print l.get_wildcards() |
---|
111 | |
---|
112 | |
---|