Module pyboiler.internal.color.parse

Functions

def escape(obj)

Classes

class Formatter (fmt, *, defaults=None)
Expand source code
class Formatter:
    _tag = re.compile(r"\\?</?((?:[fb]g\s)?[^<>\s]*)>", re.I)

    def __init__(self, fmt, *, defaults=None):
        self._fmt = fmt or self.default_format
        self._defaults = defaults

    def validate(self):
        """Validate the input format, ensure it matches the correct style"""
        if not self._tag.search(self._fmt):
            raise ValueError(f"Invalid format '{self._fmt}'")

    def _format(self, record):
        if defaults := self._defaults:
            values = defaults | record.__dict__
        else:
            values = record.__dict__
        return self._fmt % values

    def format(self, record):
        try:
            return self._format(record)
        except KeyError as e:
            raise ValueError("Formatting field not found in record: %s" % e)

Methods

def format(self, record)
def validate(self)

Validate the input format, ensure it matches the correct style