2019-01-05 16:39:05 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright 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
|
|
|
|
# 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
|
2019-01-05 16:39:05 +01:00
|
|
|
|
|
|
|
|
|
|
|
class MetadataPP(PostProcessor):
|
|
|
|
|
|
|
|
def __init__(self, pathfmt, options):
|
|
|
|
PostProcessor.__init__(self)
|
2019-01-17 21:18:12 +01:00
|
|
|
|
|
|
|
mode = options.get("mode", "json")
|
|
|
|
ext = "txt"
|
|
|
|
|
|
|
|
if mode == "custom":
|
|
|
|
self.write = self._write_custom
|
|
|
|
self.formatter = util.Formatter(options.get("format"))
|
|
|
|
elif mode == "tags":
|
|
|
|
self.write = self._write_tags
|
|
|
|
else:
|
|
|
|
self.write = self._write_json
|
|
|
|
self.indent = options.get("indent", 4)
|
|
|
|
self.ascii = options.get("ascii", False)
|
|
|
|
ext = "json"
|
|
|
|
|
|
|
|
self.extension = options.get("extension", ext)
|
2019-01-05 16:39:05 +01:00
|
|
|
|
|
|
|
def run(self, pathfmt):
|
2019-01-17 21:18:12 +01:00
|
|
|
path = "{}.{}".format(pathfmt.realpath, self.extension)
|
|
|
|
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
|
|
|
|
2019-08-12 21:40:37 +02:00
|
|
|
def _write_custom(self, file, kwdict):
|
|
|
|
output = self.formatter.format_map(kwdict)
|
2019-01-17 21:18:12 +01:00
|
|
|
file.write(output)
|
|
|
|
|
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
|