mirror of
https://github.com/mikf/gallery-dl.git
synced 2024-11-22 02:32:33 +01:00
support accessing the current local datetime in format strings
{_now}, {_now:%Y-%m-%d}, etc (#1968)
This commit is contained in:
parent
dad2875a3e
commit
46e17c5e61
@ -12,12 +12,16 @@ import os
|
||||
import json
|
||||
import string
|
||||
import _string
|
||||
import datetime
|
||||
import operator
|
||||
from . import text, util
|
||||
|
||||
_CACHE = {}
|
||||
_GLOBALS = {"_env": os.environ}
|
||||
_CONVERSIONS = None
|
||||
_GLOBALS = {
|
||||
"_env": lambda: os.environ,
|
||||
"_now": datetime.datetime.now,
|
||||
}
|
||||
|
||||
|
||||
def parse(format_string, default=None):
|
||||
@ -60,7 +64,7 @@ class StringFormatter():
|
||||
- "j". calls json.dumps
|
||||
- "t": calls str.strip
|
||||
- "d": calls text.parse_timestamp
|
||||
- "U": calls urllib.parse.unquote
|
||||
- "U": calls urllib.parse.unescape
|
||||
- "S": calls util.to_string()
|
||||
- "T": calls util.to_timestamü()
|
||||
- Example: {f!l} -> "example"; {f!u} -> "EXAMPLE"
|
||||
@ -147,7 +151,7 @@ class StringFormatter():
|
||||
def _apply_globals(self, gobj, funcs, fmt):
|
||||
def wrap(_):
|
||||
try:
|
||||
obj = gobj
|
||||
obj = gobj()
|
||||
for func in funcs:
|
||||
obj = func(obj)
|
||||
except Exception:
|
||||
@ -164,7 +168,7 @@ class StringFormatter():
|
||||
def wrap(kwdict):
|
||||
for key, funcs in lst:
|
||||
try:
|
||||
obj = _GLOBALS[key] if key in _GLOBALS else kwdict[key]
|
||||
obj = _GLOBALS[key]() if key in _GLOBALS else kwdict[key]
|
||||
for func in funcs:
|
||||
obj = func(obj)
|
||||
if obj:
|
||||
|
@ -173,13 +173,31 @@ class TestFormatter(unittest.TestCase):
|
||||
self._run_test("{d[a]:?</>/L1/too long/}", "<too long>")
|
||||
self._run_test("{d[c]:?</>/L5/too long/}", "")
|
||||
|
||||
def test_environ(self):
|
||||
def test_globals_env(self):
|
||||
os.environ["FORMATTER_TEST"] = value = self.kwdict["a"]
|
||||
|
||||
self._run_test("{_env[FORMATTER_TEST]}" , value)
|
||||
self._run_test("{_env[FORMATTER_TEST]!l}", value.lower())
|
||||
self._run_test("{z|_env[FORMATTER_TEST]}", value)
|
||||
|
||||
def test_globals_now(self):
|
||||
fmt = formatter.parse("{_now}")
|
||||
out1 = fmt.format_map(self.kwdict)
|
||||
self.assertRegex(out1, r"^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d(\.\d+)?$")
|
||||
|
||||
out = formatter.parse("{_now:%Y%m%d}").format_map(self.kwdict)
|
||||
now = datetime.datetime.now()
|
||||
self.assertRegex(out, r"^\d{8}$")
|
||||
self.assertEqual(out, format(now, "%Y%m%d"))
|
||||
|
||||
out = formatter.parse("{z|_now:%Y}").format_map(self.kwdict)
|
||||
self.assertRegex(out, r"^\d{4}$")
|
||||
self.assertEqual(out, format(now, "%Y"))
|
||||
|
||||
out2 = fmt.format_map(self.kwdict)
|
||||
self.assertRegex(out1, r"^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d(\.\d+)?$")
|
||||
self.assertNotEqual(out1, out2)
|
||||
|
||||
def _run_test(self, format_string, result, default=None):
|
||||
fmt = formatter.parse(format_string, default)
|
||||
output = fmt.format_map(self.kwdict)
|
||||
|
Loading…
Reference in New Issue
Block a user