class
PreviewFormatter
pretalx.mail.domain.preview.PreviewFormatter source
Methods 9
Defined here
vformat(format_string, args, kwargs)source
on pretalx.mail.domain.preview.PreviewFormatter source
def vformat(self, format_string, args, kwargs):
result = []
scan_markdown = self.mode == MODE_HTML
state = _STATE_TEXT
for literal_text, field_name, _format_spec, _conversion in self.parse(
format_string
):
result.append(literal_text)
if scan_markdown:
state = _scan_markdown_state(literal_text, state)
if field_name is None:
continue
value = self.format_field(self.get_value(field_name, args, kwargs), "")
result.append(
value if state != _STATE_TEXT else mark_placeholder_value(value)
)
return "".join(result)
def vformat(self, format_string, args, kwargs):
used_args = set()
result, _ = self._vformat(format_string, args, kwargs, used_args, 2)
self.check_unused_args(used_args, args, kwargs)
return result
From SafeFormatter pretalx.common.text.formatting
convert_field(value, conversion)source
on pretalx.common.text.formatting.SafeFormatter source
def convert_field(self, value, conversion):
# Ignore any conversions (``{x!r}``, ``{x!s}``, ``{x!a}``) so
# the output of ``{name}`` and ``{name!r}`` is identical.
return value
def convert_field(self, value, conversion):
# do any conversion on the resulting object
if conversion is None:
return value
elif conversion == 's':
return str(value)
elif conversion == 'r':
return repr(value)
elif conversion == 'a':
return ascii(value)
raise ValueError("Unknown conversion specifier {0!s}".format(conversion))
format_field(value, format_spec)source
on pretalx.common.text.formatting.SafeFormatter source
def format_field(self, value, format_spec):
# Ignore format_spec to block things like ``{x:!r}``.
return super().format_field(self._prepare_value(value), "")
def format_field(self, value, format_spec):
return format(value, format_spec)
get_field(field_name, args, kwargs)source
on pretalx.common.text.formatting.SafeFormatter source
def get_field(self, field_name, args, kwargs):
return self.get_value(field_name, args, kwargs), field_name
# given a field_name, find the object it references. # field_name: the field being looked up, e.g. "0.name" # or "lookup[3]" # used_args: a set of which args have been used # args, kwargs: as passed in to vformat
def get_field(self, field_name, args, kwargs):
first, rest = _string.formatter_field_name_split(field_name)
obj = self.get_value(first, args, kwargs)
# loop through the rest of the field_name, doing
# getattr or getitem as needed
for is_attr, i in rest:
if is_attr:
obj = getattr(obj, i)
else:
obj = obj[i]
return obj, first
get_value(key, args, kwargs)source
on pretalx.common.text.formatting.SafeFormatter source
def get_value(self, key, args, kwargs):
if not self.raise_on_missing and key not in self.context:
return "{" + str(key) + "}"
return self.context[key]
def get_value(self, key, args, kwargs):
if isinstance(key, int):
return args[key]
else:
return kwargs[key]
__init__(context, raise_on_missing=True, mode=1)sourceInitialize self.
See help(type(self)) for accurate signature.
def __init__(self, context, raise_on_missing=True, mode=MODE_PLAIN):
self.context = context
self.raise_on_missing = raise_on_missing
self.mode = mode