Changeset 84545dd in sasview
- Timestamp:
- Sep 3, 2008 4:33:48 PM (16 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:
- c87d55a
- Parents:
- d831626
- Location:
- DataLoader
- Files:
-
- 1 deleted
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
DataLoader/loader.py
rdaa56d0 r84545dd 18 18 import sys 19 19 import logging 20 import imp21 20 import time 21 from zipfile import ZipFile 22 22 23 23 class Registry(ExtensionRegistry): … … 57 57 for item in os.listdir(dir): 58 58 full_path = os.path.join(dir, item) 59 if os.path.isfile(full_path) and item.endswith('.py'): 60 toks = os.path.splitext(os.path.basename(item)) 61 name = toks[0] 62 path = [os.path.abspath(dir)] 63 file = None 64 try: 65 (file, path, info) = imp.find_module(name, path) 66 module = imp.load_module( name, file, item, info ) 67 if hasattr(module, "Reader"): 68 try: 69 # Find supported extensions 70 loader = module.Reader() 71 for ext in loader.ext: 72 if ext not in self.loaders: 73 self.loaders[ext] = [] 74 self.loaders[ext].insert(0,loader.read) 59 if os.path.isfile(full_path): 60 61 # Process python files 62 if item.endswith('.py'): 63 toks = os.path.splitext(os.path.basename(item)) 64 try: 65 sys.path.insert(0, os.path.abspath(dir)) 66 module = __import__(toks[0], globals(), locals()) 67 if self._identify_plugin(module): 75 68 readers_found += 1 69 except : 70 logging.error("Loader: Error importing %s\n %s" % (name, sys.exc_value)) 76 71 77 # Check whether writing is supported 78 if hasattr(loader, 'write'): 79 for ext in loader.ext: 80 if ext not in self.writers: 81 self.writers[ext] = [] 82 self.writers[ext].insert(0,loader.write) 83 except: 84 logging.error("Loader: Error accessing Reader in %s\n %s" % (name, sys.exc_value)) 85 except : 86 logging.error("Loader: Error importing %s\n %s" % (name, sys.exc_value)) 87 finally: 88 if not file==None: 89 file.close() 72 # Process zip files 73 elif item.endswith('.zip'): 74 try: 75 # Find the modules in the zip file 76 zfile = ZipFile(item) 77 nlist = zfile.namelist() 78 79 sys.path.insert(0, item) 80 for mfile in nlist: 81 try: 82 # Change OS path to python path 83 fullname = mfile.replace('/', '.') 84 fullname = os.path.splitext(fullname)[0] 85 module = __import__(fullname, globals(), locals(), [""]) 86 if self._identify_plugin(module): 87 readers_found += 1 88 except: 89 logging.error("Loader: Error importing %s\n %s" % (mfile, sys.exc_value)) 90 91 except: 92 logging.error("Loader: Error importing %s\n %s" % (item, sys.exc_value)) 93 90 94 return readers_found 95 96 def _identify_plugin(self, module): 97 """ 98 Look into a module to find whether it contains a 99 Reader class. If so, add it to readers and (potentially) 100 to the list of writers. 101 @param module: module object 102 """ 103 reader_found = False 104 105 if hasattr(module, "Reader"): 106 try: 107 # Find supported extensions 108 loader = module.Reader() 109 for ext in loader.ext: 110 if ext not in self.loaders: 111 self.loaders[ext] = [] 112 self.loaders[ext].insert(0,loader.read) 113 reader_found = True 114 115 # Check whether writing is supported 116 if hasattr(loader, 'write'): 117 for ext in loader.ext: 118 if ext not in self.writers: 119 self.writers[ext] = [] 120 self.writers[ext].insert(0,loader.write) 121 except: 122 logging.error("Loader: Error accessing Reader in %s\n %s" % (name, sys.exc_value)) 123 return reader_found 91 124 92 125 def lookup_writers(self, path): -
DataLoader/test/testplugings.py
rdaa56d0 r84545dd 34 34 self.assertTrue(self.L.loaders.has_key('.test')) 35 35 36 class testZip(unittest.TestCase): 37 38 def setUp(self): 39 self.L=Registry() 40 41 # Create module 42 import zipfile 43 z = zipfile.PyZipFile("plugins.zip", 'w') 44 z.writepy("../plugins", "") 45 z.close() 46 47 def testplugin_checksetup(self): 48 """ 49 Check that the test is valid by confirming 50 that the file can't be loaded without the 51 plugins 52 """ 53 self.assertRaises(ValueError, self.L.load, 'test_data.test') 54 55 def testplugin(self): 56 """ 57 test loading with a test reader only 58 found in the plugins directory 59 """ 60 self.L.find_plugins('.') 61 output = self.L.load('test_data.test') 62 self.assertEqual(output.x[0], 1234.) 63 self.assertTrue(self.L.loaders.has_key('.test')) 64 65 36 66 if __name__ == '__main__': 37 67 unittest.main()
Note: See TracChangeset
for help on using the changeset viewer.