[4749514] | 1 | """ |
---|
| 2 | File handler to support different file extensions. |
---|
[270c882b] | 3 | Uses reflectometer registry utility. |
---|
[f06d7fc] | 4 | |
---|
[4749514] | 5 | The default readers are found in the 'readers' sub-module |
---|
| 6 | and registered by default at initialization time. |
---|
[f06d7fc] | 7 | |
---|
[4749514] | 8 | To add a new default reader, one must register it in |
---|
| 9 | the register_readers method found in readers/__init__.py. |
---|
[f06d7fc] | 10 | |
---|
[4749514] | 11 | A utility method (find_plugins) is available to inspect |
---|
| 12 | a directory (for instance, a user plug-in directory) and |
---|
| 13 | look for new readers/writers. |
---|
| 14 | """ |
---|
| 15 | ##################################################################### |
---|
[5d8f9b3] | 16 | # This software was developed by the University of Tennessee as part of the |
---|
| 17 | # Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
| 18 | # project funded by the US National Science Foundation. |
---|
| 19 | # See the license text in license.txt |
---|
| 20 | # copyright 2008, University of Tennessee |
---|
[4749514] | 21 | ###################################################################### |
---|
| 22 | |
---|
| 23 | import os |
---|
| 24 | import sys |
---|
| 25 | import logging |
---|
| 26 | import time |
---|
| 27 | from zipfile import ZipFile |
---|
[b699768] | 28 | from sas.sascalc.data_util.registry import ExtensionRegistry |
---|
[4749514] | 29 | # Default readers are defined in the readers sub-module |
---|
| 30 | import readers |
---|
[da8bb53] | 31 | from loader_exceptions import NoKnownLoaderException, FileContentsException,\ |
---|
| 32 | DefaultReaderException |
---|
[4749514] | 33 | from readers import ascii_reader |
---|
| 34 | from readers import cansas_reader |
---|
[d0764bf] | 35 | from readers import cansas_reader_HDF5 |
---|
[4749514] | 36 | |
---|
[463e7ffc] | 37 | logger = logging.getLogger(__name__) |
---|
[c155a16] | 38 | |
---|
[5d8f9b3] | 39 | |
---|
[4749514] | 40 | class Registry(ExtensionRegistry): |
---|
| 41 | """ |
---|
| 42 | Registry class for file format extensions. |
---|
| 43 | Readers and writers are supported. |
---|
| 44 | """ |
---|
| 45 | def __init__(self): |
---|
| 46 | super(Registry, self).__init__() |
---|
[f06d7fc] | 47 | |
---|
[270c882b] | 48 | # Writers |
---|
[4749514] | 49 | self.writers = {} |
---|
[f06d7fc] | 50 | |
---|
[270c882b] | 51 | # List of wildcards |
---|
[4749514] | 52 | self.wildcards = ['All (*.*)|*.*'] |
---|
[f06d7fc] | 53 | |
---|
[270c882b] | 54 | # Creation time, for testing |
---|
[4749514] | 55 | self._created = time.time() |
---|
[f06d7fc] | 56 | |
---|
[4749514] | 57 | # Register default readers |
---|
| 58 | readers.read_associations(self) |
---|
| 59 | |
---|
| 60 | def load(self, path, format=None): |
---|
| 61 | """ |
---|
| 62 | Call the loader for the file type of path. |
---|
| 63 | |
---|
| 64 | :param path: file path |
---|
| 65 | :param format: explicit extension, to force the use |
---|
| 66 | of a particular reader |
---|
| 67 | |
---|
[b9b612a] | 68 | Defaults to the ascii (multi-column), cansas XML, and cansas NeXuS |
---|
| 69 | readers if no reader was registered for the file's extension. |
---|
[4749514] | 70 | """ |
---|
| 71 | try: |
---|
| 72 | return super(Registry, self).load(path, format=format) |
---|
[371b9e2] | 73 | except NoKnownLoaderException as nkl_e: |
---|
[3ece5dd] | 74 | pass # try the ASCII reader |
---|
| 75 | except FileContentsException as fc_exc: |
---|
| 76 | raise RuntimeError(fc_exc.message) |
---|
| 77 | except Exception: |
---|
[371b9e2] | 78 | pass |
---|
[b9b612a] | 79 | try: |
---|
| 80 | ascii_loader = ascii_reader.Reader() |
---|
| 81 | return ascii_loader.read(path) |
---|
[da8bb53] | 82 | except DefaultReaderException: |
---|
[3ece5dd] | 83 | pass # Loader specific error to try the cansas XML reader |
---|
| 84 | except FileContentsException as e: |
---|
[371b9e2] | 85 | # TODO: handle error |
---|
[3ece5dd] | 86 | raise RuntimeError(e) |
---|
[b9b612a] | 87 | try: |
---|
| 88 | cansas_loader = cansas_reader.Reader() |
---|
| 89 | return cansas_loader.read(path) |
---|
[da8bb53] | 90 | except DefaultReaderException: |
---|
| 91 | pass # Loader specific error to try the cansas NeXuS reader |
---|
[3ece5dd] | 92 | except FileContentsException as e: |
---|
[da8bb53] | 93 | # TODO: Handle errors properly |
---|
[3ece5dd] | 94 | raise RuntimeError(e) |
---|
[da8bb53] | 95 | except Exception as csr: |
---|
| 96 | # TODO: Modify cansas reader to throw DefaultReaderException |
---|
| 97 | pass |
---|
[b9b612a] | 98 | try: |
---|
| 99 | cansas_nexus_loader = cansas_reader_HDF5.Reader() |
---|
| 100 | return cansas_nexus_loader.read(path) |
---|
[8dec7e7] | 101 | except DefaultReaderException as e: |
---|
| 102 | logging.error("No default loader can load the data") |
---|
[b9b612a] | 103 | # No known reader available. Give up and throw an error |
---|
| 104 | msg = "\n\tUnknown data format: %s.\n\tThe file is not a " % path |
---|
[7f75a3f] | 105 | msg += "known format that can be loaded by SasView.\n" |
---|
| 106 | raise NoKnownLoaderException, msg |
---|
[8dec7e7] | 107 | except FileContentsException as e: |
---|
[da8bb53] | 108 | # TODO: Handle error(s) properly |
---|
[8dec7e7] | 109 | raise RuntimeError(e) |
---|
[f06d7fc] | 110 | |
---|
[4749514] | 111 | def find_plugins(self, dir): |
---|
| 112 | """ |
---|
| 113 | Find readers in a given directory. This method |
---|
| 114 | can be used to inspect user plug-in directories to |
---|
| 115 | find new readers/writers. |
---|
[f06d7fc] | 116 | |
---|
[4749514] | 117 | :param dir: directory to search into |
---|
| 118 | :return: number of readers found |
---|
| 119 | """ |
---|
| 120 | readers_found = 0 |
---|
| 121 | temp_path = os.path.abspath(dir) |
---|
| 122 | if not os.path.isdir(temp_path): |
---|
| 123 | temp_path = os.path.join(os.getcwd(), dir) |
---|
| 124 | if not os.path.isdir(temp_path): |
---|
| 125 | temp_path = os.path.join(os.path.dirname(__file__), dir) |
---|
| 126 | if not os.path.isdir(temp_path): |
---|
| 127 | temp_path = os.path.join(os.path.dirname(sys.path[0]), dir) |
---|
[f06d7fc] | 128 | |
---|
[4749514] | 129 | dir = temp_path |
---|
| 130 | # Check whether the directory exists |
---|
| 131 | if not os.path.isdir(dir): |
---|
| 132 | msg = "DataLoader couldn't locate DataLoader plugin folder." |
---|
| 133 | msg += """ "%s" does not exist""" % dir |
---|
[c155a16] | 134 | logger.warning(msg) |
---|
[4749514] | 135 | return readers_found |
---|
[f06d7fc] | 136 | |
---|
[4749514] | 137 | for item in os.listdir(dir): |
---|
| 138 | full_path = os.path.join(dir, item) |
---|
| 139 | if os.path.isfile(full_path): |
---|
[f06d7fc] | 140 | |
---|
[4749514] | 141 | # Process python files |
---|
| 142 | if item.endswith('.py'): |
---|
| 143 | toks = os.path.splitext(os.path.basename(item)) |
---|
| 144 | try: |
---|
| 145 | sys.path.insert(0, os.path.abspath(dir)) |
---|
| 146 | module = __import__(toks[0], globals(), locals()) |
---|
| 147 | if self._identify_plugin(module): |
---|
| 148 | readers_found += 1 |
---|
| 149 | except: |
---|
| 150 | msg = "Loader: Error importing " |
---|
| 151 | msg += "%s\n %s" % (item, sys.exc_value) |
---|
[c155a16] | 152 | logger.error(msg) |
---|
[f06d7fc] | 153 | |
---|
[4749514] | 154 | # Process zip files |
---|
| 155 | elif item.endswith('.zip'): |
---|
| 156 | try: |
---|
| 157 | # Find the modules in the zip file |
---|
| 158 | zfile = ZipFile(item) |
---|
| 159 | nlist = zfile.namelist() |
---|
[f06d7fc] | 160 | |
---|
[4749514] | 161 | sys.path.insert(0, item) |
---|
| 162 | for mfile in nlist: |
---|
| 163 | try: |
---|
| 164 | # Change OS path to python path |
---|
| 165 | fullname = mfile.replace('/', '.') |
---|
| 166 | fullname = os.path.splitext(fullname)[0] |
---|
| 167 | module = __import__(fullname, globals(), |
---|
[f06d7fc] | 168 | locals(), [""]) |
---|
[4749514] | 169 | if self._identify_plugin(module): |
---|
| 170 | readers_found += 1 |
---|
| 171 | except: |
---|
| 172 | msg = "Loader: Error importing" |
---|
| 173 | msg += " %s\n %s" % (mfile, sys.exc_value) |
---|
[c155a16] | 174 | logger.error(msg) |
---|
[f06d7fc] | 175 | |
---|
[4749514] | 176 | except: |
---|
| 177 | msg = "Loader: Error importing " |
---|
| 178 | msg += " %s\n %s" % (item, sys.exc_value) |
---|
[c155a16] | 179 | logger.error(msg) |
---|
[f06d7fc] | 180 | |
---|
[4749514] | 181 | return readers_found |
---|
[f06d7fc] | 182 | |
---|
[4749514] | 183 | def associate_file_type(self, ext, module): |
---|
| 184 | """ |
---|
| 185 | Look into a module to find whether it contains a |
---|
| 186 | Reader class. If so, APPEND it to readers and (potentially) |
---|
| 187 | to the list of writers for the given extension |
---|
[f06d7fc] | 188 | |
---|
[4749514] | 189 | :param ext: file extension [string] |
---|
| 190 | :param module: module object |
---|
| 191 | """ |
---|
| 192 | reader_found = False |
---|
[f06d7fc] | 193 | |
---|
[4749514] | 194 | if hasattr(module, "Reader"): |
---|
| 195 | try: |
---|
| 196 | # Find supported extensions |
---|
| 197 | loader = module.Reader() |
---|
| 198 | if ext not in self.loaders: |
---|
| 199 | self.loaders[ext] = [] |
---|
| 200 | # Append the new reader to the list |
---|
| 201 | self.loaders[ext].append(loader.read) |
---|
| 202 | |
---|
| 203 | reader_found = True |
---|
[f06d7fc] | 204 | |
---|
[4749514] | 205 | # Keep track of wildcards |
---|
| 206 | type_name = module.__name__ |
---|
| 207 | if hasattr(loader, 'type_name'): |
---|
| 208 | type_name = loader.type_name |
---|
[f06d7fc] | 209 | |
---|
[4749514] | 210 | wcard = "%s files (*%s)|*%s" % (type_name, ext.lower(), |
---|
[f06d7fc] | 211 | ext.lower()) |
---|
[4749514] | 212 | if wcard not in self.wildcards: |
---|
| 213 | self.wildcards.append(wcard) |
---|
[f06d7fc] | 214 | |
---|
[4749514] | 215 | # Check whether writing is supported |
---|
| 216 | if hasattr(loader, 'write'): |
---|
| 217 | if ext not in self.writers: |
---|
| 218 | self.writers[ext] = [] |
---|
| 219 | # Append the new writer to the list |
---|
| 220 | self.writers[ext].append(loader.write) |
---|
[f06d7fc] | 221 | |
---|
[4749514] | 222 | except: |
---|
| 223 | msg = "Loader: Error accessing" |
---|
| 224 | msg += " Reader in %s\n %s" % (module.__name__, sys.exc_value) |
---|
[c155a16] | 225 | logger.error(msg) |
---|
[4749514] | 226 | return reader_found |
---|
| 227 | |
---|
| 228 | def associate_file_reader(self, ext, loader): |
---|
| 229 | """ |
---|
| 230 | Append a reader object to readers |
---|
[f06d7fc] | 231 | |
---|
[4749514] | 232 | :param ext: file extension [string] |
---|
| 233 | :param module: reader object |
---|
| 234 | """ |
---|
| 235 | reader_found = False |
---|
[f06d7fc] | 236 | |
---|
[4749514] | 237 | try: |
---|
| 238 | # Find supported extensions |
---|
| 239 | if ext not in self.loaders: |
---|
| 240 | self.loaders[ext] = [] |
---|
| 241 | # Append the new reader to the list |
---|
| 242 | self.loaders[ext].append(loader.read) |
---|
| 243 | |
---|
| 244 | reader_found = True |
---|
[f06d7fc] | 245 | |
---|
[4749514] | 246 | # Keep track of wildcards |
---|
| 247 | if hasattr(loader, 'type_name'): |
---|
| 248 | type_name = loader.type_name |
---|
[f06d7fc] | 249 | |
---|
[4749514] | 250 | wcard = "%s files (*%s)|*%s" % (type_name, ext.lower(), |
---|
| 251 | ext.lower()) |
---|
| 252 | if wcard not in self.wildcards: |
---|
| 253 | self.wildcards.append(wcard) |
---|
[f06d7fc] | 254 | |
---|
[4749514] | 255 | except: |
---|
| 256 | msg = "Loader: Error accessing Reader " |
---|
| 257 | msg += "in %s\n %s" % (loader.__name__, sys.exc_value) |
---|
[c155a16] | 258 | logger.error(msg) |
---|
[4749514] | 259 | return reader_found |
---|
| 260 | |
---|
| 261 | def _identify_plugin(self, module): |
---|
| 262 | """ |
---|
[f06d7fc] | 263 | Look into a module to find whether it contains a |
---|
[4749514] | 264 | Reader class. If so, add it to readers and (potentially) |
---|
| 265 | to the list of writers. |
---|
| 266 | :param module: module object |
---|
[f06d7fc] | 267 | |
---|
[4749514] | 268 | """ |
---|
| 269 | reader_found = False |
---|
[f06d7fc] | 270 | |
---|
[4749514] | 271 | if hasattr(module, "Reader"): |
---|
| 272 | try: |
---|
| 273 | # Find supported extensions |
---|
| 274 | loader = module.Reader() |
---|
| 275 | for ext in loader.ext: |
---|
| 276 | if ext not in self.loaders: |
---|
| 277 | self.loaders[ext] = [] |
---|
| 278 | # When finding a reader at run time, |
---|
| 279 | # treat this reader as the new default |
---|
| 280 | self.loaders[ext].insert(0, loader.read) |
---|
| 281 | |
---|
| 282 | reader_found = True |
---|
[f06d7fc] | 283 | |
---|
[4749514] | 284 | # Keep track of wildcards |
---|
| 285 | type_name = module.__name__ |
---|
| 286 | if hasattr(loader, 'type_name'): |
---|
| 287 | type_name = loader.type_name |
---|
| 288 | wcard = "%s files (*%s)|*%s" % (type_name, ext.lower(), |
---|
[f06d7fc] | 289 | ext.lower()) |
---|
[4749514] | 290 | if wcard not in self.wildcards: |
---|
| 291 | self.wildcards.append(wcard) |
---|
[f06d7fc] | 292 | |
---|
[4749514] | 293 | # Check whether writing is supported |
---|
| 294 | if hasattr(loader, 'write'): |
---|
| 295 | for ext in loader.ext: |
---|
| 296 | if ext not in self.writers: |
---|
| 297 | self.writers[ext] = [] |
---|
| 298 | self.writers[ext].insert(0, loader.write) |
---|
[f06d7fc] | 299 | |
---|
[4749514] | 300 | except: |
---|
| 301 | msg = "Loader: Error accessing Reader" |
---|
| 302 | msg += " in %s\n %s" % (module.__name__, sys.exc_value) |
---|
[c155a16] | 303 | logger.error(msg) |
---|
[4749514] | 304 | return reader_found |
---|
| 305 | |
---|
| 306 | def lookup_writers(self, path): |
---|
| 307 | """ |
---|
| 308 | :return: the loader associated with the file type of path. |
---|
| 309 | :Raises ValueError: if file type is not known. |
---|
| 310 | """ |
---|
| 311 | # Find matching extensions |
---|
| 312 | extlist = [ext for ext in self.extensions() if path.endswith(ext)] |
---|
| 313 | # Sort matching extensions by decreasing order of length |
---|
| 314 | extlist.sort(lambda a, b: len(a) < len(b)) |
---|
| 315 | # Combine loaders for matching extensions into one big list |
---|
| 316 | writers = [] |
---|
| 317 | for L in [self.writers[ext] for ext in extlist]: |
---|
| 318 | writers.extend(L) |
---|
| 319 | # Remove duplicates if they exist |
---|
| 320 | if len(writers) != len(set(writers)): |
---|
| 321 | result = [] |
---|
| 322 | for L in writers: |
---|
| 323 | if L not in result: |
---|
| 324 | result.append(L) |
---|
| 325 | writers = L |
---|
| 326 | # Raise an error if there are no matching extensions |
---|
| 327 | if len(writers) == 0: |
---|
| 328 | raise ValueError, "Unknown file type for " + path |
---|
| 329 | # All done |
---|
| 330 | return writers |
---|
| 331 | |
---|
| 332 | def save(self, path, data, format=None): |
---|
| 333 | """ |
---|
| 334 | Call the writer for the file type of path. |
---|
| 335 | |
---|
| 336 | Raises ValueError if no writer is available. |
---|
| 337 | Raises KeyError if format is not available. |
---|
| 338 | May raise a writer-defined exception if writer fails. |
---|
| 339 | """ |
---|
| 340 | if format is None: |
---|
| 341 | writers = self.lookup_writers(path) |
---|
| 342 | else: |
---|
| 343 | writers = self.writers[format] |
---|
| 344 | for fn in writers: |
---|
| 345 | try: |
---|
| 346 | return fn(path, data) |
---|
| 347 | except: |
---|
| 348 | pass # give other loaders a chance to succeed |
---|
| 349 | # If we get here it is because all loaders failed |
---|
| 350 | raise # reraises last exception |
---|
| 351 | |
---|
[f06d7fc] | 352 | |
---|
[4749514] | 353 | class Loader(object): |
---|
| 354 | """ |
---|
| 355 | Utility class to use the Registry as a singleton. |
---|
| 356 | """ |
---|
| 357 | ## Registry instance |
---|
| 358 | __registry = Registry() |
---|
[f06d7fc] | 359 | |
---|
[4749514] | 360 | def associate_file_type(self, ext, module): |
---|
| 361 | """ |
---|
| 362 | Look into a module to find whether it contains a |
---|
| 363 | Reader class. If so, append it to readers and (potentially) |
---|
| 364 | to the list of writers for the given extension |
---|
[f06d7fc] | 365 | |
---|
[4749514] | 366 | :param ext: file extension [string] |
---|
| 367 | :param module: module object |
---|
| 368 | """ |
---|
| 369 | return self.__registry.associate_file_type(ext, module) |
---|
| 370 | |
---|
| 371 | def associate_file_reader(self, ext, loader): |
---|
| 372 | """ |
---|
| 373 | Append a reader object to readers |
---|
[f06d7fc] | 374 | |
---|
[4749514] | 375 | :param ext: file extension [string] |
---|
| 376 | :param module: reader object |
---|
| 377 | """ |
---|
| 378 | return self.__registry.associate_file_reader(ext, loader) |
---|
| 379 | |
---|
| 380 | def load(self, file, format=None): |
---|
| 381 | """ |
---|
| 382 | Load a file |
---|
[f06d7fc] | 383 | |
---|
[4749514] | 384 | :param file: file name (path) |
---|
| 385 | :param format: specified format to use (optional) |
---|
| 386 | :return: DataInfo object |
---|
| 387 | """ |
---|
| 388 | return self.__registry.load(file, format) |
---|
[f06d7fc] | 389 | |
---|
[4749514] | 390 | def save(self, file, data, format): |
---|
| 391 | """ |
---|
| 392 | Save a DataInfo object to file |
---|
| 393 | :param file: file name (path) |
---|
| 394 | :param data: DataInfo object |
---|
[f06d7fc] | 395 | :param format: format to write the data in |
---|
[4749514] | 396 | """ |
---|
| 397 | return self.__registry.save(file, data, format) |
---|
[f06d7fc] | 398 | |
---|
[4749514] | 399 | def _get_registry_creation_time(self): |
---|
| 400 | """ |
---|
| 401 | Internal method used to test the uniqueness |
---|
| 402 | of the registry object |
---|
| 403 | """ |
---|
| 404 | return self.__registry._created |
---|
[f06d7fc] | 405 | |
---|
| 406 | def find_plugins(self, directory): |
---|
[4749514] | 407 | """ |
---|
| 408 | Find plugins in a given directory |
---|
[f06d7fc] | 409 | |
---|
[4749514] | 410 | :param dir: directory to look into to find new readers/writers |
---|
| 411 | """ |
---|
[f06d7fc] | 412 | return self.__registry.find_plugins(directory) |
---|
| 413 | |
---|
[4749514] | 414 | def get_wildcards(self): |
---|
[f06d7fc] | 415 | """ |
---|
| 416 | Return the list of wildcards |
---|
| 417 | """ |
---|
[4749514] | 418 | return self.__registry.wildcards |
---|