1
2
3 """
4 General class for file handling.
5
6 @author: E. Sutorius
7 @org: WFAU, IfA, University of Edinburgh
8 """
9
10 import os
11 import cPickle
12
13
15 """general File class"""
17 """Split name into parts, eg.:
18 /diskNN/wsa/ingest/fits/20050101_v0/w20050101_01234_sf_st_cat.fits
19 { path }{ base }
20 { topdir }{ subdir }{ root }{ext}
21 { diskdir }{ common }{ fileID (w/o cat) }
22 { sdate }{runno}{ ftype }
23 """
24 self.name = fname
25 self.path, self.base = os.path.split(self.name)
26 self.root, self.ext = os.path.splitext(self.base)
27 self.topdir, self.subdir = os.path.split(self.path)
28 self.diskdir = '/'.join(self.topdir.split('/', 3)[:-1])
29 self.commondir = self.topdir.split('/', 3)[-1]
30 self.sdate, self.runno, self.ftype = (self.root.split('_', 2) + \
31 ['', '', ''])[:3]
32 self.fileID = os.path.join(
33 self.subdir, self.root.replace("_cat", '').replace("_fix", ''))
34
35 self.fobj = None
36
38 """test for its existence"""
39 return os.path.exists(self.name)
40
42 """get the size"""
43 return os.path.getsize(self.name)
44
46 """open in append mode"""
47 self.fobj = open(self.name,'a')
48
50 """open for reading"""
51 self.fobj = open(self.name,'r')
52
54 """open for writing"""
55 self.fobj = open(self.name,'w')
56
61
63 """close the file object"""
64 self.fobj.close()
65
67 """test if the file is closed"""
68 return self.fobj.closed
69
71 """change the mode of the file"""
72 os.chmod(self.name, mode)
73
74 - def readlines(self, strip=True, commentChar=None, omitEmptyLines=False,
75 findValues=[], omitValues=[]):
76 """read all lines and strip the linebreaks"""
77 lines = self.fobj.readlines()
78 if strip:
79 lines = [l.rstrip() for l in lines]
80 if commentChar:
81 lines = [l for l in lines if not l.startswith(commentChar)][:]
82 if omitEmptyLines:
83 lines = [l for l in lines if len(l)>0][:]
84 if findValues:
85 lines = [l for l in lines if any(v in l for v in findValues)]
86 if omitValues:
87 lines = [l for l in lines if all(v not in l for v in omitValues)]
88 return lines
89
91 """read one line (including linebreak)"""
92 line = self.fobj.readline()
93 return line.rstrip() if strip else line
94
96 """write all lines in given list, appending linebreaks"""
97 for l in lines:
98 self.fobj.write(l + '\n')
99
101 """write given line, no linebreak appended"""
102 self.fobj.write(the_line)
103
105 """write given line with linebreak"""
106 self.fobj.write(the_line + '\n')
107
108 - def copyFile(self, outfile, the_line=""):
109 """copy file linewise; up to line 'the_line' if given"""
110 f1 = open(self.name,'r')
111 f2 = open(outfile,'w')
112 if the_line == "":
113 while 1:
114 ll = f1.readline()
115 if ll == "":
116 break
117 f2.write(ll)
118 else:
119 while 1:
120 ll = f1.readline()
121 if ll == "":
122 break
123 elif ll.find(the_line) > 0:
124 break
125 f2.write(ll)
126
127 f1.close()
128 f2.close()
129
130
131
133 """Class for browser and monitor page files.
134 """
137
139 """Write the standard header info into the file.
140 """
141 if type(cssStyleFiles) != list:
142 cssStyleFiles = [cssStyleFiles]
143
144 headerLines = [
145 '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"',
146 ' "http://www.w3.org/TR/REC-html40/loose.dtd">',
147 '<HTML>',
148 '<HEAD>']
149
150 for style in cssStyleFiles:
151 if style.endswith(".css"):
152 headerLines.append(
153 '<link href="%s" rel="stylesheet" type="text/css">' % style)
154 else:
155 headerLines.append('<style>%s</style>' % style)
156
157 if baseUrl:
158 headerLines += [
159 '<link rel="icon" href="%sfavicon.ico" '
160 'type="image/x-icon">' % baseUrl,
161 '<link rel="shortcut icon" href="%sfavicon.ico" '
162 'type="image/x-icon">\n' % baseUrl]
163
164 headerLines += [
165 '<title>%s</title>' % title,
166 '<META NAME="Keywords" CONTENT="">',
167 '</HEAD>',
168 '<body bgcolor=#000000 color=#ffffff>']
169
170 self.writelines(headerLines)
171
172
173
175 """Write the monitor page timestamp.
176 """
177 self.writetheline(
178 "<TABLE border=0 bgcolor=#000000 width=100% cellspacing=0 cellpadding=0>\n"
179 "<tr><td align='left'>go to "
180 "<a class='bar' href='../../monitor.html' title='progress monitor'>"
181 "<b>Monitoring home page</b></a></td><tr><tr><td align='center'>"
182 "<h1>" + db + " - Semester " + semester + " Monitoring</h1><td></tr>"
183 "<tr><td class='med' align='center'>"
184 "generated: " + timestamp + " UT (" + os.getenv('HOST') + ")"
185 "</td></tr>\n"
186 "</TABLE>")
187
188
189
194
195
196
198 """Logfile holding the information to ingest csv/binary data and to
199 update the curation histories accordingly.
200 """
202 """
203 @param fileName: Name of the pickle data file.
204 @type fileName: str
205 """
206 super(PickleFile, self).__init__(fileName)
207
208
209
211 """
212 @param objects: List of objects to be written to the file.
213 @type objects: Python objects
214 """
215 ofh = open(self.name, 'a')
216 for obj in objects:
217 cPickle.dump(obj, ofh)
218 ofh.close()
219
220
221
223 """Read all objects from the file.
224 """
225 self.lines = []
226 ifh = open(self.name, 'r')
227 while True:
228 try:
229 yield cPickle.load(ifh)
230 except EOFError:
231 break
232 ifh.close()
233
234
235
237 """
238 @param objects: List of objects to be written to the file.
239 @type objects: Python objects
240 """
241 ofh = open(self.name, 'w')
242 for obj in objects:
243 cPickle.dump(obj, ofh)
244 ofh.close()
245
246
247
248
249
250
251
252
253
254
255
256
257
258