improve postprocessor initialization code
This commit is contained in:
@@ -159,7 +159,7 @@ class DownloadJob(Job):
|
|||||||
self.archive = None
|
self.archive = None
|
||||||
self.sleep = None
|
self.sleep = None
|
||||||
self.downloaders = {}
|
self.downloaders = {}
|
||||||
self.postprocessors = []
|
self.postprocessors = None
|
||||||
self.out = output.select()
|
self.out = output.select()
|
||||||
|
|
||||||
def handle_url(self, url, keywords, fallback=None):
|
def handle_url(self, url, keywords, fallback=None):
|
||||||
@@ -189,8 +189,9 @@ class DownloadJob(Job):
|
|||||||
return
|
return
|
||||||
|
|
||||||
# run post processors
|
# run post processors
|
||||||
for pp in self.postprocessors:
|
if self.postprocessors:
|
||||||
pp.run(self.pathfmt)
|
for pp in self.postprocessors:
|
||||||
|
pp.run(self.pathfmt)
|
||||||
|
|
||||||
# download succeeded
|
# download succeeded
|
||||||
self.pathfmt.finalize()
|
self.pathfmt.finalize()
|
||||||
@@ -209,26 +210,8 @@ class DownloadJob(Job):
|
|||||||
if not self.pathfmt:
|
if not self.pathfmt:
|
||||||
self.pathfmt = util.PathFormat(self.extractor)
|
self.pathfmt = util.PathFormat(self.extractor)
|
||||||
self.sleep = self.extractor.config("sleep")
|
self.sleep = self.extractor.config("sleep")
|
||||||
postprocs = self.extractor.config("postprocessor")
|
self._init_archive(self.extractor.config("archive"))
|
||||||
archive = self.extractor.config("archive")
|
self._init_postprocessors(self.extractor.config("postprocessor"))
|
||||||
|
|
||||||
if postprocs:
|
|
||||||
for pp_dict in postprocs:
|
|
||||||
try:
|
|
||||||
name = pp_dict["name"]
|
|
||||||
pp_obj = postprocessor.find(name)(pp_dict)
|
|
||||||
except KeyError as exc:
|
|
||||||
postprocessor.log.warning("missing key %s", exc)
|
|
||||||
except Exception as exc:
|
|
||||||
postprocessor.log.warning(
|
|
||||||
"%s %s", exc.__class__.__name__, exc)
|
|
||||||
else:
|
|
||||||
self.postprocessors.append(pp_obj)
|
|
||||||
|
|
||||||
if archive:
|
|
||||||
path = util.expand_path(archive)
|
|
||||||
self.archive = util.DownloadArchive(path, self.extractor)
|
|
||||||
|
|
||||||
self.pathfmt.set_directory(keywords)
|
self.pathfmt.set_directory(keywords)
|
||||||
|
|
||||||
def handle_queue(self, url, keywords):
|
def handle_queue(self, url, keywords):
|
||||||
@@ -250,6 +233,36 @@ class DownloadJob(Job):
|
|||||||
self.downloaders[scheme] = instance
|
self.downloaders[scheme] = instance
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
def _init_archive(self, archive):
|
||||||
|
if archive:
|
||||||
|
path = util.expand_path(archive)
|
||||||
|
self.archive = util.DownloadArchive(path, self.extractor)
|
||||||
|
|
||||||
|
def _init_postprocessors(self, postprocessors):
|
||||||
|
if not postprocessors:
|
||||||
|
return
|
||||||
|
|
||||||
|
self.postprocessors = []
|
||||||
|
for pp_dict in postprocessors:
|
||||||
|
if "name" not in pp_dict:
|
||||||
|
postprocessor.log.warning("no 'name' specified")
|
||||||
|
continue
|
||||||
|
|
||||||
|
name = pp_dict["name"]
|
||||||
|
pp_cls = postprocessor.find(name)
|
||||||
|
if not pp_cls:
|
||||||
|
postprocessor.log.warning("'%s' not found", name)
|
||||||
|
continue
|
||||||
|
|
||||||
|
try:
|
||||||
|
pp_obj = pp_cls(pp_dict)
|
||||||
|
except Exception as exc:
|
||||||
|
postprocessor.log.error(
|
||||||
|
"%s: initialization failed: %s %s",
|
||||||
|
name, exc.__class__.__name__, exc)
|
||||||
|
else:
|
||||||
|
self.postprocessors.append(pp_obj)
|
||||||
|
|
||||||
|
|
||||||
class KeywordJob(Job):
|
class KeywordJob(Job):
|
||||||
"""Print available keywords"""
|
"""Print available keywords"""
|
||||||
|
|||||||
@@ -425,6 +425,7 @@ class PathFormat():
|
|||||||
def set_keywords(self, keywords):
|
def set_keywords(self, keywords):
|
||||||
"""Set filename keywords"""
|
"""Set filename keywords"""
|
||||||
self.keywords = keywords
|
self.keywords = keywords
|
||||||
|
self.temppath = ""
|
||||||
self.has_extension = bool(keywords.get("extension"))
|
self.has_extension = bool(keywords.get("extension"))
|
||||||
if self.has_extension:
|
if self.has_extension:
|
||||||
self.build_path()
|
self.build_path()
|
||||||
@@ -455,7 +456,6 @@ class PathFormat():
|
|||||||
self.temppath = self.realpath + ".part"
|
self.temppath = self.realpath + ".part"
|
||||||
else:
|
else:
|
||||||
self.set_extension("part", False)
|
self.set_extension("part", False)
|
||||||
self.temppath = self.realpath
|
|
||||||
if part_directory:
|
if part_directory:
|
||||||
self.temppath = os.path.join(
|
self.temppath = os.path.join(
|
||||||
part_directory,
|
part_directory,
|
||||||
|
|||||||
Reference in New Issue
Block a user