[formatter] combine '_apply_*' methods

This commit is contained in:
Mike Fährmann
2026-01-11 18:09:59 +01:00
parent a79a945494
commit 25397903ce

View File

@@ -142,37 +142,32 @@ class StringFormatter():
], fmt)
else:
key, funcs = parse_field_name(field_name)
if key in _GLOBALS:
return self._apply_globals(_GLOBALS[key], funcs, fmt)
if funcs:
return self._apply(key, funcs, fmt)
return self._apply_simple(key, fmt)
return self._apply(key, funcs, fmt)
def _apply(self, key, funcs, fmt):
def wrap(kwdict):
try:
obj = kwdict[key]
for func in funcs:
obj = func(obj)
except Exception:
obj = self.default
return fmt(obj)
return wrap
def _apply_globals(self, gobj, funcs, fmt):
def wrap(_):
try:
obj = gobj()
for func in funcs:
obj = func(obj)
except Exception:
obj = self.default
return fmt(obj)
return wrap
def _apply_simple(self, key, fmt):
def wrap(kwdict):
return fmt(kwdict[key] if key in kwdict else self.default)
if key in _GLOBALS:
def wrap(_):
try:
obj = gobj()
for func in funcs:
obj = func(obj)
except Exception:
obj = self.default
return fmt(obj)
gobj = _GLOBALS[key]
elif funcs:
def wrap(kwdict):
try:
obj = kwdict[key]
for func in funcs:
obj = func(obj)
except Exception:
obj = self.default
return fmt(obj)
else:
def wrap(kwdict):
return fmt(kwdict[key] if key in kwdict else self.default)
del funcs
return wrap
def _apply_list(self, lst, fmt):