1
2 """Working with direcotries from a time-step"""
3
4 from SolutionFile import SolutionFile
5 from ParsedParameterFile import ParsedParameterFile
6 from FileBasis import FileBasis
7 from PyFoam.Error import error,warning
8
9 from os import listdir,stat,path,system,makedirs
10 from stat import ST_CTIME
11 from fnmatch import fnmatch
12
14 """Represents a directory for a timestep"""
15
16 - def __init__(self,
17 name,
18 time,
19 create=False,
20 region=None,
21 processor=None,
22 yieldParsedFiles=False):
23 """@param name: name of the case directory
24 @param time: time in the directory
25 @param create: Create the directory if it does not exist
26 @param region: The mesh region for multi-region cases
27 @param yieldParsedFiles: let the iterator return PasedParameterFile objects instead of SolutionFile"""
28
29 self.name=name
30 self.yieldParsedFiles=yieldParsedFiles
31 if processor!=None:
32 if type(processor)==int:
33 processor="processor%d" % processor
34 self.name=path.join(self.name,processor)
35 self.name=path.join(self.name,time)
36 if region!=None:
37 self.name=path.join(self.name,region)
38
39 if path.exists(self.name):
40 if not path.isdir(self.name):
41 error(self.name,"is not a directory")
42 elif create:
43 makedirs(self.name)
44 else:
45 error(self.name,"does not exist")
46
47 self.values=[]
48
49 self.lastReread=0L
50 self.reread()
51
53 """The name of the directory"""
54 return path.basename(self.name)
55
57 """Scan the directory for files with valid names"""
58
59 if not force and stat(self.name)[ST_CTIME]<=self.lastReread:
60 return
61
62 self.values=[]
63
64 ex=["*~",".svn"]
65
66 for f in listdir(self.name):
67 matched=False
68 for e in ex:
69 if fnmatch(f,e):
70 matched=True
71
72 if path.isdir(path.join(self.name,f)):
73 continue
74
75 if not matched:
76 nm=f
77 if len(nm)>3:
78 if nm[-3:]==".gz":
79 nm=nm[:-3]
80 if nm not in self.values:
81 self.values.append(nm)
82 else:
83 error(nm," already found, propably exists as zipped and unzipped")
84
85 self.values.sort()
86
87 self.lastReread=stat(self.name)[ST_CTIME]
88
90 """Get a list of the solution files in that directory"""
91
92 return self.values
93
97
101
103 self.reread()
104 if type(key)!=str:
105 raise TypeError(type(key),"of",key,"is not 'str'")
106
107 if key not in self.values:
108 raise KeyError(key)
109 else:
110 return SolutionFile(self.name,key)
111
113 f=path.join(self.name,key)
114 if path.exists(f):
115 system("rm -f "+f)
116 elif path.exists(f+".gz"):
117 system("rm -f "+f+".gz")
118 else:
119 error("Problem:",key,"(",f,") is supposed to exists, but no file found")
120 self.values.remove(key)
121
123 self.reread()
124 if key in self.values:
125 self.__remove(key)
126 else:
127 raise KeyError(key)
128
129 self.reread(force=True)
130
145
153
155 """Wipe the directory clean"""
156
157 for v in self.values:
158 nm=path.join(self.name,v)
159 system("rm -f "+nm+" "+nm+".gz")
160
161 self.reread(force=True)
162
163 - def copy(self,orig,purge=False,overwrite=True,mustExist=False,exclude=[],include=['*']):
164 """Copy SolutionFiles from another TimeDirectory to the
165 current TimeDirectory. Returns a list with the copied values
166 @param orig: the TimeDirectory with the original files
167 @param purge: remove all current files in this directory
168 @param overwrite: if the file already exists it is overwritten
169 @param mustExist: only if the file already exists it is overwritten
170 @param exclude: List of fnmatch-patterns that should be excluded
171 (Default: none)
172 @param include: List of fnmatch-patterns that should be included
173 (Default: all)"""
174
175 if not overwrite and mustExist:
176 warning("The options mustExist needs the option overwrite")
177 overwrite=True
178
179 if type(orig)!=TimeDirectory:
180 raise TypeError(type(value),"is not TimeDirectory")
181
182 if purge:
183 self.clear()
184
185 copied=[]
186
187 for v in orig:
188 nm=v.baseName()
189
190 doIt=False
191
192 for p in include:
193 if fnmatch(nm,p):
194 doIt=True
195
196 for p in exclude:
197 if fnmatch(nm,p):
198 doIt=False
199
200 if not overwrite and nm in self:
201 doIt=False
202
203 if mustExist and nm not in self:
204 doIt=False
205
206 if doIt:
207 copied.append(nm)
208 self[nm]=v
209
210 return copied
211