1 | |
---|
2 | |
---|
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 | |
---|
14 | |
---|
15 | """ |
---|
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. |
---|
19 | """ |
---|
20 | |
---|
21 | import os, sys |
---|
22 | import logging |
---|
23 | from lxml import etree |
---|
24 | # Py2exe compatibility: import _elementpath to ensure that py2exe finds it |
---|
25 | from lxml import _elementpath |
---|
26 | |
---|
27 | ## Format version for the XML settings file |
---|
28 | VERSION = 'sansloader/1.0' |
---|
29 | |
---|
30 | def read_associations(loader, settings='defaults.xml'): |
---|
31 | """ |
---|
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] |
---|
37 | """ |
---|
38 | reader_dir = os.path.dirname(__file__) |
---|
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) |
---|
45 | |
---|
46 | if os.path.isfile(path): |
---|
47 | tree = etree.parse(path, parser=etree.ETCompatXMLParser()) |
---|
48 | |
---|
49 | # Check the format version number |
---|
50 | # Specifying the namespace will take care of the file format version |
---|
51 | root = tree.getroot() |
---|
52 | |
---|
53 | # Read in the file extension associations |
---|
54 | entry_list = root.xpath('/ns:SansLoader/ns:FileType', namespaces={'ns': VERSION}) |
---|
55 | |
---|
56 | # For each FileType entry, get the associated reader and extension |
---|
57 | for entry in entry_list: |
---|
58 | reader = entry.get('reader') |
---|
59 | ext = entry.get('extension') |
---|
60 | |
---|
61 | if reader is not None and ext is not None: |
---|
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: |
---|
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) |
---|
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 | """ |
---|
75 | Function called by the registry/loader object to register |
---|
76 | all default readers using a call back function. |
---|
77 | |
---|
78 | :WARNING: this method is now obsolete |
---|
79 | |
---|
80 | :param registry_function: function to be called to register each reader |
---|
81 | """ |
---|
82 | logging.info("register_readers is now obsolete: use read_associations()") |
---|
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 |
---|
90 | import red2d_reader |
---|
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) |
---|
100 | registry_function(red2d_reader) |
---|
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 | |
---|