1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
# -*- coding: iso-8859-1 -*-
# Copyright (C) 2000-2009 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 CSV logger.
"""
import time
import csv
import os
from . import Logger
from .. import strformat, configuration
class CSVLogger (Logger):
"""
CSV output, consisting of one line per entry. Entries are
separated by a semicolon.
"""
def __init__ (self, **args):
"""
Store default separator and (os dependent) line terminator.
"""
super(CSVLogger, self).__init__(**args)
self.init_fileoutput(args)
self.separator = args['separator']
self.quotechar = args['quotechar']
def comment (self, s, **args):
"""
Write CSV comment.
"""
self.write(u"# ")
self.writeln(s=s, **args)
def start_output (self):
"""
Write checking start info as csv comment.
"""
super(CSVLogger, self).start_output()
self.starttime = time.time()
row = []
if self.has_part("intro"):
self.comment(_("created by %(app)s at %(time)s") %
{"app": configuration.AppName,
"time": strformat.strtime(self.starttime)})
self.comment(_("Get the newest version at %(url)s") %
{'url': configuration.Url})
self.comment(_("Write comments and bugs to %(email)s") %
{'email': configuration.Email})
self.check_date()
self.flush()
else:
# write empty string to initialize file output
self.write(u"")
self.writer = csv.writer(self.fd, dialect='excel',
delimiter=self.separator, lineterminator=os.linesep,
quotechar=self.quotechar)
for s in (u"urlname",
u"parentname",
u"baseref",
u"result",
u"warningstring",
u"infostring",
u"valid",
u"url",
u"line",
u"column",
u"name",
u"dltime",
u"dlsize",
u"checktime",
u"cached"):
if self.has_part(s):
row.append(s)
if row:
self.writer.writerow(row)
def log_url (self, url_data):
"""
Write csv formatted url check info.
"""
row = []
for s in (url_data.base_url,
url_data.parent_url, url_data.base_ref,
url_data.result,
os.linesep.join(url_data.warnings),
os.linesep.join(url_data.info),
url_data.valid, url_data.url,
url_data.line, url_data.column,
url_data.name, url_data.dltime,
url_data.dlsize, url_data.checktime,
url_data.cached):
if isinstance(s, unicode):
row.append(s.encode(self.output_encoding, "ignore"))
else:
row.append(s)
self.writer.writerow(row)
self.flush()
def end_output (self):
"""
Write end of checking info as csv comment.
"""
self.stoptime = time.time()
if self.has_part("outro"):
duration = self.stoptime - self.starttime
self.comment(_("Stopped checking at %(time)s (%(duration)s)") %
{"time": strformat.strtime(self.stoptime),
"duration": strformat.strduration_long(duration)})
self.close_fileoutput()
|