Package wsatools :: Module File
[hide private]

Source Code for Module wsatools.File

  1  #------------------------------------------------------------------------------ 
  2  #$Id: File.py 10093 2013-10-03 13:17:48Z EckhardSutorius $ 
  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   
14 -class File(object):
15 """general File class"""
16 - def __init__(self, fname):
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
37 - def exists(self):
38 """test for its existence""" 39 return os.path.exists(self.name)
40
41 - def getsize(self):
42 """get the size""" 43 return os.path.getsize(self.name)
44
45 - def aopen(self):
46 """open in append mode""" 47 self.fobj = open(self.name,'a')
48
49 - def ropen(self):
50 """open for reading""" 51 self.fobj = open(self.name,'r')
52
53 - def wopen(self):
54 """open for writing""" 55 self.fobj = open(self.name,'w')
56
57 - def remove(self):
58 """remove file""" 59 if os.path.exists(self.name): 60 os.remove(self.name)
61
62 - def close(self):
63 """close the file object""" 64 self.fobj.close()
65
66 - def isClosed(self):
67 """test if the file is closed""" 68 return self.fobj.closed
69
70 - def chmod(self, mode):
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
90 - def readline(self, strip=False):
91 """read one line (including linebreak)""" 92 line = self.fobj.readline() 93 return line.rstrip() if strip else line
94
95 - def writelines(self, lines):
96 """write all lines in given list, appending linebreaks""" 97 for l in lines: 98 self.fobj.write(l + '\n')
99
100 - def writeline(self, the_line):
101 """write given line, no linebreak appended""" 102 self.fobj.write(the_line)
103
104 - def writetheline(self, the_line):
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
132 -class HTMLFile(File):
133 """Class for browser and monitor page files. 134 """
135 - def __init__(self, fileName):
136 super(HTMLFile, self).__init__(fileName)
137
138 - def writeHeader(self, title, cssStyleFiles, baseUrl=None):
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
174 - def writeTimestamp(self, semester, timestamp, db):
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
190 - def writeFoot(self):
191 """Write the genaral footer into the file. 192 """ 193 self.writelines(["</BODY>","</HTML>"])
194 195 #------------------------------------------------------------------------------ 196
197 -class PickleFile(File):
198 """Logfile holding the information to ingest csv/binary data and to 199 update the curation histories accordingly. 200 """
201 - def __init__(self, fileName):
202 """ 203 @param fileName: Name of the pickle data file. 204 @type fileName: str 205 """ 206 super(PickleFile, self).__init__(fileName)
207 208 #-------------------------------------------------------------------------- 209
210 - def pickleAppend(self, *objects):
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
222 - def pickleRead(self):
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
236 - def pickleWrite(self, *objects):
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 # Change log: 248 # 249 # 30-Jan-2007, ETWS: First version. 250 # 23-Feb-2007, ETWS: Included check for closed file. 251 # 3-May-2007, ETWS: Included chmod and getsize functionality. 252 # 16-Nov-2007, ETWS: Included split of path. 253 # 22-Nov-2007, ETWS: Added PickleFile class. 254 # 21-Jan-2007, ETWS: Added missing import statement. 255 # 24-Apr-2008, ETWS: Included comment character in readlines. 256 # 11-Aug-2008, ETWS: Added disk dir and common dir to File variables. 257 # 13-Nov-2008, ETWS: Added HTMLFile class. 258