| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Categorize files by file extension""" |
| 10 |
|
| 11 |
from .common import PostProcessor |
| 12 |
import os |
| 13 |
|
| 14 |
|
| 15 |
class ClassifyPP(PostProcessor): |
| 16 |
|
| 17 |
DEFAULT_MAPPING = { |
| 18 |
"Pictures" : ("jpg", "jpeg", "png", "gif", "bmp", "svg", "webp", |
| 19 |
"avif", "heic", "heif", "ico", "psd"), |
| 20 |
"Video" : ("flv", "ogv", "avi", "mp4", "mpg", "mpeg", "3gp", "mkv", |
| 21 |
"webm", "vob", "wmv", "m4v", "mov"), |
| 22 |
"Music" : ("mp3", "aac", "flac", "ogg", "wma", "m4a", "wav"), |
| 23 |
"Archives" : ("zip", "rar", "7z", "tar", "gz", "bz2"), |
| 24 |
"Documents": ("txt", "pdf"), |
| 25 |
} |
| 26 |
|
| 27 |
def __init__(self, job, options): |
| 28 |
PostProcessor.__init__(self, job) |
| 29 |
self.directory = self.realdirectory = "" |
| 30 |
|
| 31 |
mapping = options.get("mapping", self.DEFAULT_MAPPING) |
| 32 |
self.mapping = { |
| 33 |
ext: directory |
| 34 |
for directory, exts in mapping.items() |
| 35 |
for ext in exts |
| 36 |
} |
| 37 |
|
| 38 |
job.register_hooks({ |
| 39 |
"post" : self.initialize, |
| 40 |
"prepare": self.prepare, |
| 41 |
}, options) |
| 42 |
|
| 43 |
def initialize(self, pathfmt): |
| 44 |
|
| 45 |
self.directory = pathfmt.directory |
| 46 |
self.realdirectory = pathfmt.realdirectory |
| 47 |
|
| 48 |
def prepare(self, pathfmt): |
| 49 |
|
| 50 |
ext = pathfmt.extension |
| 51 |
if ext in self.mapping: |
| 52 |
extra = self.mapping[ext] + os.sep |
| 53 |
pathfmt.directory = self.directory + extra |
| 54 |
pathfmt.realdirectory = self.realdirectory + extra |
| 55 |
|
| 56 |
|
| 57 |
__postprocessor__ = ClassifyPP |