2019-01-05 16:39:05 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2020-05-18 01:35:53 +02:00
|
|
|
# Copyright 2019-2020 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.
|
|
|
|
|
|
|
|
"""Write metadata to JSON files"""
|
|
|
|
|
|
|
|
from .common import PostProcessor
|
2019-01-17 21:18:12 +01:00
|
|
|
from .. import util
|
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"
|
2019-11-30 17:27:49 +01:00
|
|
|
self.contentfmt = util.Formatter(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 "")
|
|
|
|
self.metadir = directory.rstrip(sep) + os.sep
|
|
|
|
|
2019-11-29 23:12:22 +01:00
|
|
|
extfmt = options.get("extension-format")
|
|
|
|
if extfmt:
|
2020-01-02 20:58:10 +01:00
|
|
|
self._filename = self._filename_custom
|
2019-11-29 23:12:22 +01:00
|
|
|
self.extfmt = util.Formatter(extfmt).format_map
|
|
|
|
else:
|
|
|
|
self.extension = options.get("extension", ext)
|
2019-01-05 16:39:05 +01:00
|
|
|
|
2020-11-18 17:11:55 +01:00
|
|
|
event = "metadata" if options.get("bypost") else "file"
|
|
|
|
job.hooks[event].append(self.run)
|
2019-12-16 17:19:23 +01:00
|
|
|
|
2019-01-05 16:39:05 +01:00
|
|
|
def run(self, pathfmt):
|
2020-01-02 20:58:10 +01:00
|
|
|
path = self._directory(pathfmt) + self._filename(pathfmt)
|
|
|
|
with open(path, "w", encoding="utf-8") as file:
|
2019-08-12 21:40:37 +02:00
|
|
|
self.write(file, 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):
|
|
|
|
directory = os.path.join(pathfmt.realdirectory, self.metadir)
|
|
|
|
os.makedirs(directory, exist_ok=True)
|
|
|
|
return directory
|
|
|
|
|
|
|
|
def _filename(self, pathfmt):
|
|
|
|
return pathfmt.filename + "." + self.extension
|
2019-11-29 23:12:22 +01:00
|
|
|
|
2020-01-02 20:58:10 +01:00
|
|
|
def _filename_custom(self, pathfmt):
|
2019-11-29 23:12:22 +01:00
|
|
|
kwdict = pathfmt.kwdict
|
|
|
|
ext = kwdict["extension"]
|
|
|
|
kwdict["extension"] = pathfmt.extension
|
|
|
|
kwdict["extension"] = pathfmt.prefix + self.extfmt(kwdict)
|
2020-01-02 20:58:10 +01:00
|
|
|
filename = pathfmt.build_filename()
|
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
|
|
|
|
2019-08-12 21:40:37 +02:00
|
|
|
def _write_custom(self, file, kwdict):
|
2019-11-29 23:12:22 +01:00
|
|
|
file.write(self.contentfmt(kwdict))
|
2019-01-17 21:18:12 +01:00
|
|
|
|
2019-08-12 21:40:37 +02:00
|
|
|
def _write_tags(self, file, kwdict):
|
|
|
|
tags = kwdict.get("tags") or kwdict.get("tag_string")
|
2019-01-17 21:18:12 +01:00
|
|
|
|
|
|
|
if not tags:
|
|
|
|
return
|
|
|
|
|
|
|
|
if not isinstance(tags, list):
|
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
|
|
|
|
|
|
|
|
file.write("\n".join(tags))
|
|
|
|
file.write("\n")
|
|
|
|
|
2019-08-12 21:40:37 +02:00
|
|
|
def _write_json(self, file, kwdict):
|
2019-11-21 16:57:39 +01:00
|
|
|
util.dump_json(util.filter_dict(kwdict), file, self.ascii, self.indent)
|
2019-01-05 16:39:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
__postprocessor__ = MetadataPP
|