support accessing the current local datetime in format strings

{_now}, {_now:%Y-%m-%d}, etc
(#1968)
This commit is contained in:
Mike Fährmann
2021-10-30 21:34:28 +02:00
parent dad2875a3e
commit 46e17c5e61
2 changed files with 27 additions and 5 deletions

View File

@@ -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: