From 03fff069ee3de11cea42fec1b768829ec498cdde Mon Sep 17 00:00:00 2001 From: Bastian Kleineidam Date: Thu, 5 Dec 2013 18:29:15 +0100 Subject: [PATCH] Apply same file checks files as for image files. --- dosagelib/comic.py | 25 +++++-------------------- dosagelib/util.py | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 20 deletions(-) diff --git a/dosagelib/comic.py b/dosagelib/comic.py index 123b5b767..1bc0b0e07 100644 --- a/dosagelib/comic.py +++ b/dosagelib/comic.py @@ -2,10 +2,9 @@ # Copyright (C) 2004-2005 Tristan Seligmann and Jonathan Jacobs # Copyright (C) 2012-2013 Bastian Kleineidam import os -import codecs from .output import out -from .util import getImageObject, normaliseURL, unquote, strsize, getDirname, getFilename +from .util import getImageObject, normaliseURL, unquote, getDirname, getFilename, writeFile from .events import getHandler class ComicStrip(object): @@ -86,25 +85,11 @@ class ComicImage(object): out.warn(u"Empty content from %s, try again..." % self.url) self.connect() content = self.urlobj.content - try: - out.debug(u'Writing comic to file %s...' % fn) - with open(fn, 'wb') as comicOut: - comicOut.write(content) - comicOut.flush() - os.fsync(comicOut.fileno()) - size = os.path.getsize(fn) - if size == 0: - raise OSError("empty file %s" % fn) - except Exception: - if os.path.isfile(fn): - os.remove(fn) - raise - else: - out.info(u"Saved %s (%s)." % (fn, strsize(size))) - getHandler().comicDownloaded(self, fn, text=self.text) + out.debug(u'Writing comic to file %s...' % fn) + writeFile(fn, content) if self.text: fntext = os.path.join(comicDir, "%s.txt" % self.filename) out.debug(u'Writing comic text to file %s...' % fntext) - with codecs.open(fntext, 'w', 'utf-8') as textOut: - textOut.write(self.text) + writeFile(fntext, self.text, encoding='utf-8') + getHandler().comicDownloaded(self, fn, text=self.text) return fn, True diff --git a/dosagelib/util.py b/dosagelib/util.py index f208759f3..44d445c79 100644 --- a/dosagelib/util.py +++ b/dosagelib/util.py @@ -19,6 +19,7 @@ import sys import os import cgi import re +import codecs import traceback import time import subprocess @@ -565,3 +566,30 @@ def strlimit (s, length=72): def getLangName(code): """Get name of language specified by ISO 693-1 code.""" return Iso2Language[code] + + +def writeFile(filename, content, encoding=None): + """Write content to given filename. Checks for zero-sized files. + If encoding is given writes to a codec.open() file.""" + if not content: + raise OSError("empty content for file %s" % filename) + + def getfp(filename, encoding): + if encoding: + return codecs.open(filename, 'w', encoding) + return open(filename, 'wb') + + try: + with getfp(filename, encoding) as fp: + fp.write(content) + fp.flush() + os.fsync(fp.fileno()) + size = os.path.getsize(filename) + if size == 0: + raise OSError("empty file %s" % filename) + except Exception: + if os.path.isfile(filename): + os.remove(filename) + raise + else: + out.info(u"Saved %s (%s)." % (filename, strsize(size)))