add 'python' post processor

similar to 'exec' but calls a Python function
pull/4403/head
Mike Fährmann 1 year ago
parent 1baf83a9e5
commit 54d974deb0
No known key found for this signature in database
GPG Key ID: 5680CA389D365A88

@ -4748,6 +4748,50 @@ Description
|datetime|_ object.
python.archive
--------------
Type
|Path|_
Description
File to store IDs of called Python functions in,
similar to `extractor.*.archive`_.
``archive-format``, ``archive-prefix``, and ``archive-pragma`` options,
akin to
`extractor.*.archive-format`_,
`extractor.*.archive-prefix`_, and
`extractor.*.archive-pragma`_, are supported as well.
python.event
------------
Type
``string``
Default
``"file"``
Description
The event for which `python.function`_ gets called.
See `metadata.event`_ for a list of available events.
python.function
---------------
Type
``string``
Example
* ``"my_module:generate_text"``
* ``"~/.local/share/gdl-utils.py:resize"``
Description
The Python function to call.
This function gets specified as ``<module>:<function name>``
and gets called with the current metadata dict as argument.
``module`` is either an importable Python module name
or the |Path|_ to a `.py` file,
ugoira.extension
----------------
Type
@ -5345,6 +5389,8 @@ Description
Write metadata to separate files
``mtime``
Set file modification time according to its metadata
``python``
Call Python functions
``ugoira``
Convert Pixiv Ugoira to WebM using `FFmpeg <https://www.ffmpeg.org/>`__
``zip``

@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright 2018-2021 Mike Fährmann
# Copyright 2018-2023 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
@ -14,6 +14,7 @@ modules = [
"exec",
"metadata",
"mtime",
"python",
"ugoira",
"zip",
]

@ -0,0 +1,47 @@
# -*- coding: utf-8 -*-
# Copyright 2023 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.
"""Run Python functions"""
from .common import PostProcessor
from .. import util
class PythonPP(PostProcessor):
def __init__(self, job, options):
PostProcessor.__init__(self, job)
spec = options["function"]
module_name, _, function_name = spec.rpartition(":")
module = util.import_file(module_name)
self.function = getattr(module, function_name)
self._init_archive(job, options)
if self.archive:
self.run = self.run_archive
events = options.get("event")
if events is None:
events = ("file",)
elif isinstance(events, str):
events = events.split(",")
job.register_hooks({event: self.run for event in events}, options)
def run(self, pathfmt):
self.function(pathfmt.kwdict)
def run_archive(self, pathfmt):
kwdict = pathfmt.kwdict
if self.archive.check(kwdict):
return
self.function(kwdict)
self.archive.add(kwdict)
__postprocessor__ = PythonPP
Loading…
Cancel
Save