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):
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=path.join(name,time)
22 if region!=None:
23 self.name=path.join(self.name,region)
24
25 if path.exists(self.name):
26 if not path.isdir(self.name):
27 error(self.name,"is not a directory")
28 elif create:
29 makedirs(self.name)
30 else:
31 error(self.name,"does not exist")
32
33 self.values=[]
34
35 self.lastReread=0L
36 self.reread()
37
39 """The name of the directory"""
40 return path.basename(self.name)
41
43 """Scan the directory for files with valid names"""
44
45 if not force and stat(self.name)[ST_CTIME]<=self.lastReread:
46 return
47
48 self.values=[]
49
50 ex=["*~",".svn"]
51
52 for f in listdir(self.name):
53 matched=False
54 for e in ex:
55 if fnmatch(f,e):
56 matched=True
57
58 if path.isdir(path.join(self.name,f)):
59 continue
60
61 if not matched:
62 nm=f
63 if len(nm)>3:
64 if nm[-3:]==".gz":
65 nm=nm[:-3]
66 if nm not in self.values:
67 self.values.append(nm)
68 else:
69 error(nm," already found, propably exists as zipped and unzipped")
70
71 self.values.sort()
72
73 self.lastReread=stat(self.name)[ST_CTIME]
74
76 """Get a list of the solution files in that directory"""
77
78 return self.values
79
83
87
89 self.reread()
90 if type(key)!=str:
91 raise TypeError(type(key),"of",key,"is not 'str'")
92
93 if key not in self.values:
94 raise KeyError(key)
95 else:
96 return SolutionFile(self.name,key)
97
99 f=path.join(self.name,key)
100 if path.exists(f):
101 system("rm -f "+f)
102 elif path.exists(f+".gz"):
103 system("rm -f "+f+".gz")
104 else:
105 error("Problem:",key,"(",f,") is supposed to exists, but no file found")
106 self.values.remove(key)
107
109 self.reread()
110 if key in self.values:
111 self.__remove(key)
112 else:
113 raise KeyError(key)
114
115 self.reread(force=True)
116
131
136
138 """Wipe the directory clean"""
139
140 for v in self.values:
141 nm=path.join(self.name,v)
142 system("rm -f "+nm+" "+nm+".gz")
143
144 self.reread(force=True)
145
146 - def copy(self,orig,purge=False,overwrite=True,mustExist=False,exclude=[],include=['*']):
147 """Copy SolutionFiles from another TimeDirectory to the
148 current TimeDirectory. Returns a list with the copied values
149 @param orig: the TimeDirectory with the original files
150 @param purge: remove all current files in this directory
151 @param overwrite: if the file already exists it is overwritten
152 @param mustExist: only if the file already exists it is overwritten
153 @param exclude: List of fnmatch-patterns that should be excluded
154 (Default: none)
155 @param include: List of fnmatch-patterns that should be included
156 (Default: all)"""
157
158 if not overwrite and mustExist:
159 warning("The options mustExist needs the option overwrite")
160 overwrite=True
161
162 if type(orig)!=TimeDirectory:
163 raise TypeError(type(value),"is not TimeDirectory")
164
165 if purge:
166 self.clear()
167
168 copied=[]
169
170 for v in orig:
171 nm=v.baseName()
172
173 doIt=False
174
175 for p in include:
176 if fnmatch(nm,p):
177 doIt=True
178
179 for p in exclude:
180 if fnmatch(nm,p):
181 doIt=False
182
183 if not overwrite and nm in self:
184 doIt=False
185
186 if mustExist and nm not in self:
187 doIt=False
188
189 if doIt:
190 copied.append(nm)
191 self[nm]=v
192
193 return copied
194