Package PyFoam :: Package RunDictionary :: Module TimeDirectory
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.RunDictionary.TimeDirectory

  1  #  ICE Revision: $Id: /local/openfoam/Python/PyFoam/PyFoam/RunDictionary/TimeDirectory.py 6219 2010-03-16T09:05:49.396899Z bgschaid  $  
  2  """Working with direcotries from a time-step""" 
  3   
  4  from SolutionFile import SolutionFile 
  5  from FileBasis import FileBasis 
  6  from PyFoam.Error import error,warning 
  7   
  8  from os import listdir,stat,path,system,makedirs 
  9  from stat import ST_CTIME 
 10  from fnmatch import fnmatch 
 11   
12 -class TimeDirectory(object):
13 """Represents a directory for a timestep""" 14
15 - def __init__(self,name,time,create=False,region=None,processor=None):
16 """@param name: name of the case directory 17 @param time: time in the directory 18 @param create: Create the directory if it does not exist 19 @param region: The mesh region for multi-region cases""" 20 21 self.name=name 22 if processor!=None: 23 if type(processor)==int: 24 processor="processor%d" % processor 25 self.name=path.join(self.name,processor) 26 self.name=path.join(self.name,time) 27 if region!=None: 28 self.name=path.join(self.name,region) 29 30 if path.exists(self.name): 31 if not path.isdir(self.name): 32 error(self.name,"is not a directory") 33 elif create: 34 makedirs(self.name) 35 else: 36 error(self.name,"does not exist") 37 38 self.values=[] 39 40 self.lastReread=0L 41 self.reread()
42
43 - def baseName(self):
44 """The name of the directory""" 45 return path.basename(self.name)
46
47 - def reread(self,force=False):
48 """Scan the directory for files with valid names""" 49 50 if not force and stat(self.name)[ST_CTIME]<=self.lastReread: 51 return 52 53 self.values=[] 54 55 ex=["*~",".svn"] 56 57 for f in listdir(self.name): 58 matched=False 59 for e in ex: 60 if fnmatch(f,e): 61 matched=True 62 63 if path.isdir(path.join(self.name,f)): 64 continue 65 66 if not matched: 67 nm=f 68 if len(nm)>3: 69 if nm[-3:]==".gz": 70 nm=nm[:-3] 71 if nm not in self.values: 72 self.values.append(nm) 73 else: 74 error(nm," already found, propably exists as zipped and unzipped") 75 76 self.values.sort() 77 78 self.lastReread=stat(self.name)[ST_CTIME]
79
80 - def getFiles(self):
81 """Get a list of the solution files in that directory""" 82 83 return self.values
84
85 - def __contains__(self,item):
86 self.reread() 87 return item in self.values
88
89 - def __len__(self):
90 self.reread() 91 return len(self.values)
92
93 - def __getitem__(self,key):
94 self.reread() 95 if type(key)!=str: 96 raise TypeError(type(key),"of",key,"is not 'str'") 97 98 if key not in self.values: 99 raise KeyError(key) 100 else: 101 return SolutionFile(self.name,key)
102
103 - def __remove(self,key):
104 f=path.join(self.name,key) 105 if path.exists(f): 106 system("rm -f "+f) 107 elif path.exists(f+".gz"): 108 system("rm -f "+f+".gz") 109 else: 110 error("Problem:",key,"(",f,") is supposed to exists, but no file found") 111 self.values.remove(key)
112
113 - def __delitem__(self,key):
114 self.reread() 115 if key in self.values: 116 self.__remove(key) 117 else: 118 raise KeyError(key) 119 120 self.reread(force=True)
121
122 - def __setitem__(self,key,value):
123 self.reread() 124 if type(key)!=str: 125 raise TypeError(type(key),"of",key,"is not 'str'") 126 127 if key in self.values: 128 self.__remove(key) 129 130 if FileBasis in value.__class__.__mro__: 131 value.writeFileAs(path.join(self.name,key)) 132 else: 133 f=FileBasis(path.join(self.name,key)) 134 f.writeFile(str(value)) 135 self.reread(force=True)
136
137 - def __iter__(self):
138 self.reread() 139 for key in self.values: 140 yield SolutionFile(self.name,key)
141
142 - def clear(self):
143 """Wipe the directory clean""" 144 145 for v in self.values: 146 nm=path.join(self.name,v) 147 system("rm -f "+nm+" "+nm+".gz") 148 149 self.reread(force=True)
150
151 - def copy(self,orig,purge=False,overwrite=True,mustExist=False,exclude=[],include=['*']):
152 """Copy SolutionFiles from another TimeDirectory to the 153 current TimeDirectory. Returns a list with the copied values 154 @param orig: the TimeDirectory with the original files 155 @param purge: remove all current files in this directory 156 @param overwrite: if the file already exists it is overwritten 157 @param mustExist: only if the file already exists it is overwritten 158 @param exclude: List of fnmatch-patterns that should be excluded 159 (Default: none) 160 @param include: List of fnmatch-patterns that should be included 161 (Default: all)""" 162 163 if not overwrite and mustExist: 164 warning("The options mustExist needs the option overwrite") 165 overwrite=True 166 167 if type(orig)!=TimeDirectory: 168 raise TypeError(type(value),"is not TimeDirectory") 169 170 if purge: 171 self.clear() 172 173 copied=[] 174 175 for v in orig: 176 nm=v.baseName() 177 178 doIt=False 179 180 for p in include: 181 if fnmatch(nm,p): 182 doIt=True 183 184 for p in exclude: 185 if fnmatch(nm,p): 186 doIt=False 187 188 if not overwrite and nm in self: 189 doIt=False 190 191 if mustExist and nm not in self: 192 doIt=False 193 194 if doIt: 195 copied.append(nm) 196 self[nm]=v 197 198 return copied
199