diff --git a/linkcheck/logger/__init__.py b/linkcheck/logger/__init__.py index 478b621c..097a02f9 100644 --- a/linkcheck/logger/__init__.py +++ b/linkcheck/logger/__init__.py @@ -76,11 +76,11 @@ class Logger (object): Initialize self.fd file descriptor from args. """ if args.get('fileoutput'): - fname = args['filename'] - path = os.path.dirname(fname) + filename = args['filename'] + path = os.path.dirname(filename) if path and not os.path.isdir(path): os.makedirs(path) - self.fd = file(args['filename'], "w") + self.fd = file(filename, "wb") self.close_fd = True elif args.has_key('fd'): self.fd = args['fd'] @@ -89,6 +89,15 @@ class Logger (object): self.fd = sys.stdout self.close_fd = False + def close_fileoutput (self): + """ + Flush and close the file output denoted by self.fd. + """ + self.flush() + if self.close_fd: + self.fd.close() + self.fd = None + def encode (self, s): """ Encode string with configured output encoding. Wrong encoded diff --git a/linkcheck/logger/blacklist.py b/linkcheck/logger/blacklist.py index 599546f9..704c242c 100644 --- a/linkcheck/logger/blacklist.py +++ b/linkcheck/logger/blacklist.py @@ -37,18 +37,12 @@ class BlacklistLogger (linkcheck.logger.Logger): Intialize with old blacklist data (if found, else not). """ super(BlacklistLogger, self).__init__(**args) + self.init_fileoutput(args) self.blacklist = {} if args.get('fileoutput'): - self.fileoutput = True filename = args['filename'] if os.path.exists(filename): - self.read_blacklist(file(filename, "r")) - self.fd = file(filename, "w") - elif args.has_key('fd'): - self.fd = args['fd'] - else: - self.fileoutput = False - self.fd = sys.stdout + self.read_blacklist(filename) def comment (self, s, **args): """ @@ -77,10 +71,11 @@ class BlacklistLogger (linkcheck.logger.Logger): """ self.write_blacklist() - def read_blacklist (self, fd): + def read_blacklist (self, filename): """ Read a previously stored blacklist from file fd. """ + fd = file(filename) for line in fd: line = line.rstrip() if line.startswith('#') or not line: @@ -97,7 +92,6 @@ class BlacklistLogger (linkcheck.logger.Logger): for key, value in self.blacklist.items(): self.fd.write("%d %s" % (value, key)) self.fd.write(os.linesep) - if self.fileoutput: - self.fd.close() + self.close_fileoutput() # restore umask os.umask(oldmask) diff --git a/linkcheck/logger/csvlog.py b/linkcheck/logger/csvlog.py index ee7ab0e4..446fdf4e 100644 --- a/linkcheck/logger/csvlog.py +++ b/linkcheck/logger/csvlog.py @@ -128,7 +128,4 @@ class CSVLogger (linkcheck.logger.Logger): self.comment(_("Stopped checking at %s (%s)") % \ (linkcheck.strformat.strtime(self.stoptime), linkcheck.strformat.strduration(duration))) - self.flush() - if self.close_fd: - self.fd.close() - self.fd = None + self.close_fileoutput() diff --git a/linkcheck/logger/dot.py b/linkcheck/logger/dot.py index b23e0989..ef83152e 100644 --- a/linkcheck/logger/dot.py +++ b/linkcheck/logger/dot.py @@ -120,10 +120,7 @@ class DOTLogger (linkcheck.logger.Logger): self.comment(_("Stopped checking at %s (%s)")%\ (linkcheck.strformat.strtime(self.stoptime), linkcheck.strformat.strduration(duration))) - self.flush() - if self.close_fd: - self.fd.close() - self.fd = None + self.close_fileoutput() def dotquote (s): diff --git a/linkcheck/logger/gml.py b/linkcheck/logger/gml.py index 8da2fa1e..3f84611c 100644 --- a/linkcheck/logger/gml.py +++ b/linkcheck/logger/gml.py @@ -124,7 +124,4 @@ class GMLLogger (linkcheck.logger.Logger): self.comment(_("Stopped checking at %s (%s)")%\ (linkcheck.strformat.strtime(self.stoptime), linkcheck.strformat.strduration(duration))) - self.flush() - if self.close_fd: - self.fd.close() - self.fd = None + self.close_fileoutput() diff --git a/linkcheck/logger/html.py b/linkcheck/logger/html.py index c3594afc..2e55f657 100644 --- a/linkcheck/logger/html.py +++ b/linkcheck/logger/html.py @@ -307,7 +307,4 @@ class HtmlLogger (linkcheck.logger.Logger): (u''+ linkcheck.configuration.Email+u".")) self.writeln(u"") - self.flush() - if self.close_fd: - self.fd.close() - self.fd = None + self.close_fileoutput() diff --git a/linkcheck/logger/sql.py b/linkcheck/logger/sql.py index 467a4066..83df49e1 100644 --- a/linkcheck/logger/sql.py +++ b/linkcheck/logger/sql.py @@ -148,7 +148,4 @@ class SQLLogger (linkcheck.logger.Logger): self.comment(_("Stopped checking at %s (%s)") % \ (linkcheck.strformat.strtime(self.stoptime), linkcheck.strformat.strduration(duration))) - self.flush() - if self.close_fd: - self.fd.close() - self.fd = None + self.close_fileoutput() diff --git a/linkcheck/logger/text.py b/linkcheck/logger/text.py index d8badcc1..1cf76475 100644 --- a/linkcheck/logger/text.py +++ b/linkcheck/logger/text.py @@ -254,7 +254,4 @@ class TextLogger (linkcheck.logger.Logger): self.writeln(_("Stopped checking at %s (%s)") % \ (linkcheck.strformat.strtime(self.stoptime), linkcheck.strformat.strduration(duration))) - self.flush() - if self.close_fd: - self.fd.close() - self.fd = None + self.close_fileoutput() diff --git a/linkcheck/logger/xmllog.py b/linkcheck/logger/xmllog.py index 7edce3ef..04f44e95 100644 --- a/linkcheck/logger/xmllog.py +++ b/linkcheck/logger/xmllog.py @@ -171,7 +171,4 @@ class XMLLogger (linkcheck.logger.Logger): self.comment(_("Stopped checking at %s (%s)") % \ (linkcheck.strformat.strtime(self.stoptime), linkcheck.strformat.strduration(duration))) - self.flush() - if self.close_fd: - self.fd.close() - self.fd = None + self.close_fileoutput()