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