remove DownloadManager class

pull/13/head
Mike Fährmann 9 years ago
parent 352950eebe
commit 47f016a016
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -8,45 +8,17 @@
import os import os
import sys import sys
import importlib from . import config, extractor, downloader, text
from . import config, extractor, text
from .extractor.common import Message from .extractor.common import Message
class DownloadManager():
def __init__(self, opts):
self.opts = opts
self.modules = {}
def add(self, url):
job = DownloadJob(self, url)
job.run()
def get_downloader_module(self, scheme):
"""Return a downloader module suitable for 'scheme'"""
module = self.modules.get(scheme)
if module is None:
module = importlib.import_module(".downloader."+scheme, __package__)
self.modules[scheme] = module
return module
def get_base_directory(self):
"""Return the base-destination-directory for downloads"""
if self.opts.dest:
return self.opts.dest
else:
return config.get(("base-directory",), default="/tmp/")
class DownloadJob(): class DownloadJob():
def __init__(self, mngr, url): def __init__(self, url):
self.mngr = mngr
self.extractor, self.info = extractor.find(url) self.extractor, self.info = extractor.find(url)
if self.extractor is None: if self.extractor is None:
print(url, ": No extractor found", sep="", file=sys.stderr) print(url, ": No extractor found", sep="", file=sys.stderr)
return return
self.directory = mngr.get_base_directory() self.directory = self.get_base_directory()
self.downloaders = {} self.downloaders = {}
self.filename_fmt = config.get( self.filename_fmt = config.get(
("extractor", self.info["category"], "filename"), ("extractor", self.info["category"], "filename"),
@ -91,15 +63,15 @@ class DownloadJob():
if os.path.exists(path): if os.path.exists(path):
self.print_skip(path) self.print_skip(path)
return return
downloader = self.get_downloader(url) dlinstance = self.get_downloader(url)
self.print_start(path) self.print_start(path)
tries = downloader.download(url, path) tries = dlinstance.download(url, path)
self.print_success(path, tries) self.print_success(path, tries)
def set_directory(self, msg): def set_directory(self, msg):
"""Set and create the target directory for downloads""" """Set and create the target directory for downloads"""
self.directory = os.path.join( self.directory = os.path.join(
self.mngr.get_base_directory(), self.get_base_directory(),
self.directory_fmt.format(**{ self.directory_fmt.format(**{
key: text.clean_path(value) for key, value in msg[1].items() key: text.clean_path(value) for key, value in msg[1].items()
}) })
@ -112,12 +84,17 @@ class DownloadJob():
scheme = url[:pos] if pos != -1 else "http" scheme = url[:pos] if pos != -1 else "http"
if scheme == "https": if scheme == "https":
scheme = "http" scheme = "http"
downloader = self.downloaders.get(scheme) instance = self.downloaders.get(scheme)
if downloader is None: if instance is None:
module = self.mngr.get_downloader_module(scheme) klass = downloader.find(scheme)
downloader = module.Downloader() instance = klass()
self.downloaders[scheme] = downloader self.downloaders[scheme] = instance
return downloader return instance
@staticmethod
def get_base_directory():
"""Return the base-destination-directory for downloads"""
return config.get(("base-directory",), default="/tmp/")
@staticmethod @staticmethod
def print_start(path): def print_start(path):

Loading…
Cancel
Save