1
2 """Working with direcotries from a time-step"""
3
4 from PyFoam.RunDictionary.SolutionFile import SolutionFile
5 from PyFoam.RunDictionary.ParsedParameterFile import ParsedParameterFile
6 from PyFoam.RunDictionary.FileBasis import FileBasis
7 from PyFoam.Error import error,warning
8 from PyFoam.Basics.Utilities import remove
9
10 from os import listdir,stat,path,makedirs
11 from stat import ST_CTIME
12 from fnmatch import fnmatch
13
15 """Represents a directory for a timestep"""
16
17 - def __init__(self,
18 name,
19 time,
20 create=False,
21 region=None,
22 processor=None,
23 tolerant=False,
24 yieldParsedFiles=False):
25 """@param name: name of the case directory
26 @param time: time in the directory
27 @param create: Create the directory if it does not exist
28 @param tolerant: Do not fail if there are inconsistencies
29 @param region: The mesh region for multi-region cases
30 @param yieldParsedFiles: let the iterator return PasedParameterFile objects instead of SolutionFile"""
31
32 self.name=name
33 self.yieldParsedFiles=yieldParsedFiles
34 if processor!=None:
35 if type(processor)==int:
36 processor="processor%d" % processor
37 self.name=path.join(self.name,processor)
38 self.name=path.join(self.name,time)
39 if region!=None:
40 self.name=path.join(self.name,region)
41
42 if path.exists(self.name):
43 if not path.isdir(self.name):
44 error(self.name,"is not a directory")
45 elif create:
46 makedirs(self.name)
47 else:
48 error(self.name,"does not exist")
49
50 self.values=[]
51
52 self.lastReread=0
53 self.tolerant=tolerant
54 self.reread()
55
57 """The name of the directory"""
58 return path.basename(self.name)
59
61 """Scan the directory for files with valid names"""
62
63 if not force and stat(self.name)[ST_CTIME]<=self.lastReread:
64 return
65
66 self.values=[]
67
68 ex=["*~",".svn"]
69
70 for f in listdir(self.name):
71 matched=False
72 for e in ex:
73 if fnmatch(f,e):
74 matched=True
75
76 if path.isdir(path.join(self.name,f)):
77 continue
78
79 if not matched:
80 nm=f
81 if len(nm)>3:
82 if nm[-3:]==".gz":
83 nm=nm[:-3]
84 if nm not in self.values:
85 self.values.append(nm)
86 else:
87 if not self.tolerant:
88 error(nm," already found, propably exists as zipped and unzipped")
89 else:
90 warning(nm," already found, propably exists as zipped and unzipped")
91
92 self.values.sort()
93
94 self.lastReread=stat(self.name)[ST_CTIME]
95
97 """Get a list of the solution files in that directory"""
98
99 return self.values
100
104
108
110 self.reread()
111 if type(key)!=str:
112 raise TypeError(type(key),"of",key,"is not 'str'")
113
114 if key not in self.values:
115 raise KeyError(key)
116 else:
117 return SolutionFile(self.name,key)
118
128
130 self.reread()
131 if key in self.values:
132 self.__remove(key)
133 else:
134 raise KeyError(key)
135
136 self.reread(force=True)
137
152
160
170
171 - def copy(self,orig,purge=False,overwrite=True,mustExist=False,exclude=[],include=['*']):
172 """Copy SolutionFiles from another TimeDirectory to the
173 current TimeDirectory. Returns a list with the copied values
174 @param orig: the TimeDirectory with the original files
175 @param purge: remove all current files in this directory
176 @param overwrite: if the file already exists it is overwritten
177 @param mustExist: only if the file already exists it is overwritten
178 @param exclude: List of fnmatch-patterns that should be excluded
179 (Default: none)
180 @param include: List of fnmatch-patterns that should be included
181 (Default: all)"""
182
183 if not overwrite and mustExist:
184 warning("The options mustExist needs the option overwrite")
185 overwrite=True
186
187 if type(orig)!=TimeDirectory:
188 raise TypeError(type(value),"is not TimeDirectory")
189
190 if purge:
191 self.clear()
192
193 copied=[]
194
195 for v in orig:
196 nm=v.baseName()
197
198 doIt=False
199
200 for p in include:
201 if fnmatch(nm,p):
202 doIt=True
203
204 for p in exclude:
205 if fnmatch(nm,p):
206 doIt=False
207
208 if not overwrite and nm in self:
209 doIt=False
210
211 if mustExist and nm not in self:
212 doIt=False
213
214 if doIt:
215 copied.append(nm)
216 self[nm]=v
217
218 return copied
219
220
221