1
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
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
44 """The name of the directory"""
45 return path.basename(self.name)
46
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
81 """Get a list of the solution files in that directory"""
82
83 return self.values
84
88
92
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
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
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
136
141
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