From 43c2e6641bb1db0aee40de5411c4a4e10c78e4fd Mon Sep 17 00:00:00 2001 From: Bastian Kleineidam Date: Wed, 30 Apr 2014 19:59:43 +0200 Subject: [PATCH] Logging refactor, interrupt and abort flags added. --- linkcheck/director/__init__.py | 6 +++--- linkcheck/director/aggregator.py | 6 +++--- linkcheck/gui/logger.py | 4 ++-- linkcheck/logger/__init__.py | 7 ++++--- linkcheck/logger/blacklist.py | 2 +- linkcheck/logger/csvlog.py | 2 +- linkcheck/logger/customxml.py | 2 +- linkcheck/logger/graph.py | 2 +- linkcheck/logger/gxml.py | 2 +- linkcheck/logger/html.py | 2 +- linkcheck/logger/none.py | 2 +- linkcheck/logger/sitemapxml.py | 2 +- linkcheck/logger/sql.py | 2 +- linkcheck/logger/text.py | 6 +++--- 14 files changed, 24 insertions(+), 23 deletions(-) diff --git a/linkcheck/director/__init__.py b/linkcheck/director/__init__.py index 4a35588c..fe8ec483 100644 --- a/linkcheck/director/__init__.py +++ b/linkcheck/director/__init__.py @@ -163,7 +163,7 @@ def interrupt (aggregate): while True: try: log.warn(LOG_CHECK, - _("user interrupt; waiting for active threads to finish")) + _("interrupt; waiting for active threads to finish")) log.warn(LOG_CHECK, _("another interrupt will exit immediately")) abort(aggregate) @@ -178,11 +178,11 @@ def abort (aggregate): try: aggregate.abort() aggregate.finish() - aggregate.end_log_output() + aggregate.end_log_output(interrupt=True) break except KeyboardInterrupt: log.warn(LOG_CHECK, _("user abort; force shutdown")) - aggregate.end_log_output() + aggregate.end_log_output(abort=True) abort_now() diff --git a/linkcheck/director/aggregator.py b/linkcheck/director/aggregator.py index 82f8325b..82fe5822 100644 --- a/linkcheck/director/aggregator.py +++ b/linkcheck/director/aggregator.py @@ -173,10 +173,10 @@ class Aggregate (object): """Add to number of downloaded bytes.""" self.downloaded_bytes += numbytes - def end_log_output(self): + def end_log_output(self, **kwargs): """Print ending output to log.""" - kwargs = dict( + kwargs.update(dict( downloaded_bytes=self.downloaded_bytes, num_urls = len(self.result_cache), - ) + )) self.logger.end_log_output(**kwargs) diff --git a/linkcheck/gui/logger.py b/linkcheck/gui/logger.py index 6cbab1a0..d55cf039 100644 --- a/linkcheck/gui/logger.py +++ b/linkcheck/gui/logger.py @@ -55,9 +55,9 @@ class SignalLogger (_Logger): """Emit URL data which gets logged in the main window.""" self.log_url_signal.emit(url_data) - def end_output (self, downloaded_bytes=None, num_urls=None): + def end_output (self, **kwargs): """Emit statistic data which gets logged in the main window.""" - self.stats.downloaded_bytes = downloaded_bytes + self.stats.downloaded_bytes = kwargs.get("downloaded_bytes") self.log_stats_signal.emit(self.stats) diff --git a/linkcheck/logger/__init__.py b/linkcheck/logger/__init__.py index cede4dcc..7409e7e9 100644 --- a/linkcheck/logger/__init__.py +++ b/linkcheck/logger/__init__.py @@ -139,9 +139,10 @@ class _Logger (object): * start_output() Initialize and start log output. Most loggers print a comment with copyright information. - * end_output() + * end_output(**kwargs) Finish log output, possibly flushing buffers. Most loggers also print some statistics. + Custom keyword arguments can be given for different loggers. * log_filter_url(url_data, do_print) Log a checked URL. The url_data object is a transport form of the UrlData class. The do_print flag indicates if this URL @@ -151,7 +152,7 @@ class _Logger (object): * start_output() Also call the base class implementation of this. - * end_output() + * end_output(**kwargs) See above. * log_url(url_data) Log a checked URL. Called by log_filter_url if do_print is True. @@ -401,7 +402,7 @@ class _Logger (object): pass @abc.abstractmethod - def end_output (self, downloaded_bytes=None, num_urls=None): + def end_output (self, **kwargs): """ End of output, used for cleanup (eg output buffer flushing). """ diff --git a/linkcheck/logger/blacklist.py b/linkcheck/logger/blacklist.py index f4bd6b13..e4174c67 100644 --- a/linkcheck/logger/blacklist.py +++ b/linkcheck/logger/blacklist.py @@ -66,7 +66,7 @@ class BlacklistLogger (_Logger): if not url_data.valid: self.blacklist[key] = 1 - def end_output (self, downloaded_bytes=None, num_urls=None): + def end_output (self, **kwargs): """ Write blacklist file. """ diff --git a/linkcheck/logger/csvlog.py b/linkcheck/logger/csvlog.py index 083a56b9..43329e17 100644 --- a/linkcheck/logger/csvlog.py +++ b/linkcheck/logger/csvlog.py @@ -130,7 +130,7 @@ class CSVLogger (_Logger): # empty queue self.queue.truncate(0) - def end_output (self, downloaded_bytes=None, num_urls=None): + def end_output (self, **kwargs): """Write end of checking info as csv comment.""" if self.has_part("outro"): self.write_outro() diff --git a/linkcheck/logger/customxml.py b/linkcheck/logger/customxml.py index 005800e5..d81b471a 100644 --- a/linkcheck/logger/customxml.py +++ b/linkcheck/logger/customxml.py @@ -95,7 +95,7 @@ class CustomXMLLogger (xmllog._XMLLogger): self.xml_endtag(u'urldata') self.flush() - def end_output (self, downloaded_bytes=None, num_urls=None): + def end_output (self, **kwargs): """ Write XML end tag. """ diff --git a/linkcheck/logger/graph.py b/linkcheck/logger/graph.py index 31352d90..adf416e4 100644 --- a/linkcheck/logger/graph.py +++ b/linkcheck/logger/graph.py @@ -83,7 +83,7 @@ class _GraphLogger (_Logger): """Write end-of-graph marker.""" pass - def end_output (self, downloaded_bytes=None, num_urls=None): + def end_output (self, **kwargs): """Write edges and end of checking info as gml comment.""" self.write_edges() self.end_graph() diff --git a/linkcheck/logger/gxml.py b/linkcheck/logger/gxml.py index ea000784..d141ba2e 100644 --- a/linkcheck/logger/gxml.py +++ b/linkcheck/logger/gxml.py @@ -81,7 +81,7 @@ class GraphXMLLogger (_XMLLogger, _GraphLogger): self.xml_endtag(u"data") self.xml_endtag(u"edge") - def end_output (self, downloaded_bytes=None, num_urls=None): + def end_output (self, **kwargs): """Finish graph output, and print end of checking info as xml comment.""" self.xml_endtag(u"graph") diff --git a/linkcheck/logger/html.py b/linkcheck/logger/html.py index c96e93f1..d5bc4a46 100644 --- a/linkcheck/logger/html.py +++ b/linkcheck/logger/html.py @@ -328,7 +328,7 @@ class HtmlLogger (_Logger): configuration.DonateUrl+u".")) self.writeln(u"") - def end_output (self, downloaded_bytes=None, num_urls=None): + def end_output (self, **kwargs): """Write end of checking info as HTML.""" if self.has_part("stats"): self.write_stats() diff --git a/linkcheck/logger/none.py b/linkcheck/logger/none.py index 3bd50d14..c4771b9e 100644 --- a/linkcheck/logger/none.py +++ b/linkcheck/logger/none.py @@ -43,7 +43,7 @@ class NoneLogger (_Logger): """Do nothing.""" pass - def end_output (self, downloaded_bytes=None, num_urls=None): + def end_output (self, **kwargs): """ Do nothing. """ diff --git a/linkcheck/logger/sitemapxml.py b/linkcheck/logger/sitemapxml.py index 580e4450..b708bec0 100644 --- a/linkcheck/logger/sitemapxml.py +++ b/linkcheck/logger/sitemapxml.py @@ -113,7 +113,7 @@ class SitemapXmlLogger (xmllog._XMLLogger): self.xml_endtag(u'url') self.flush() - def end_output (self, downloaded_bytes=None, num_urls=None): + def end_output (self, **kwargs): """Write XML end tag.""" self.xml_endtag(u"urlset") self.xml_end_output() diff --git a/linkcheck/logger/sql.py b/linkcheck/logger/sql.py index f59e8b5f..8412c892 100644 --- a/linkcheck/logger/sql.py +++ b/linkcheck/logger/sql.py @@ -131,7 +131,7 @@ class SQLLogger (_Logger): }) self.flush() - def end_output (self, downloaded_bytes=None, num_urls=None): + def end_output (self, **kwargs): """ Write end of checking info as sql comment. """ diff --git a/linkcheck/logger/text.py b/linkcheck/logger/text.py index 5616d758..df5eb374 100644 --- a/linkcheck/logger/text.py +++ b/linkcheck/logger/text.py @@ -282,10 +282,10 @@ class TextLogger (_Logger): else: self.writeln(_("No statistics available since no URLs were checked.")) - def end_output (self, downloaded_bytes=None, num_urls=None): + def end_output (self, **kwargs): """Write end of output info, and flush all output buffers.""" - self.stats.downloaded_bytes = downloaded_bytes - self.stats.num_urls = num_urls + self.stats.downloaded_bytes = kwargs.get("downloaded_bytes") + self.stats.num_urls = kwargs.get("num_urls") if self.has_part('stats'): self.write_stats() if self.has_part('outro'):