[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) ], fmt)
else: else:
key, funcs = parse_field_name(field_name) key, funcs = parse_field_name(field_name)
if key in _GLOBALS: return self._apply(key, funcs, fmt)
return self._apply_globals(_GLOBALS[key], funcs, fmt)
if funcs:
return self._apply(key, funcs, fmt)
return self._apply_simple(key, fmt)
def _apply(self, key, funcs, fmt): def _apply(self, key, funcs, fmt):
def wrap(kwdict): if key in _GLOBALS:
try: def wrap(_):
obj = kwdict[key] try:
for func in funcs: obj = gobj()
obj = func(obj) for func in funcs:
except Exception: obj = func(obj)
obj = self.default except Exception:
return fmt(obj) obj = self.default
return wrap return fmt(obj)
gobj = _GLOBALS[key]
def _apply_globals(self, gobj, funcs, fmt): elif funcs:
def wrap(_): def wrap(kwdict):
try: try:
obj = gobj() obj = kwdict[key]
for func in funcs: for func in funcs:
obj = func(obj) obj = func(obj)
except Exception: except Exception:
obj = self.default obj = self.default
return fmt(obj) return fmt(obj)
return wrap else:
def wrap(kwdict):
def _apply_simple(self, key, fmt): return fmt(kwdict[key] if key in kwdict else self.default)
def wrap(kwdict): del funcs
return fmt(kwdict[key] if key in kwdict else self.default)
return wrap return wrap
def _apply_list(self, lst, fmt): def _apply_list(self, lst, fmt):