# Copyright (C) 2000-2014 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., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. """ A HTML logger. """ import html import os import time from . import _Logger from .. import strformat, configuration # ss=1 enables show source validate_html = "http://validator.w3.org/check?ss=1&uri=%(uri)s" # options are the default validate_css = "http://jigsaw.w3.org/css-validator/validator?" \ "uri=%(uri)s&warning=1&profile=css2&usermedium=all" HTML_HEADER = """
"+ configuration.Freeware+"
"+ (_("Start checking at %s") % strformat.strtime(self.starttime))+ os.linesep+"
") self.check_date() self.flush() def log_url(self, url_data): """Write url checking info as HTML.""" self.write_table_start() if self.has_part("url"): self.write_url(url_data) if url_data.name and self.has_part("name"): self.write_name(url_data) if url_data.parent_url and self.has_part("parenturl"): self.write_parent(url_data) if url_data.base_ref and self.has_part("base"): self.write_base(url_data) if url_data.url and self.has_part("realurl"): self.write_real(url_data) if url_data.dltime >= 0 and self.has_part("dltime"): self.write_dltime(url_data) if url_data.size >= 0 and self.has_part("dlsize"): self.write_size(url_data) if url_data.checktime and self.has_part("checktime"): self.write_checktime(url_data) if url_data.info and self.has_part("info"): self.write_info(url_data) if url_data.modified and self.has_part("modified"): self.write_modified(url_data) if url_data.warnings and self.has_part("warning"): self.write_warning(url_data) if self.has_part("result"): self.write_result(url_data) self.write_table_end() self.flush() def write_table_start(self): """Start html table.""" self.writeln('') def write_table_end(self): """End html table.""" self.write('
') def write_id(self): """Write ID for current URL.""" self.writeln("") self.writeln(' " % self.stats.number) def write_url(self, url_data): """Write url_data.base_url.""" self.writeln("%s ' % self.part("id")) self.write("%d ") self.writeln(' ") def write_name(self, url_data): """Write url_data.name.""" args = (self.part("name"), html.escape(url_data.name)) self.writeln("%s ' % self.part("url")) self.write('') self.write("`%s'" % html.escape(url_data.base_url)) self.writeln(" " % args) def write_parent(self, url_data): """Write url_data.parent_url.""" self.write(" %s `%s' ") def write_base(self, url_data): """Write url_data.base_ref.""" self.writeln(" "+self.part("parenturl")+ ' '+ html.escape(url_data.parent_url)+"") if url_data.line is not None: self.write(_(", line %d") % url_data.line) if url_data.column is not None: self.write(_(", col %d") % url_data.column) if url_data.page > 0: self.write(_(", page %d") % url_data.page) if not url_data.valid: # on errors show HTML and CSS validation for parent url vhtml = validate_html % {'uri': url_data.parent_url} vcss = validate_css % {'uri': url_data.parent_url} self.writeln() self.writeln('(HTML)') self.write('(CSS)') self.writeln(" ") def write_real(self, url_data): """Write url_data.url.""" self.writeln(" "+self.part("base")+" "+ html.escape(url_data.base_ref)+" ") def write_dltime(self, url_data): """Write url_data.dltime.""" self.writeln(" "+self.part("realurl")+" "+ ''+html.escape(url_data.url)+" ") def write_size(self, url_data): """Write url_data.size.""" self.writeln(" "+self.part("dltime")+" "+ (_("%.3f seconds") % url_data.dltime)+ " ") def write_checktime(self, url_data): """Write url_data.checktime.""" self.writeln(" "+self.part("dlsize")+" "+ strformat.strsize(url_data.size)+ " ") def write_info(self, url_data): """Write url_data.info.""" sep = " "+self.part("checktime")+" "+ (_("%.3f seconds") % url_data.checktime)+"
"+os.linesep text = sep.join(html.escape(x) for x in url_data.info) self.writeln('") def write_modified(self, url_data): """Write url_data.modified.""" text = html.escape(self.format_modified(url_data.modified)) self.writeln(' ' + self.part("info")+ " "+text+" ") def write_warning(self, url_data): """Write url_data.warnings.""" sep = " ' + self.part("modified") + " "+text+"
"+os.linesep text = sep.join(html.escape(x[1]) for x in url_data.warnings) self.writeln('") def write_result(self, url_data): """Write url_data.result.""" if url_data.valid: self.write(' ' + self.part("warning") + ' ' + text + " ') self.write(self.part("result")) self.write(' ') self.write(html.escape(_("Valid"))) else: self.write(' ") def write_stats(self): """Write check statistic infos.""" self.writeln(' ') self.write(self.part("result")) self.write(' ') self.write(html.escape(_("Error"))) if url_data.result: self.write(": "+html.escape(url_data.result)) self.writeln("
%s
' % _("Statistics")) if self.stats.number > 0: self.writeln(_( "Content types: %(image)d image, %(text)d text, %(video)d video, " "%(audio)d audio, %(application)d application, %(mail)d mail" " and %(other)d other.") % self.stats.link_types) self.writeln("
") self.writeln(_("URL lengths: min=%(min)d, max=%(max)d, avg=%(avg)d.") % dict(min=self.stats.min_url_length, max=self.stats.max_url_length, avg=self.stats.avg_url_length)) else: self.writeln(_("No statistics available since no URLs were checked.")) self.writeln("
") def write_outro(self): """Write end of check message.""" self.writeln("
") self.write(_("That's it.")+" ") if self.stats.number >= 0: self.write(_n("%d link checked.", "%d links checked.", self.stats.number) % self.stats.number) self.write(" ") self.write(_n("%d warning found", "%d warnings found", self.stats.warnings_printed) % self.stats.warnings_printed) if self.stats.warnings != self.stats.warnings_printed: self.write(_(" (%d ignored or duplicates not printed)") % (self.stats.warnings - self.stats.warnings_printed)) self.write(". ") self.write(_n("%d error found", "%d errors found", self.stats.errors_printed) % self.stats.errors_printed) if self.stats.errors != self.stats.errors_printed: self.write(_(" (%d duplicates not printed)") % (self.stats.errors - self.stats.errors_printed)) self.writeln(".") self.writeln("
") num = self.stats.internal_errors if num: self.write(_n("There was %(num)d internal error.", "There were %(num)d internal errors.", num) % {"num": num}) self.writeln("
") self.stoptime = time.time() duration = self.stoptime - self.starttime self.writeln(_("Stopped checking at %(time)s (%(duration)s)") % {"time": strformat.strtime(self.stoptime), "duration": strformat.strduration_long(duration)}) self.writeln('