2019-01-05 16:39:05 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2021-01-10 00:10:47 +01:00
|
|
|
# Copyright 2019-2021 Mike Fährmann
|
2019-01-05 16:39:05 +01:00
|
|
|
#
|
|
|
|
# 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
|
|
|
|
# published by the Free Software Foundation.
|
|
|
|
|
2020-11-20 22:28:01 +01:00
|
|
|
"""Write metadata to external files"""
|
2019-01-05 16:39:05 +01:00
|
|
|
|
|
|
|
from .common import PostProcessor
|
2021-09-27 02:37:04 +02:00
|
|
|
from .. import util, formatter
|
2020-01-02 20:58:10 +01:00
|
|
|
import os
|
2019-01-05 16:39:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
class MetadataPP(PostProcessor):
|
|
|
|
|
2020-05-18 01:35:53 +02:00
|
|
|
def __init__(self, job, options):
|
|
|
|
PostProcessor.__init__(self, job)
|
2019-01-17 21:18:12 +01:00
|
|
|
|
|
|
|
mode = options.get("mode", "json")
|
|
|
|
if mode == "custom":
|
|
|
|
self.write = self._write_custom
|
2019-11-30 17:27:49 +01:00
|
|
|
cfmt = options.get("content-format") or options.get("format")
|
2020-10-27 20:09:58 +01:00
|
|
|
if isinstance(cfmt, list):
|
|
|
|
cfmt = "\n".join(cfmt) + "\n"
|
2021-09-27 02:37:04 +02:00
|
|
|
self._content_fmt = formatter.parse(cfmt).format_map
|
2019-11-29 23:12:22 +01:00
|
|
|
ext = "txt"
|
2019-01-17 21:18:12 +01:00
|
|
|
elif mode == "tags":
|
|
|
|
self.write = self._write_tags
|
2019-11-29 23:12:22 +01:00
|
|
|
ext = "txt"
|
2019-01-17 21:18:12 +01:00
|
|
|
else:
|
|
|
|
self.write = self._write_json
|
|
|
|
self.indent = options.get("indent", 4)
|
|
|
|
self.ascii = options.get("ascii", False)
|
|
|
|
ext = "json"
|
|
|
|
|
2020-01-02 20:58:10 +01:00
|
|
|
directory = options.get("directory")
|
|
|
|
if directory:
|
|
|
|
self._directory = self._directory_custom
|
|
|
|
sep = os.sep + (os.altsep or "")
|
2021-02-27 17:19:29 +01:00
|
|
|
self._metadir = util.expand_path(directory).rstrip(sep) + os.sep
|
2020-01-02 20:58:10 +01:00
|
|
|
|
2020-11-20 22:28:01 +01:00
|
|
|
filename = options.get("filename")
|
2019-11-29 23:12:22 +01:00
|
|
|
extfmt = options.get("extension-format")
|
2020-11-20 22:28:01 +01:00
|
|
|
if filename:
|
2020-01-02 20:58:10 +01:00
|
|
|
self._filename = self._filename_custom
|
2021-09-27 02:37:04 +02:00
|
|
|
self._filename_fmt = formatter.parse(filename).format_map
|
2020-11-20 22:28:01 +01:00
|
|
|
elif extfmt:
|
|
|
|
self._filename = self._filename_extfmt
|
2021-09-27 02:37:04 +02:00
|
|
|
self._extension_fmt = formatter.parse(extfmt).format_map
|
2019-11-29 23:12:22 +01:00
|
|
|
else:
|
|
|
|
self.extension = options.get("extension", ext)
|
2019-01-05 16:39:05 +01:00
|
|
|
|
2020-11-20 22:28:01 +01:00
|
|
|
events = options.get("event")
|
|
|
|
if events is None:
|
|
|
|
events = ("file",)
|
|
|
|
elif isinstance(events, str):
|
|
|
|
events = events.split(",")
|
2021-06-04 18:08:08 +02:00
|
|
|
job.register_hooks({event: self.run for event in events}, options)
|
2019-12-16 17:19:23 +01:00
|
|
|
|
2019-01-05 16:39:05 +01:00
|
|
|
def run(self, pathfmt):
|
2020-11-20 22:28:01 +01:00
|
|
|
directory = self._directory(pathfmt)
|
|
|
|
path = directory + self._filename(pathfmt)
|
|
|
|
|
|
|
|
try:
|
|
|
|
with open(path, "w", encoding="utf-8") as fp:
|
|
|
|
self.write(fp, pathfmt.kwdict)
|
|
|
|
except FileNotFoundError:
|
|
|
|
os.makedirs(directory, exist_ok=True)
|
|
|
|
with open(path, "w", encoding="utf-8") as fp:
|
|
|
|
self.write(fp, pathfmt.kwdict)
|
2019-01-17 21:18:12 +01:00
|
|
|
|
2020-01-02 20:58:10 +01:00
|
|
|
def _directory(self, pathfmt):
|
|
|
|
return pathfmt.realdirectory
|
|
|
|
|
|
|
|
def _directory_custom(self, pathfmt):
|
2020-11-20 22:28:01 +01:00
|
|
|
return os.path.join(pathfmt.realdirectory, self._metadir)
|
2020-01-02 20:58:10 +01:00
|
|
|
|
|
|
|
def _filename(self, pathfmt):
|
2020-11-20 22:28:01 +01:00
|
|
|
return (pathfmt.filename or "metadata") + "." + self.extension
|
2019-11-29 23:12:22 +01:00
|
|
|
|
2020-01-02 20:58:10 +01:00
|
|
|
def _filename_custom(self, pathfmt):
|
2021-01-10 00:10:47 +01:00
|
|
|
return pathfmt.clean_path(pathfmt.clean_segment(
|
|
|
|
self._filename_fmt(pathfmt.kwdict)))
|
2020-11-20 22:28:01 +01:00
|
|
|
|
|
|
|
def _filename_extfmt(self, pathfmt):
|
2019-11-29 23:12:22 +01:00
|
|
|
kwdict = pathfmt.kwdict
|
2021-02-10 18:05:46 +01:00
|
|
|
ext = kwdict.get("extension")
|
2019-11-29 23:12:22 +01:00
|
|
|
kwdict["extension"] = pathfmt.extension
|
2020-11-20 22:28:01 +01:00
|
|
|
kwdict["extension"] = pathfmt.prefix + self._extension_fmt(kwdict)
|
2021-06-20 20:11:32 +02:00
|
|
|
filename = pathfmt.build_filename(kwdict)
|
2019-11-29 23:12:22 +01:00
|
|
|
kwdict["extension"] = ext
|
2020-01-02 20:58:10 +01:00
|
|
|
return filename
|
2019-11-29 23:12:22 +01:00
|
|
|
|
2020-11-20 22:28:01 +01:00
|
|
|
def _write_custom(self, fp, kwdict):
|
|
|
|
fp.write(self._content_fmt(kwdict))
|
2019-01-17 21:18:12 +01:00
|
|
|
|
2020-11-20 22:28:01 +01:00
|
|
|
def _write_tags(self, fp, kwdict):
|
2019-08-12 21:40:37 +02:00
|
|
|
tags = kwdict.get("tags") or kwdict.get("tag_string")
|
2019-01-17 21:18:12 +01:00
|
|
|
|
|
|
|
if not tags:
|
|
|
|
return
|
|
|
|
|
2021-06-04 20:58:11 +02:00
|
|
|
if isinstance(tags, str):
|
2019-05-09 22:50:25 +02:00
|
|
|
taglist = tags.split(", ")
|
|
|
|
if len(taglist) < len(tags) / 16:
|
|
|
|
taglist = tags.split(" ")
|
2019-01-17 21:18:12 +01:00
|
|
|
tags = taglist
|
2021-06-04 20:58:11 +02:00
|
|
|
elif isinstance(tags, dict):
|
|
|
|
taglists = tags.values()
|
|
|
|
tags = []
|
|
|
|
extend = tags.extend
|
|
|
|
for taglist in taglists:
|
|
|
|
extend(taglist)
|
2021-06-05 14:49:14 +02:00
|
|
|
tags.sort()
|
2019-01-17 21:18:12 +01:00
|
|
|
|
2020-11-20 22:28:01 +01:00
|
|
|
fp.write("\n".join(tags) + "\n")
|
2019-01-17 21:18:12 +01:00
|
|
|
|
2020-11-20 22:28:01 +01:00
|
|
|
def _write_json(self, fp, kwdict):
|
|
|
|
util.dump_json(util.filter_dict(kwdict), fp, self.ascii, self.indent)
|
2019-01-05 16:39:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
__postprocessor__ = MetadataPP
|