better file descriptor initialization and closing

git-svn-id: https://linkchecker.svn.sourceforge.net/svnroot/linkchecker/trunk/linkchecker@2566 e7d03fd6-7b0d-0410-9947-9c21f3af8025
This commit is contained in:
calvin 2005-05-06 13:05:16 +00:00
parent fc5aa749c7
commit cd3f5294cb
9 changed files with 24 additions and 42 deletions

View file

@ -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

View file

@ -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)

View file

@ -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()

View file

@ -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):

View file

@ -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()

View file

@ -307,7 +307,4 @@ class HtmlLogger (linkcheck.logger.Logger):
(u'<a href="mailto:'+linkcheck.configuration.Email+u'">'+
linkcheck.configuration.Email+u"</a>."))
self.writeln(u"</small></body></html>")
self.flush()
if self.close_fd:
self.fd.close()
self.fd = None
self.close_fileoutput()

View file

@ -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()

View file

@ -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()

View file

@ -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()