2004-08-16 19:28:42 +00:00
|
|
|
# -*- coding: iso-8859-1 -*-
|
2012-03-31 07:24:08 +00:00
|
|
|
# Copyright (C) 2000-2012 Bastian Kleineidam
|
2004-08-16 19:28:42 +00:00
|
|
|
#
|
|
|
|
|
# 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.
|
|
|
|
|
#
|
2009-07-24 21:58:20 +00:00
|
|
|
# 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.
|
2005-01-19 15:08:02 +00:00
|
|
|
"""
|
|
|
|
|
A CSV logger.
|
|
|
|
|
"""
|
2004-08-16 19:28:42 +00:00
|
|
|
import csv
|
|
|
|
|
import os
|
2011-04-06 10:54:58 +00:00
|
|
|
import sys
|
2008-05-09 06:16:03 +00:00
|
|
|
from . import Logger
|
2010-11-21 19:48:50 +00:00
|
|
|
from .. import strformat
|
2004-08-16 19:28:42 +00:00
|
|
|
|
|
|
|
|
|
2008-05-09 06:16:03 +00:00
|
|
|
class CSVLogger (Logger):
|
2004-08-16 19:28:42 +00:00
|
|
|
"""
|
2005-01-19 14:38:01 +00:00
|
|
|
CSV output, consisting of one line per entry. Entries are
|
2010-07-31 20:30:11 +00:00
|
|
|
separated by a separator (a semicolon per default).
|
2005-01-19 14:38:01 +00:00
|
|
|
"""
|
|
|
|
|
|
2004-08-16 19:28:42 +00:00
|
|
|
def __init__ (self, **args):
|
2011-02-14 20:06:34 +00:00
|
|
|
"""Store default separator and (os dependent) line terminator."""
|
2004-08-16 19:28:42 +00:00
|
|
|
super(CSVLogger, self).__init__(**args)
|
2010-11-21 19:48:50 +00:00
|
|
|
# due to a limitation of the csv module, all output has to be
|
|
|
|
|
# utf-8 encoded
|
|
|
|
|
self.output_encoding = "utf-8"
|
2004-08-25 20:08:53 +00:00
|
|
|
self.init_fileoutput(args)
|
2004-08-16 19:28:42 +00:00
|
|
|
self.separator = args['separator']
|
2005-01-20 22:21:43 +00:00
|
|
|
self.quotechar = args['quotechar']
|
2010-11-21 19:48:50 +00:00
|
|
|
self.linesep = os.linesep
|
|
|
|
|
|
|
|
|
|
def create_fd (self):
|
|
|
|
|
"""Create open file descriptor."""
|
2011-04-06 10:54:58 +00:00
|
|
|
if self.filename is None:
|
|
|
|
|
return sys.stdout
|
2010-11-21 19:48:50 +00:00
|
|
|
return open(self.filename, "wb")
|
2004-08-16 19:28:42 +00:00
|
|
|
|
2012-04-22 15:52:47 +00:00
|
|
|
def write (self, s, **args):
|
|
|
|
|
"""Write encoded string."""
|
|
|
|
|
super(CSVLogger, self).write(self.encode(s), **args)
|
|
|
|
|
|
2004-11-25 16:20:58 +00:00
|
|
|
def comment (self, s, **args):
|
2011-02-14 20:06:34 +00:00
|
|
|
"""Write CSV comment."""
|
2012-04-11 18:43:46 +00:00
|
|
|
self.writeln(s=u"# %s" % s, **args)
|
2004-11-25 16:20:58 +00:00
|
|
|
|
2004-08-16 19:28:42 +00:00
|
|
|
def start_output (self):
|
2011-02-14 20:06:34 +00:00
|
|
|
"""Write checking start info as csv comment."""
|
2004-08-25 20:08:53 +00:00
|
|
|
super(CSVLogger, self).start_output()
|
2005-01-16 23:30:16 +00:00
|
|
|
row = []
|
2005-05-08 20:06:40 +00:00
|
|
|
if self.has_part("intro"):
|
2010-11-01 08:58:03 +00:00
|
|
|
self.write_intro()
|
2004-08-16 19:28:42 +00:00
|
|
|
self.flush()
|
2009-06-18 17:59:54 +00:00
|
|
|
else:
|
|
|
|
|
# write empty string to initialize file output
|
|
|
|
|
self.write(u"")
|
|
|
|
|
self.writer = csv.writer(self.fd, dialect='excel',
|
2010-11-21 19:48:50 +00:00
|
|
|
delimiter=self.separator, lineterminator=self.linesep,
|
|
|
|
|
quotechar=self.quotechar)
|
2009-06-18 04:52:33 +00:00
|
|
|
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",
|
2011-04-09 08:51:03 +00:00
|
|
|
u"cached",
|
|
|
|
|
u"level"):
|
2009-06-18 04:52:33 +00:00
|
|
|
if self.has_part(s):
|
|
|
|
|
row.append(s)
|
2005-01-16 23:30:16 +00:00
|
|
|
if row:
|
2010-11-21 19:48:50 +00:00
|
|
|
self.writerow(row)
|
2004-08-16 19:28:42 +00:00
|
|
|
|
2005-04-25 14:51:35 +00:00
|
|
|
def log_url (self, url_data):
|
2011-02-14 20:06:34 +00:00
|
|
|
"""Write csv formatted url check info."""
|
2004-10-27 22:34:50 +00:00
|
|
|
row = []
|
2011-04-06 09:12:05 +00:00
|
|
|
if self.has_part("urlname"):
|
|
|
|
|
row.append(url_data.base_url)
|
|
|
|
|
if self.has_part("parentname"):
|
|
|
|
|
row.append(url_data.parent_url)
|
|
|
|
|
if self.has_part("baseref"):
|
|
|
|
|
row.append(url_data.base_ref)
|
|
|
|
|
if self.has_part("result"):
|
|
|
|
|
row.append(url_data.result)
|
|
|
|
|
if self.has_part("warningstring"):
|
|
|
|
|
row.append(self.linesep.join(x[1] for x in url_data.warnings))
|
|
|
|
|
if self.has_part("infostring"):
|
|
|
|
|
row.append(self.linesep.join(url_data.info))
|
|
|
|
|
if self.has_part("valid"):
|
|
|
|
|
row.append(url_data.valid)
|
|
|
|
|
if self.has_part("url"):
|
|
|
|
|
row.append(url_data.url)
|
|
|
|
|
if self.has_part("line"):
|
|
|
|
|
row.append(url_data.line)
|
|
|
|
|
if self.has_part("column"):
|
|
|
|
|
row.append(url_data.column)
|
|
|
|
|
if self.has_part("name"):
|
|
|
|
|
row.append(url_data.name)
|
|
|
|
|
if self.has_part("dltime"):
|
|
|
|
|
row.append(url_data.dltime)
|
|
|
|
|
if self.has_part("dlsize"):
|
|
|
|
|
row.append(url_data.dlsize)
|
|
|
|
|
if self.has_part("checktime"):
|
|
|
|
|
row.append(url_data.checktime)
|
|
|
|
|
if self.has_part("cached"):
|
|
|
|
|
row.append(url_data.cached)
|
2011-04-09 08:51:03 +00:00
|
|
|
if self.has_part("level"):
|
|
|
|
|
row.append(url_data.level)
|
2011-04-06 09:12:05 +00:00
|
|
|
self.writerow(map(strformat.unicode_safe, row))
|
2004-08-16 19:28:42 +00:00
|
|
|
self.flush()
|
|
|
|
|
|
2010-11-21 19:48:50 +00:00
|
|
|
def writerow (self, row):
|
2011-02-14 20:06:34 +00:00
|
|
|
"""Write one row in CSV format."""
|
2011-04-06 09:12:05 +00:00
|
|
|
self.writer.writerow(map(self.encode, row))
|
2010-11-21 19:48:50 +00:00
|
|
|
|
2005-04-25 14:51:35 +00:00
|
|
|
def end_output (self):
|
2011-02-14 20:06:34 +00:00
|
|
|
"""Write end of checking info as csv comment."""
|
2005-05-08 20:06:40 +00:00
|
|
|
if self.has_part("outro"):
|
2010-11-01 08:58:03 +00:00
|
|
|
self.write_outro()
|
2005-05-06 13:05:16 +00:00
|
|
|
self.close_fileoutput()
|