[formatter] update format string type lookup
use a dict instead of if/elif/else
This commit is contained in:
@@ -28,25 +28,17 @@ def parse(format_string, default=NONE, fmt=format):
|
|||||||
except KeyError:
|
except KeyError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
cls = StringFormatter
|
|
||||||
if format_string and format_string[0] == "\f":
|
if format_string and format_string[0] == "\f":
|
||||||
kind, _, format_string = format_string.partition(" ")
|
kind, _, format_string = format_string.partition(" ")
|
||||||
kind = kind[1:]
|
try:
|
||||||
|
cls = _FORMATTERS[kind[1:]]
|
||||||
if kind == "T":
|
except KeyError:
|
||||||
cls = TemplateFormatter
|
import logging
|
||||||
elif kind == "TF":
|
logging.getLogger("formatter").error(
|
||||||
cls = TemplateFStringFormatter
|
"Invalid formatter type '%s'", kind[1:])
|
||||||
elif kind == "TJ":
|
cls = StringFormatter
|
||||||
cls = TemplateJinjaFormatter
|
else:
|
||||||
elif kind == "E":
|
cls = StringFormatter
|
||||||
cls = ExpressionFormatter
|
|
||||||
elif kind == "J":
|
|
||||||
cls = JinjaFormatter
|
|
||||||
elif kind == "F":
|
|
||||||
cls = FStringFormatter
|
|
||||||
elif kind == "M":
|
|
||||||
cls = ModuleFormatter
|
|
||||||
|
|
||||||
formatter = _CACHE[key] = cls(format_string, default, fmt)
|
formatter = _CACHE[key] = cls(format_string, default, fmt)
|
||||||
return formatter
|
return formatter
|
||||||
@@ -212,15 +204,6 @@ class ExpressionFormatter():
|
|||||||
self.format_map = util.compile_expression(expression)
|
self.format_map = util.compile_expression(expression)
|
||||||
|
|
||||||
|
|
||||||
class ModuleFormatter():
|
|
||||||
"""Generate text by calling an external function"""
|
|
||||||
|
|
||||||
def __init__(self, function_spec, default=NONE, fmt=None):
|
|
||||||
module_name, _, function_name = function_spec.rpartition(":")
|
|
||||||
module = util.import_file(module_name)
|
|
||||||
self.format_map = getattr(module, function_name)
|
|
||||||
|
|
||||||
|
|
||||||
class FStringFormatter():
|
class FStringFormatter():
|
||||||
"""Generate text by evaluating an f-string literal"""
|
"""Generate text by evaluating an f-string literal"""
|
||||||
|
|
||||||
@@ -239,6 +222,15 @@ class JinjaFormatter():
|
|||||||
self.format_map = self.env.from_string(source).render
|
self.format_map = self.env.from_string(source).render
|
||||||
|
|
||||||
|
|
||||||
|
class ModuleFormatter():
|
||||||
|
"""Generate text by calling an external function"""
|
||||||
|
|
||||||
|
def __init__(self, function_spec, default=NONE, fmt=None):
|
||||||
|
module_name, _, function_name = function_spec.rpartition(":")
|
||||||
|
module = util.import_file(module_name)
|
||||||
|
self.format_map = getattr(module, function_name)
|
||||||
|
|
||||||
|
|
||||||
class TemplateFormatter(StringFormatter):
|
class TemplateFormatter(StringFormatter):
|
||||||
"""Read format_string from file"""
|
"""Read format_string from file"""
|
||||||
|
|
||||||
@@ -516,6 +508,18 @@ _literal = Literal()
|
|||||||
|
|
||||||
_CACHE = {}
|
_CACHE = {}
|
||||||
_SEPARATOR = "/"
|
_SEPARATOR = "/"
|
||||||
|
_FORMATTERS = {
|
||||||
|
"E" : ExpressionFormatter,
|
||||||
|
"F" : FStringFormatter,
|
||||||
|
"J" : JinjaFormatter,
|
||||||
|
"M" : ModuleFormatter,
|
||||||
|
"S" : StringFormatter,
|
||||||
|
"T" : TemplateFormatter,
|
||||||
|
"TF": TemplateFStringFormatter,
|
||||||
|
"FT": TemplateFStringFormatter,
|
||||||
|
"TJ": TemplateJinjaFormatter,
|
||||||
|
"JT": TemplateJinjaFormatter,
|
||||||
|
}
|
||||||
_GLOBALS = {
|
_GLOBALS = {
|
||||||
"_env": lambda: os.environ,
|
"_env": lambda: os.environ,
|
||||||
"_lit": lambda: _literal,
|
"_lit": lambda: _literal,
|
||||||
|
|||||||
Reference in New Issue
Block a user