Changeset f60a8c2 in sasview for sansdataloader/src/sans/dataloader/loader.py
- Timestamp:
- Apr 27, 2012 10:42:24 AM (12 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 7d6351e
- Parents:
- 10bfeb3
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sansdataloader/src/sans/dataloader/loader.py
r371cb85 rf60a8c2 1 1 """ 2 File handler to support different file extensions. 3 Uses reflectometry's registry utility. 4 5 The default readers are found in the 'readers' sub-module 6 and registered by default at initialization time. 7 8 To add a new default reader, one must register it in 9 the register_readers method found in readers/__init__.py. 10 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 """ 2 15 ##################################################################### 3 16 #This software was developed by the University of Tennessee as part of the 4 17 #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) 5 #project funded by the US National Science Foundation. 18 #project funded by the US National Science Foundation. 6 19 #See the license text in license.txt 7 20 #copyright 2008, University of Tennessee 8 21 ###################################################################### 9 22 10 """ 11 File handler to support different file extensions. 12 Uses reflectometry's registry utility. 13 14 The default readers are found in the 'readers' sub-module 15 and registered by default at initialization time. 16 17 To add a new default reader, one must register it in 18 the register_readers method found in readers/__init__.py. 19 20 A utility method (find_plugins) is available to inspect 21 a directory (for instance, a user plug-in directory) and 22 look for new readers/writers. 23 """ 24 25 import os 23 import os 26 24 import sys 27 25 import logging … … 55 53 readers.read_associations(self) 56 54 57 #TODO: remove the following line when ready to switch to 55 #TODO: remove the following line when ready to switch to 58 56 #the new default readers 59 57 #readers.register_readers(self._identify_plugin) … … 62 60 #self.find_plugins('plugins') 63 61 64 65 62 def load(self, path, format=None): 66 63 """ … … 68 65 69 66 :param path: file path 70 :param format: explicit extension, to force the use 67 :param format: explicit extension, to force the use 71 68 of a particular reader 72 69 73 70 Defaults to the ascii (multi-column) reader 74 71 if no reader was registered for the file's 75 extension. 72 extension. 76 73 """ 77 74 try: … … 107 104 dir = temp_path 108 105 # Check whether the directory exists 109 if not os.path.isdir(dir): 106 if not os.path.isdir(dir): 110 107 msg = "DataLoader couldn't locate DataLoader plugin folder." 111 108 msg += """ "%s" does not exist""" % dir … … 157 154 logging.error(msg) 158 155 159 return readers_found 156 return readers_found 160 157 161 158 def associate_file_type(self, ext, module): 162 159 """ 163 Look into a module to find whether it contains a 160 Look into a module to find whether it contains a 164 161 Reader class. If so, APPEND it to readers and (potentially) 165 162 to the list of writers for the given extension … … 226 223 type_name = loader.type_name 227 224 228 wcard = "%s files (*%s)|*%s" % (type_name, ext.lower(), 225 wcard = "%s files (*%s)|*%s" % (type_name, ext.lower(), 229 226 ext.lower()) 230 227 if wcard not in self.wildcards: … … 237 234 return reader_found 238 235 239 240 236 def _identify_plugin(self, module): 241 237 """ … … 268 264 ext.lower()) 269 265 if wcard not in self.wildcards: 270 266 self.wildcards.append(wcard) 271 267 272 268 # Check whether writing is supported … … 275 271 if ext not in self.writers: 276 272 self.writers[ext] = [] 277 self.writers[ext].insert(0, loader.write)273 self.writers[ext].insert(0, loader.write) 278 274 279 275 except: … … 288 284 289 285 :Raises ValueError: if file type is not known. 290 """ 286 """ 291 287 # Find matching extensions 292 288 extlist = [ext for ext in self.extensions() if path.endswith(ext)] … … 301 297 result = [] 302 298 for L in writers: 303 if L not in result: result.append(L) 299 if L not in result: 300 result.append(L) 304 301 writers = L 305 302 # Raise an error if there are no matching extensions … … 316 313 Raises KeyError if format is not available. 317 314 318 May raise a writer-defined exception if writer fails. 315 May raise a writer-defined exception if writer fails. 319 316 """ 320 317 if format is None: … … 326 323 return fn(path, data) 327 324 except: 328 pass # give other loaders a chance to succeed325 pass # give other loaders a chance to succeed 329 326 # If we get here it is because all loaders failed 330 raise # reraises last exception327 raise # reraises last exception 331 328 332 329 … … 340 337 def associate_file_type(self, ext, module): 341 338 """ 342 Look into a module to find whether it contains a 339 Look into a module to find whether it contains a 343 340 Reader class. If so, append it to readers and (potentially) 344 341 to the list of writers for the given extension … … 394 391 def get_wildcards(self): 395 392 return self.__registry.wildcards 396 397 if __name__ == "__main__":398 logging.basicConfig(level=logging.INFO,399 format='%(asctime)s %(levelname)s %(message)s',400 filename='loader.log',401 filemode='w')402 l = Loader()403 test_data = l.load('test/cansas1d.xml')404 l.save('test_file.xml', test_data, '.xml')405 406 print l.get_wildcards()407 408 409
Note: See TracChangeset
for help on using the changeset viewer.