- Timestamp:
- Mar 3, 2015 9:59:47 AM (10 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:
- c8a6c3d7
- Parents:
- 038c00cf
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/dataloader/loader.py
r4749514 rf06d7fc 2 2 File handler to support different file extensions. 3 3 Uses reflectometry's registry utility. 4 4 5 5 The default readers are found in the 'readers' sub-module 6 6 and registered by default at initialization time. 7 7 8 8 To add a new default reader, one must register it in 9 9 the register_readers method found in readers/__init__.py. 10 10 11 11 A utility method (find_plugins) is available to inspect 12 12 a directory (for instance, a user plug-in directory) and … … 37 37 Readers and writers are supported. 38 38 """ 39 39 40 40 def __init__(self): 41 41 super(Registry, self).__init__() 42 42 43 43 ## Writers 44 44 self.writers = {} 45 45 46 46 ## List of wildcards 47 47 self.wildcards = ['All (*.*)|*.*'] 48 48 49 49 ## Creation time, for testing 50 50 self._created = time.time() 51 51 52 52 # Register default readers 53 53 readers.read_associations(self) 54 55 #TODO: remove the following line when ready to switch to56 #the new default readers57 #readers.register_readers(self._identify_plugin)58 59 # Look for plug-in readers60 #self.find_plugins('plugins')61 54 62 55 def load(self, path, format=None): … … 82 75 cansas_loader = cansas_reader.Reader() 83 76 return cansas_loader.read(path) 84 77 85 78 def find_plugins(self, dir): 86 79 """ … … 88 81 can be used to inspect user plug-in directories to 89 82 find new readers/writers. 90 83 91 84 :param dir: directory to search into 92 93 85 :return: number of readers found 94 86 """ … … 101 93 if not os.path.isdir(temp_path): 102 94 temp_path = os.path.join(os.path.dirname(sys.path[0]), dir) 103 95 104 96 dir = temp_path 105 97 # Check whether the directory exists … … 109 101 logging.warning(msg) 110 102 return readers_found 111 103 112 104 for item in os.listdir(dir): 113 105 full_path = os.path.join(dir, item) 114 106 if os.path.isfile(full_path): 115 107 116 108 # Process python files 117 109 if item.endswith('.py'): … … 126 118 msg += "%s\n %s" % (item, sys.exc_value) 127 119 logging.error(msg) 128 120 129 121 # Process zip files 130 122 elif item.endswith('.zip'): … … 133 125 zfile = ZipFile(item) 134 126 nlist = zfile.namelist() 135 127 136 128 sys.path.insert(0, item) 137 129 for mfile in nlist: … … 141 133 fullname = os.path.splitext(fullname)[0] 142 134 module = __import__(fullname, globals(), 143 135 locals(), [""]) 144 136 if self._identify_plugin(module): 145 137 readers_found += 1 … … 148 140 msg += " %s\n %s" % (mfile, sys.exc_value) 149 141 logging.error(msg) 150 142 151 143 except: 152 144 msg = "Loader: Error importing " 153 145 msg += " %s\n %s" % (item, sys.exc_value) 154 146 logging.error(msg) 155 147 156 148 return readers_found 157 149 158 150 def associate_file_type(self, ext, module): 159 151 """ … … 161 153 Reader class. If so, APPEND it to readers and (potentially) 162 154 to the list of writers for the given extension 163 155 164 156 :param ext: file extension [string] 165 157 :param module: module object 166 158 """ 167 159 reader_found = False 168 160 169 161 if hasattr(module, "Reader"): 170 162 try: … … 177 169 178 170 reader_found = True 179 171 180 172 # Keep track of wildcards 181 173 type_name = module.__name__ 182 174 if hasattr(loader, 'type_name'): 183 175 type_name = loader.type_name 184 176 185 177 wcard = "%s files (*%s)|*%s" % (type_name, ext.lower(), 186 178 ext.lower()) 187 179 if wcard not in self.wildcards: 188 180 self.wildcards.append(wcard) 189 181 190 182 # Check whether writing is supported 191 183 if hasattr(loader, 'write'): … … 194 186 # Append the new writer to the list 195 187 self.writers[ext].append(loader.write) 196 188 197 189 except: 198 190 msg = "Loader: Error accessing" … … 204 196 """ 205 197 Append a reader object to readers 206 198 207 199 :param ext: file extension [string] 208 200 :param module: reader object 209 201 """ 210 202 reader_found = False 211 203 212 204 try: 213 205 # Find supported extensions … … 218 210 219 211 reader_found = True 220 212 221 213 # Keep track of wildcards 222 214 if hasattr(loader, 'type_name'): 223 215 type_name = loader.type_name 224 216 225 217 wcard = "%s files (*%s)|*%s" % (type_name, ext.lower(), 226 218 ext.lower()) 227 219 if wcard not in self.wildcards: 228 220 self.wildcards.append(wcard) 229 221 230 222 except: 231 223 msg = "Loader: Error accessing Reader " … … 236 228 def _identify_plugin(self, module): 237 229 """ 238 Look into a module to find whether it contains a 230 Look into a module to find whether it contains a 239 231 Reader class. If so, add it to readers and (potentially) 240 232 to the list of writers. 241 233 :param module: module object 242 234 243 235 """ 244 236 reader_found = False 245 237 246 238 if hasattr(module, "Reader"): 247 239 try: … … 256 248 257 249 reader_found = True 258 250 259 251 # Keep track of wildcards 260 252 type_name = module.__name__ … … 262 254 type_name = loader.type_name 263 255 wcard = "%s files (*%s)|*%s" % (type_name, ext.lower(), 264 256 ext.lower()) 265 257 if wcard not in self.wildcards: 266 258 self.wildcards.append(wcard) 267 259 268 260 # Check whether writing is supported 269 261 if hasattr(loader, 'write'): … … 272 264 self.writers[ext] = [] 273 265 self.writers[ext].insert(0, loader.write) 274 266 275 267 except: 276 268 msg = "Loader: Error accessing Reader" … … 282 274 """ 283 275 :return: the loader associated with the file type of path. 284 285 276 :Raises ValueError: if file type is not known. 286 277 """ … … 312 303 Raises ValueError if no writer is available. 313 304 Raises KeyError if format is not available. 314 315 305 May raise a writer-defined exception if writer fails. 316 306 """ … … 327 317 raise # reraises last exception 328 318 329 319 330 320 class Loader(object): 331 321 """ … … 334 324 ## Registry instance 335 325 __registry = Registry() 336 326 337 327 def associate_file_type(self, ext, module): 338 328 """ … … 340 330 Reader class. If so, append it to readers and (potentially) 341 331 to the list of writers for the given extension 342 332 343 333 :param ext: file extension [string] 344 334 :param module: module object … … 349 339 """ 350 340 Append a reader object to readers 351 341 352 342 :param ext: file extension [string] 353 343 :param module: reader object … … 358 348 """ 359 349 Load a file 360 350 361 351 :param file: file name (path) 362 352 :param format: specified format to use (optional) … … 364 354 """ 365 355 return self.__registry.load(file, format) 366 356 367 357 def save(self, file, data, format): 368 358 """ … … 370 360 :param file: file name (path) 371 361 :param data: DataInfo object 372 :param format: format to write the data in 362 :param format: format to write the data in 373 363 """ 374 364 return self.__registry.save(file, data, format) 375 365 376 366 def _get_registry_creation_time(self): 377 367 """ … … 380 370 """ 381 371 return self.__registry._created 382 383 def find_plugins(self, dir ):372 373 def find_plugins(self, directory): 384 374 """ 385 375 Find plugins in a given directory 386 376 387 377 :param dir: directory to look into to find new readers/writers 388 378 """ 389 return self.__registry.find_plugins(dir )390 379 return self.__registry.find_plugins(directory) 380 391 381 def get_wildcards(self): 382 """ 383 Return the list of wildcards 384 """ 392 385 return self.__registry.wildcards
Note: See TracChangeset
for help on using the changeset viewer.