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