""" linkcheck/Logger.py
Copyright (C) 2000 Bastian Kleineidam
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Every Logger has to implement the following functions:
init(self)
Called once to initialize the Logger. Why do we not use __init__(self)?
Because we initialize the start time in init and __init__ gets not
called at the time the checking starts but when the logger object is
created.
newUrl(self,urlData)
Called every time an url finished checking. All data we checked is in
the UrlData object urlData.
endOfOutput(self)
Called at the end of checking to close filehandles and such.
Passing parameters to the constructor:
__init__(self, **args)
The args dictionary is filled in Config.py. There you can specify
default parameters. Adjust these parameters in the configuration
files in the appropriate logger section.
"""
import sys,time,string
import Config, StringUtil
import linkcheck
_ = linkcheck._
# keywords
KeyWords = ["Real URL",
"Result",
"Base",
"Name",
"Parent URL",
"Info",
"Warning",
"D/L Time",
"Check Time",
"URL",
]
MaxIndent = max(map(lambda x: len(_(x)), KeyWords))+1
Spaces = {}
for key in KeyWords:
Spaces[key] = " "*(MaxIndent - len(_(key)))
EntityTable = {
'<': '<',
'>': '>',
'&': '&',
'"': '"',
'\'': ''',
}
def quote(s):
res = list(s)
for i in range(len(res)):
c = res[i]
res[i] = EntityTable.get(c, c)
return string.joinfields(res, '')
# return formatted time
def _strtime(t):
return time.strftime("%d.%m.%Y %H:%M:%S", time.localtime(t))
class StandardLogger:
"""Standard text logger.
Informal text output format spec:
Output consists of a set of URL logs separated by one or more
blank lines.
A URL log consists of two or more lines. Each line consists of
keyword and data, separated by whitespace.
Unknown keywords will be ignored.
"""
def __init__(self, **args):
self.errors = 0
self.warnings = 0
if args.has_key('fileoutput'):
self.fd = open(args['filename'], "w")
elif args.has_key('fd'):
self.fd = args['fd']
else:
self.fd = sys.stdout
def init(self):
self.starttime = time.time()
self.fd.write("%s\n%s\n" % (Config.AppInfo, Config.Freeware))
self.fd.write(_("Get the newest version at %s\n") % Config.Url)
self.fd.write(_("Write comments and bugs to %s\n\n") % Config.Email)
self.fd.write(_("Start checking at %s\n") % _strtime(self.starttime))
self.fd.flush()
def newUrl(self, urlData):
self.fd.write("\n"+_("URL")+Spaces["URL"]+urlData.urlName)
if urlData.cached:
self.fd.write(_(" (cached)\n"))
else:
self.fd.write("\n")
if urlData.name:
self.fd.write(_("Name")+Spaces["Name"]+urlData.name+"\n")
if urlData.parentName:
self.fd.write(_("Parent URL")+Spaces["Parent URL"]+
urlData.parentName+_(", line ")+
str(urlData.line)+"\n")
if urlData.baseRef:
self.fd.write(_("Base")+Spaces["Base"]+urlData.baseRef+"\n")
if urlData.url:
self.fd.write(_("Real URL")+Spaces["Real URL"]+urlData.url+"\n")
if urlData.downloadtime:
self.fd.write(_("D/L Time")+Spaces["D/L Time"]+
_("%.3f seconds\n") % urlData.downloadtime)
if urlData.checktime:
self.fd.write(_("Check Time")+Spaces["Check Time"]+
_("%.3f seconds\n") % urlData.checktime)
if urlData.infoString:
self.fd.write(_("Info")+Spaces["Info"]+
StringUtil.indent(
StringUtil.blocktext(urlData.infoString, 65),
MaxIndent)+"\n")
if urlData.warningString:
self.warnings = self.warnings+1
self.fd.write(_("Warning")+Spaces["Warning"]+
StringUtil.indent(
StringUtil.blocktext(urlData.warningString, 65),
MaxIndent)+"\n")
self.fd.write(_("Result")+Spaces["Result"])
if urlData.valid:
self.fd.write(urlData.validString+"\n")
else:
self.errors = self.errors+1
self.fd.write(urlData.errorString+"\n")
self.fd.flush()
def endOfOutput(self, linknumber=-1):
self.fd.write(_("\nThats it. "))
if self.warnings==1:
self.fd.write(_("1 warning, "))
else:
self.fd.write(str(self.warnings)+_(" warnings, "))
if self.errors==1:
self.fd.write(_("1 error"))
else:
self.fd.write(str(self.errors)+_(" errors"))
if linknumber >= 0:
if linknumber == 1:
self.fd.write(_(" in 1 link"))
else:
self.fd.write(_(" in %d links") % linknumber)
self.fd.write(_(" found\n"))
self.stoptime = time.time()
duration = self.stoptime - self.starttime
name = _("seconds")
self.fd.write(_("Stopped checking at %s") % _strtime(self.stoptime))
if duration > 60:
duration = duration / 60
name = _("minutes")
if duration > 60:
duration = duration / 60
name = _("hours")
self.fd.write(" (%.3f %s)\n" % (duration, name))
self.fd.flush()
self.fd = None
class HtmlLogger(StandardLogger):
"""Logger with HTML output"""
def __init__(self, **args):
apply(StandardLogger.__init__, (self,), args)
self.colorbackground = args['colorbackground']
self.colorurl = args['colorurl']
self.colorborder = args['colorborder']
self.colorlink = args['colorlink']
self.tablewarning = args['tablewarning']
self.tableerror = args['tableerror']
self.tableok = args['tableok']
def init(self):
self.starttime = time.time()
self.fd.write('\n
'+Config.App+"\n"
'\n\n"+
""+
"
"+Config.App+"
"+
"
"+Config.Freeware+"
"+
(_("Start checking at %s\n") % _strtime(self.starttime))+
"