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

5
dosage
View file

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

View file

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

View file

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