Don't crash on multiple HTML output runs per day.
This commit is contained in:
parent
bed49c19ad
commit
e43694c156
3 changed files with 53 additions and 8 deletions
|
@ -1,5 +1,11 @@
|
|||
Dosage 2.16 (released xx.xx.xxxx)
|
||||
|
||||
Fixes:
|
||||
- output: Don't crash when HTML output is run more than once
|
||||
per day.
|
||||
Closes: GH bug #78
|
||||
|
||||
|
||||
Dosage 2.15 (released 3.7.2014)
|
||||
|
||||
Features:
|
||||
|
|
|
@ -139,8 +139,8 @@ class HtmlEventHandler(EventHandler):
|
|||
|
||||
def fnFromDate(self, date):
|
||||
"""Get filename from date."""
|
||||
fn = time.strftime('comics-%Y%m%d.html', date)
|
||||
fn = os.path.join(self.basepath, 'html', fn)
|
||||
fn = time.strftime('comics-%Y%m%d', date)
|
||||
fn = os.path.join(self.basepath, 'html', fn + ".html")
|
||||
fn = os.path.abspath(fn)
|
||||
return fn
|
||||
|
||||
|
@ -155,13 +155,21 @@ class HtmlEventHandler(EventHandler):
|
|||
|
||||
fn = self.fnFromDate(today)
|
||||
if os.path.exists(fn):
|
||||
raise ValueError('output file %r already exists' % fn)
|
||||
out.warn('HTML output file %r already exists' % fn)
|
||||
out.warn('the page link of previous run will skip this file')
|
||||
out.warn('try to generate HTML output only once per day')
|
||||
fn = util.getNonexistingFile(fn)
|
||||
|
||||
d = os.path.dirname(fn)
|
||||
if not os.path.isdir(d):
|
||||
os.makedirs(d)
|
||||
|
||||
yesterdayUrl = self.getUrlFromFilename(self.fnFromDate(yesterday))
|
||||
try:
|
||||
fn_yesterday = self.fnFromDate(yesterday)
|
||||
fn_yesterday = util.getExistingFile(fn_yesterday)
|
||||
yesterdayUrl = self.getUrlFromFilename(fn_yesterday)
|
||||
except ValueError:
|
||||
yesterdayUrl = None
|
||||
tomorrowUrl = self.getUrlFromFilename(self.fnFromDate(tomorrow))
|
||||
|
||||
self.html = codecs.open(fn, 'w', self.encoding)
|
||||
|
@ -173,10 +181,10 @@ class HtmlEventHandler(EventHandler):
|
|||
<title>Comics for %s</title>
|
||||
</head>
|
||||
<body>
|
||||
<a href="%s">Previous Day</a> | <a href="%s">Next Day</a>
|
||||
<ul>
|
||||
''' % (self.encoding, configuration.App, time.strftime('%Y/%m/%d', today),
|
||||
yesterdayUrl, tomorrowUrl))
|
||||
''' % (self.encoding, configuration.App, time.strftime('%Y/%m/%d', today)))
|
||||
if yesterdayUrl:
|
||||
self.html.write(u'<a href="%s">Previous Day</a> | ' % yesterdayUrl)
|
||||
self.html.write(u'<a href="%s">Next Day</a><ul>' % tomorrowUrl)
|
||||
# last comic name (eg. CalvinAndHobbes)
|
||||
self.lastComic = None
|
||||
# last comic strip URL (eg. http://example.com/page42)
|
||||
|
|
|
@ -548,6 +548,37 @@ def getFilename(name):
|
|||
return name
|
||||
|
||||
|
||||
def getExistingFile(name, max_suffix=1000):
|
||||
"""Add filename suffix until file exists
|
||||
@return: filename if file is found
|
||||
@raise: ValueError if maximum suffix number is reached while searching
|
||||
"""
|
||||
num = 1
|
||||
stem, ext = os.path.splitext(name)
|
||||
filename = name
|
||||
while not os.path.exists(filename):
|
||||
suffix = "-%d" % num
|
||||
filename = stem + suffix + ext
|
||||
num += 1
|
||||
if num >= max_suffix:
|
||||
raise ValueError("No file %r found" % name)
|
||||
return filename
|
||||
|
||||
|
||||
def getNonexistingFile(name):
|
||||
"""Add filename suffix until file not exists
|
||||
@return: filename
|
||||
"""
|
||||
num = 1
|
||||
stem, ext = os.path.splitext(name)
|
||||
filename = name
|
||||
while os.path.exists(filename):
|
||||
suffix = "-%d" % num
|
||||
filename = stem + suffix + ext
|
||||
num += 1
|
||||
return filename
|
||||
|
||||
|
||||
def strlimit (s, length=72):
|
||||
"""If the length of the string exceeds the given limit, it will be cut
|
||||
off and three dots will be appended.
|
||||
|
|
Loading…
Reference in a new issue