diff options
| author | 2019-10-11 20:28:32 -0400 | |
|---|---|---|
| committer | 2019-10-11 20:28:32 -0400 | |
| commit | 40f5fe6edef268632d3bc484e85e5b37bad67bff (patch) | |
| tree | 98817850b65f1d2877bd4ed63a3908f37d794f8d /gallery_dl/postprocessor | |
| parent | 639d9ea4a667733aadc3ff83a1df2cc9f0add3a9 (diff) | |
New upstream version 1.10.6upstream/1.10.6
Diffstat (limited to 'gallery_dl/postprocessor')
| -rw-r--r-- | gallery_dl/postprocessor/common.py | 22 | ||||
| -rw-r--r-- | gallery_dl/postprocessor/exec.py | 56 |
2 files changed, 57 insertions, 21 deletions
diff --git a/gallery_dl/postprocessor/common.py b/gallery_dl/postprocessor/common.py index b967cf6..71ef932 100644 --- a/gallery_dl/postprocessor/common.py +++ b/gallery_dl/postprocessor/common.py @@ -8,20 +8,30 @@ """Common classes and constants used by postprocessor modules.""" -from . import log +import logging class PostProcessor(): """Base class for postprocessors""" - log = log - def prepare(self, pathfmt): - """ """ + def __init__(self): + name = self.__class__.__name__[:-2].lower() + self.log = logging.getLogger("postprocessor." + name) - def run(self, pathfmt): + @staticmethod + def prepare(pathfmt): + """Update file paths, etc.""" + + @staticmethod + def run(pathfmt): """Execute the postprocessor for a file""" - def finalize(self): + @staticmethod + def run_after(pathfmt): + """Execute postprocessor after moving a file to its target location""" + + @staticmethod + def finalize(): """Cleanup""" def __repr__(self): diff --git a/gallery_dl/postprocessor/exec.py b/gallery_dl/postprocessor/exec.py index c86b480..19a9b87 100644 --- a/gallery_dl/postprocessor/exec.py +++ b/gallery_dl/postprocessor/exec.py @@ -1,6 +1,6 @@ # -*- coding: utf-8 -*- -# Copyright 2018 Mike Fährmann +# Copyright 2018-2019 Mike Fährmann # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License version 2 as @@ -9,35 +9,61 @@ """Execute processes""" from .common import PostProcessor +from .. import util import subprocess +import os + + +if os.name == "nt": + def quote(s): + return '"' + s.replace('"', '\\"') + '"' +else: + from shlex import quote class ExecPP(PostProcessor): def __init__(self, pathfmt, options): PostProcessor.__init__(self) + args = options["command"] - try: - self.args = options["command"] - self.args[0] # test if 'args' is subscriptable - except (KeyError, IndexError, TypeError): - raise TypeError("option 'command' must be a non-empty list") + if isinstance(args, str): + if "{}" not in args: + args += " {}" + self.args = args + self.shell = True + self._format = self._format_args_string + else: + self.args = [util.Formatter(arg) for arg in args] + self.shell = False + self._format = self._format_args_list if options.get("async", False): - self._exec = subprocess.Popen + self._exec = self._exec_async + + def run_after(self, pathfmt): + self._exec(self._format(pathfmt)) - def run(self, pathfmt): - self._exec([ - arg.format_map(pathfmt.keywords) - for arg in self.args - ]) + def _format_args_string(self, pathfmt): + return self.args.replace("{}", quote(pathfmt.realpath)) + + def _format_args_list(self, pathfmt): + kwdict = pathfmt.kwdict + kwdict["_directory"] = pathfmt.realdirectory + kwdict["_filename"] = pathfmt.filename + kwdict["_path"] = pathfmt.realpath + return [arg.format_map(kwdict) for arg in self.args] def _exec(self, args): - retcode = subprocess.Popen(args).wait() + self.log.debug("Running '%s'", args) + retcode = subprocess.Popen(args, shell=self.shell).wait() if retcode: self.log.warning( - "executing '%s' returned non-zero exit status %d", - " ".join(args), retcode) + "Executing '%s' returned with non-zero exit status (%d)", + " ".join(args) if isinstance(args, list) else args, retcode) + + def _exec_async(self, args): + subprocess.Popen(args, shell=self.shell) __postprocessor__ = ExecPP |
