Fix event handling.

This commit is contained in:
Bastian Kleineidam 2012-10-12 22:07:50 +02:00
parent aff905e0bd
commit 7a70cd79ca
3 changed files with 13 additions and 15 deletions

7
dosage
View file

@ -86,8 +86,9 @@ def displayHelp(comics, basepath):
def getComics(options, comics):
"""Retrieve given comics."""
errors = 0
events.installHandler(options.output, options.basepath, options.baseurl)
events.handler.start()
if options.output:
events.installHandler(options.output, options.basepath, options.baseurl)
events.getHandler().start()
for scraperobj in getScrapers(comics, options.basepath):
out.context = scraperobj.get_name()
if options.all:
@ -104,7 +105,7 @@ def getComics(options, comics):
out.write("Stop retrieval because image file already exists")
break
first = False
events.handler.end()
events.getHandler().end()
return errors

View file

@ -8,7 +8,7 @@ import time
from .output import out
from .util import urlopen, saneDataSize, normaliseURL
from .events import handler
from .events import getHandler
class FetchComicError(IOError):
"""Exception for comic fetching errors."""
@ -111,7 +111,7 @@ class ComicImage(object):
speed = '???'
attrs = dict(fn=fn, bytes=bytes, speed=speed)
out.write('Saved "%(fn)s" (%(bytes)s bytes, %(speed)s/sec).' % attrs, 1)
handler.comicDownloaded(self.name, fn)
getHandler().comicDownloaded(self.name, fn)
finally:
self.urlobj.close()

View file

@ -41,9 +41,6 @@ class EventHandler(object):
"""Emit an end event. Should be overridden in subclass."""
pass
class TextEventHandler(EventHandler):
"""Output nothing. XXX why?"""
pass
class RSSEventHandler(EventHandler):
"""Output in RSS format."""
@ -164,7 +161,6 @@ class HtmlEventHandler(EventHandler):
handlers = {
'text': TextEventHandler,
'html': HtmlEventHandler,
'rss': RSSEventHandler,
}
@ -173,13 +169,14 @@ def getHandlers():
"""Get sorted handler names."""
return sorted(handlers.keys())
def installHandler(name=None, basepath=None, baseurl=None):
_handler = EventHandler(".", None)
def installHandler(name, basepath=None, baseurl=None):
"""Install a global handler with given name."""
global handler
if name is None:
name = 'text'
global _handler
if basepath is None:
basepath = '.'
handler = handlers[name](basepath, baseurl)
_handler = handlers[name](basepath, baseurl)
installHandler()
def getHandler():
return _handler