improve postprocessor handling

- add pathfmt argument for __init__()
- add finalization step
- add option to keep or delete zipped files
This commit is contained in:
Mike Fährmann
2018-06-08 17:39:02 +02:00
parent 2628911ba0
commit baccf8a958
6 changed files with 75 additions and 47 deletions

View File

@@ -14,7 +14,7 @@ import os
class ClassifyPP(PostProcessor):
DEFAULT_MAP = {
DEFAULT_MAPPING = {
"Music" : ("mp3", "aac", "flac", "ogg", "wma", "m4a", "wav"),
"Video" : ("flv", "ogv", "avi", "mp4", "mpg", "mpeg", "3gp", "mkv",
"webm", "vob", "wmv"),
@@ -22,9 +22,9 @@ class ClassifyPP(PostProcessor):
"Archives" : ("zip", "rar", "7z", "tar", "gz", "bz2"),
}
def __init__(self, options):
def __init__(self, pathfmt, options):
PostProcessor.__init__(self)
mapping = options.get("mapping", self.DEFAULT_MAP)
mapping = options.get("mapping", self.DEFAULT_MAPPING)
self.mapping = {
ext: directory

View File

@@ -12,7 +12,11 @@ from . import log
class PostProcessor():
"""Base class for postprocessors"""
log = log
def run(self, pathfmt):
raise NotImplementedError()
"""Execute the postprocessor for a file"""
def finalize(self):
"""Cleanup"""

View File

@@ -14,7 +14,7 @@ import subprocess
class ExecPP(PostProcessor):
def __init__(self, options):
def __init__(self, pathfmt, options):
PostProcessor.__init__(self)
self.args = options["command"]
if options.get("async", False):

View File

@@ -6,7 +6,7 @@
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
"""Add files to ZIP archives"""
"""Store files in ZIP archives"""
from .common import PostProcessor
import zipfile
@@ -21,19 +21,31 @@ class ZipPP(PostProcessor):
"lzma": zipfile.ZIP_LZMA,
}
def __init__(self, options):
def __init__(self, pathfmt, options):
PostProcessor.__init__(self)
self.delete = not options.get("keep-files", False)
self.ext = "." + options.get("extension", "zip")
algorithm = options.get("compression", "store")
if algorithm not in self.COMPRESSION_ALGORITHMS:
self.log.warning(
"unknown compression algorithm '%s'; falling back to 'store'",
algorithm)
algorithm = "store"
self.compression = self.COMPRESSION_ALGORITHMS[algorithm]
path = pathfmt.realdirectory + self.ext
self.zfile = zipfile.ZipFile(
path, "a", self.COMPRESSION_ALGORITHMS[algorithm], True)
def run(self, pathfmt):
archive = pathfmt.realdirectory + self.ext
with zipfile.ZipFile(archive, "a", self.compression, True) as zfile:
zfile.write(pathfmt.temppath, pathfmt.filename)
# 'NameToInfo' is not officially documented, but it's available
# for all supported Python versions and using it directly is a lot
# better than calling getinfo()
if pathfmt.filename not in self.zfile.NameToInfo:
self.zfile.write(pathfmt.temppath, pathfmt.filename)
pathfmt.delete = self.delete
def finalize(self):
self.zfile.close()
__postprocessor__ = ZipPP