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