Changeset aa749ac in sasview for DataLoader
- Timestamp:
- Jul 2, 2008 9:30:33 AM (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:
- 70e9610
- Parents:
- cbe6dbe
- Location:
- DataLoader
- Files:
-
- 1 added
- 1 deleted
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
DataLoader/loader.py
rbb03739 raa749ac 1 from register import ExtensionRegistry 1 # This program is public domain 2 """ 3 File extension registry. 2 4 3 #import tiff_reader 4 class Loader: 5 This provides routines for opening files based on extension, 6 and registers the built-in file extensions. 7 """ 8 import logging 9 import os.path 10 11 class Loader(object): 12 """ 13 Associate a file loader with an extension. 14 15 Note that there may be multiple readers for the same extension. 16 17 Example: 18 19 registry = Loader() 20 21 # Add an association by setting an element 22 registry['.zip'] = unzip 23 24 # Multiple extensions for one loader 25 registry['.tgz'] = untar 26 registry['.tar.gz'] = untar 27 28 # Multiple readers for one extension 29 registry['.cx'] = cx1 30 registry['.cx'] = cx2 31 registry['.cx'] = cx3 32 33 # Can also register a format name for explicit control from caller 34 registry['cx3'] = cx3 35 36 # Retrieve readers for a file name 37 registry.lookup('hello.cx') -> [cx3,cx2,cx1] 38 39 # Run loader on a filename 40 registry.load('hello.cx') -> 41 try: 42 return cx3('hello.cx') 43 except: 44 try: 45 return cx2('hello.cx') 46 except: 47 return cx1('hello.cx') 48 49 # Load in a specific format ignoring extension 50 registry.load('hello.cx',format='cx3') -> 51 return cx3('hello.cx') 52 """ 5 53 def __init__(self): 6 """ init method""" 7 self.reg=ExtensionRegistry() 8 9 def setFormat(self,ext,reader): 10 """ 11 Load the available reader with file extensions 12 @param ext:file extension 13 @param reader: instance of a reader 54 self.readers = {} 55 self.reading=None 56 57 58 def __setitem__(self, ext, reader): 59 if ext not in self.readers: 60 self.readers[ext] = [] 61 self.readers[ext].insert(0,reader) 62 63 64 def __getitem__(self, ext): 65 return self.readers[ext] 66 67 68 def __contains__(self, ext): 69 return ext in self.readers 70 71 72 def formats(self, name=True, ext=False): 14 73 """ 15 #self.reg[ext]=reader 16 self.reg.__setitem__(ext, reader) 17 def getReader(self,ext): 18 return self.reg.__getitem__( ext) 74 Return a list of the registered formats. If name=True then 75 named formats are returned. If ext=True then extensions 76 are returned. 77 """ 78 names = [a for a in self.readers.keys() if not a.startswith('.')] 79 exts = [a for a in self.readers.keys() if a.startswith('.')] 80 names.sort() 81 exts.sort() 82 ret = [] 83 if name: ret += names 84 if ext: ret += exts 85 return ret 19 86 87 def lookup(self, path): 88 """ 89 Return the loader associated with the file type of path. 90 """ 91 file = os.path.basename(path) 92 idx = file.find('.') 93 ext = file[idx:] if idx >= 0 else '' 94 try: 95 return self.readers[ext] 96 except: 97 #raise ValueError, "Unknown file type '%s'"%ext 98 print "Unknown file type '%s'"%ext 99 100 101 def getAcTReader(self,path): 102 return self.reading 20 103 21 def loadData(self,path,format=None): 22 """ Read a file and save values""" 23 self.reg.lookup(path) 24 try: 25 return self.reg.load(path,format) 26 except ValueError,msg: 27 print msg 28 def getActiveReader(self,path): 29 return self.reg.getAcTReader(path) 30 def getextension(self,name=False,ext=True): 31 """ return list of readers""" 32 return self.reg.formats(name,ext) 104 def load(self, path, format=None): 105 """ 106 Call the loader for the file type of path. 107 108 Raises ValueError if no loader is available. 109 May raise a loader-defined exception if loader fails. 110 """ 111 if format is None: 112 readers = self.lookup(path) 113 else: 114 readers = self.readers[format] 115 if readers!=None: 116 for fn in readers: 117 try: 118 value=fn.read(path) 119 self.reading= fn.__class__ 120 return value 121 except ValueError,msg: 122 print str(msg) -
DataLoader/readers/TXT2_Reader.py
r11a0319 raa749ac 18 18 """ Store the values loaded from file in local variables """ 19 19 if not path == None: 20 input_f = open(path,'r') 21 buff = input_f.read() 22 lines = buff.split('\n') 23 self.x=[] 24 self.y=[] 25 self.dx = [] 26 self.dy=[] 27 value="can't read" 28 for line in lines: 29 toks = line.split() 30 try: 31 x = float(toks[0]) 32 self.x.append(x) 33 except: 34 print "READ ERROR", line 35 try: 36 y = float(toks[1]) 37 self.y.append(y) 38 except: 39 print "READ ERROR", line 40 try: 41 dy=float(toks[2]) 42 self.dy.append(dy) 43 44 except: 45 print "READ ERROR", line 46 47 if(( self.x==[])or (self.y==[])) or (self.dy !=[]): 48 print "went here" 49 #return value 50 raise ValueError, "Reader1 can't read" 20 read_it = False 21 ext=['.txt','.dat'] 22 for item in ext: 23 if path.lower().find(item)>=0: 24 read_it = True 25 #print "this is the flag",read_it, path.lower() 26 if read_it==False: 27 return None 51 28 else: 52 #msg="R1 reading: \n"+"this x :"+ str(self.x) +"\n"+"this y:"+str(self.y)+"\n"+"this dy :"+str(self.dy)+"\n" 53 #return msg 54 print "TXT2_Reader reading: \n" 55 return self.x,self.y,self.dy 56 29 input_f = open(path,'r') 30 buff = input_f.read() 31 lines = buff.split('\n') 32 self.x=[] 33 self.y=[] 34 self.dx = [] 35 self.dy=[] 36 for line in lines: 37 toks = line.split() 38 try: 39 x = float(toks[0]) 40 self.x.append(x) 41 except: 42 pass 43 #print "READ ERROR", line 44 try: 45 y = float(toks[1]) 46 self.y.append(y) 47 except: 48 pass 49 #print "READ ERROR", line 50 try: 51 dy=float(toks[2]) 52 self.dy.append(dy) 53 54 except: 55 pass 56 #print "READ ERROR", line 57 58 if(( self.x==[])or (self.y==[])) or (self.dy !=[]): 59 #print "went here" 60 #return value 61 raise ValueError, "TXT2_Reader can't read" 62 else: 63 #msg="R1 reading: \n"+"this x :"+ str(self.x) +"\n"+"this y:"+str(self.y)+"\n"+"this dy :"+str(self.dy)+"\n" 64 #return msg 65 print "TXT2_Reader reading \n" 66 return self.x,self.y,self.dy 67 return None 57 68 58 69 -
DataLoader/readers/TXT3_Reader.py
rbb03739 raa749ac 27 27 if path.lower().find(item)>=0: 28 28 read_it = True 29 print "this is the flag",read_it, path.lower()29 #print "this is the flag",read_it, path.lower() 30 30 if read_it==False: 31 r aise ValueError, "txtReader can't read"31 return None 32 32 else: 33 33 input_f = open(path,'r') … … 38 38 self.dx = [] 39 39 self.dy=[] 40 value="can't read"41 40 for line in lines: 42 41 toks = line.split() … … 51 50 52 51 except: 53 print "READ ERROR", line 52 pass 53 #print "READ ERROR", line 54 54 55 55 self.dx = numpy.zeros(len(self.x)) … … 61 61 62 62 if (self.x==[] or self.y==[])and (self.dy==[]): 63 raise ValueError, " txtReader can't read"63 raise ValueError, "TXT3_Reader can't read" 64 64 else: 65 65 #msg="txtReader Reading:\n"+"this x :"+ str(self.x) +"\n"+"this y:"+str(self.y)+"\n"+"this dy :"+str(self.dy)+"\n" … … 68 68 return self.x,self.y,self.dy 69 69 70 70 return None 71 71 if __name__ == "__main__": 72 72 read= Reader()
Note: See TracChangeset
for help on using the changeset viewer.