| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright 2019-2026 Mike Fährmann |
| 4 |
# |
| 5 |
# This program is free software; you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU General Public License version 2 as |
| 7 |
# published by the Free Software Foundation. |
| 8 |
|
| 9 |
"""Use metadata as file modification time""" |
| 10 |
|
| 11 |
from .common import PostProcessor |
| 12 |
from .. import text, util, dt, formatter |
| 13 |
|
| 14 |
|
| 15 |
class MtimePP(PostProcessor): |
| 16 |
|
| 17 |
def __init__(self, job, options): |
| 18 |
PostProcessor.__init__(self, job) |
| 19 |
|
| 20 |
target = options.get("target") or options.get("mode") |
| 21 |
self._file = (target not in {"dir", "directory"}) |
| 22 |
|
| 23 |
if value := options.get("value"): |
| 24 |
self._get = formatter.parse(value, None, util.identity).format_map |
| 25 |
else: |
| 26 |
key = options.get("key", "date") |
| 27 |
self._get = lambda kwdict: kwdict.get(key) |
| 28 |
|
| 29 |
events = options.get("event") |
| 30 |
if events is None: |
| 31 |
events = ("file",) if self._file else ("post-after",) |
| 32 |
elif isinstance(events, str): |
| 33 |
events = events.split(",") |
| 34 |
job.register_hooks({event: self.run for event in events}, options) |
| 35 |
|
| 36 |
def run(self, pathfmt): |
| 37 |
if mtime := self._get(pathfmt.kwdict): |
| 38 |
if isinstance(mtime, dt.datetime): |
| 39 |
mtime = dt.to_ts(mtime) |
| 40 |
else: |
| 41 |
mtime = text.parse_int(mtime) |
| 42 |
else: |
| 43 |
mtime = None |
| 44 |
|
| 45 |
if self._file: |
| 46 |
pathfmt.kwdict["_mtime_meta"] = mtime |
| 47 |
elif mtime: |
| 48 |
util.set_mtime(pathfmt.realdirectory, mtime) |
| 49 |
|
| 50 |
|
| 51 |
__postprocessor__ = MtimePP |