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 yieldParsedFiles=False):
24 """@param name: name of the case directory
25 @param time: time in the directory
26 @param create: Create the directory if it does not exist
27 @param region: The mesh region for multi-region cases
28 @param yieldParsedFiles: let the iterator return PasedParameterFile objects instead of SolutionFile"""
29
30 self.name=name
31 self.yieldParsedFiles=yieldParsedFiles
32 if processor!=None:
33 if type(processor)==int:
34 processor="processor%d" % processor
35 self.name=path.join(self.name,processor)
36 self.name=path.join(self.name,time)
37 if region!=None:
38 self.name=path.join(self.name,region)
39
40 if path.exists(self.name):
41 if not path.isdir(self.name):
42 error(self.name,"is not a directory")
43 elif create:
44 makedirs(self.name)
45 else:
46 error(self.name,"does not exist")
47
48 self.values=[]
49
50 self.lastReread=0
51 self.reread()
52
54 """The name of the directory"""
55 return path.basename(self.name)
56
58 """Scan the directory for files with valid names"""
59
60 if not force and stat(self.name)[ST_CTIME]<=self.lastReread:
61 return
62
63 self.values=[]
64
65 ex=["*~",".svn"]
66
67 for f in listdir(self.name):
68 matched=False
69 for e in ex:
70 if fnmatch(f,e):
71 matched=True
72
73 if path.isdir(path.join(self.name,f)):
74 continue
75
76 if not matched:
77 nm=f
78 if len(nm)>3:
79 if nm[-3:]==".gz":
80 nm=nm[:-3]
81 if nm not in self.values:
82 self.values.append(nm)
83 else:
84 error(nm," already found, propably exists as zipped and unzipped")
85
86 self.values.sort()
87
88 self.lastReread=stat(self.name)[ST_CTIME]
89
91 """Get a list of the solution files in that directory"""
92
93 return self.values
94
98
102
104 self.reread()
105 if type(key)!=str:
106 raise TypeError(type(key),"of",key,"is not 'str'")
107
108 if key not in self.values:
109 raise KeyError(key)
110 else:
111 return SolutionFile(self.name,key)
112
122
124 self.reread()
125 if key in self.values:
126 self.__remove(key)
127 else:
128 raise KeyError(key)
129
130 self.reread(force=True)
131
146
154
164
165 - def copy(self,orig,purge=False,overwrite=True,mustExist=False,exclude=[],include=['*']):
166 """Copy SolutionFiles from another TimeDirectory to the
167 current TimeDirectory. Returns a list with the copied values
168 @param orig: the TimeDirectory with the original files
169 @param purge: remove all current files in this directory
170 @param overwrite: if the file already exists it is overwritten
171 @param mustExist: only if the file already exists it is overwritten
172 @param exclude: List of fnmatch-patterns that should be excluded
173 (Default: none)
174 @param include: List of fnmatch-patterns that should be included
175 (Default: all)"""
176
177 if not overwrite and mustExist:
178 warning("The options mustExist needs the option overwrite")
179 overwrite=True
180
181 if type(orig)!=TimeDirectory:
182 raise TypeError(type(value),"is not TimeDirectory")
183
184 if purge:
185 self.clear()
186
187 copied=[]
188
189 for v in orig:
190 nm=v.baseName()
191
192 doIt=False
193
194 for p in include:
195 if fnmatch(nm,p):
196 doIt=True
197
198 for p in exclude:
199 if fnmatch(nm,p):
200 doIt=False
201
202 if not overwrite and nm in self:
203 doIt=False
204
205 if mustExist and nm not in self:
206 doIt=False
207
208 if doIt:
209 copied.append(nm)
210 self[nm]=v
211
212 return copied
213
214
215