Updating ChangeLog for 4.22.10
[centerim.git] / misc / CenterIMLog2HTML.py
blob01b2d4116792c6ad1497005e9508b6d27b54c9db
1 #!/usr/bin/env python
3 #Copyright (C) 2008 Luar Roji
5 #This program is free software; you can redistribute it and/or
6 #modify it under the terms of the GNU General Public License
7 #as published by the Free Software Foundation; either version 2
8 #of the License, or (at your option) any later version.
10 #This program is distributed in the hope that it will be useful,
11 #but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 #GNU General Public License for more details.
15 #You should have received a copy of the GNU General Public License
16 #along with this program; if not, write to the Free Software
17 #Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #For full license view COPYRIGHT file or go to:
20 #http://www.fsf.org/licensing/licenses/info/GPLv2.html
22 from datetime import *
23 import sys
25 print "CenterIM Log2HTML - $Rev: 25$"
26 print "Luar Roji - http://roji.net\n"
28 def usage():
29 print "Usage: " + sys.argv[0] + " [logFileName] {outFileName}"
30 print ""
31 print " If outFileName is not specified, logFileName+.html is assumed."
32 print ""
33 sys.exit()
35 def getLine(f):
36 s = f.readline()
37 s = s.rstrip('\n\r')
38 return s
40 def out(s):
41 outfile.write(s + "\n")
42 # print s
44 def outtd(msg, field):
45 data = msg[field]
46 if (field=="timestamp1" or field=="timestamp2"):
47 if (data!=""):
48 floatData = float(data)
49 dateTimeObj = datetime.fromtimestamp(floatData)
50 data = dateTimeObj.strftime(DATEFORMAT)
51 out(" <td class=\""+field+"\">"+data+"</td>")
53 msglist = []
54 x=0
56 DATEFORMAT = "%d/%b/%Y %H:%M:%S"
57 now = datetime.now().strftime(DATEFORMAT)
58 HTMLHEADER = "<html>\n" + \
59 " <head>\n" + \
60 " <title>CenterIM Conversation</title>\n" + \
61 " <style type=\"text/css\">\n" + \
62 " tr.imparOUT { background-color: #BAF0FA; }\n" + \
63 " tr.parOUT { background-color: #F0BAFA; }\n" + \
64 " tr.imparIN { background-color: #BAF000; }\n" + \
65 " tr.parIN { background-color: #F0BA00; }\n" + \
66 " td.timestamp1 { width:11%; font-size: 80%; }\n" + \
67 " </style>\n" + \
68 " </head>\n" + \
69 " <!-- Generated by $Id$ on " +now+ " -->\n" + \
70 " <body>\n" + \
71 " <table border=\"1\">"
73 HTMLFOOTER = " </table>\n" + \
74 " </body>\n" + \
75 "</html>\n"
77 if len(sys.argv)<2:
78 usage()
80 infilename = sys.argv[1]
81 if len(sys.argv)>2:
82 outfilename = sys.argv[2]
83 else:
84 outfilename = infilename + ".html"
86 print "Opening input file..",
87 infile = open(infilename,"r")
88 print "Ok"
89 print "Opening output file..",
90 outfile = open(outfilename,"w")
91 print "Ok"
93 print "Processing..."
94 lineCount = 0
95 while infile:
96 s = infile.readline()
97 lineCount = lineCount+1
98 if (lineCount%500==0):
99 print str(lineCount) + " lines readed"
100 if (len(s)==0):
101 break;
102 if (s=="\x0c\n"):
103 msg = {'direction' : getLine(infile),
104 'type' : getLine(infile),
105 'timestamp1' : getLine(infile),
106 'timestamp2' : getLine(infile),
107 'message' : getLine(infile)}
108 msglist.append(msg)
110 print "File reading done. " + str(lineCount) +" lines readed."
111 print "Generating HTML output..",
113 out(HTMLHEADER)
114 for msg in msglist:
115 if (msg["type"]!="MSG"):
116 continue
117 x=x+1
118 if (x%2==0):
119 trClass = "par"
120 else:
121 trClass = "impar"
123 out(" <tr class=\""+trClass+msg["direction"]+"\">")
124 outtd(msg,"timestamp1")
125 # outtd(msg,"timestamp2") -- Don't understand the difference between those
126 outtd(msg,"direction")
127 # outtd(msg,"type") -- Redundant with the above if
128 outtd(msg,"message")
129 out(" </tr>")
131 out(HTMLFOOTER)
132 print "Done."
134 infile.close()
135 outfile.close()