Skip to content

format_fos

Full documentation pages are generated for docstring reference only and may contain symbols imported from other modules. Imported symbols are not distinguished from locally defined symbols and will appear in any module that they are imported into. For better information on where symbols should be imported from, review the sourcecode on the github.

FoSpy.parsing.format_fos

SYNTAX module-attribute

SYNTAX = {
    "block_header": {
        "single": {"open": "[", "close": "]"},
        "list": {"open": "[[", "close": "]]"},
    },
    "nested": {"open": "[", "close": "]"},
    "embedded": {
        "open": "{{{",
        "close": "END FOS EMBED }}}",
        "prefix": "#",
    },
    "key_value": {
        "delimiter": ":",
        "require_value": True,
        "prefix": False,
    },
    "comment": {"prefix": "//", "allow_leading_ws": True},
    "calc_comment": {
        "prefix": "!",
        "allow_leading_ws": True,
    },
    "indent_size": 4,
}

_debug module-attribute

_debug = Debug()

Debug

Source code in FoSpy/_debug.py
class Debug:
    def __init__(self):
        self.on = False

        frame = inspect.currentframe().f_back
        self.module_name = frame.f_globals.get("__name__", "<unknown>")
        self.label = f"|(Debug message from {self.module_name})"
        self.label_width = len(self.label)

    def _get_text_width(self, module=None):
        if module:
            label = f"|(Debug message from {module} via {self.module_name})"
            label_width = len(label)
        else:
            label = self.label
            label_width = self.label_width

        text_width = DEBUG_WIDTH - label_width
        return text_width, label, label_width


    def msg(self,msg, module=None):
        if not self.on:
            return

        text_width, label, label_width = self._get_text_width(module)

        wrapped = textwrap.fill(str(msg), width=text_width)

        for line in wrapped.splitlines():
            print(f'{line:<{text_width}}{label:>{label_width}}')

    def pmsg(self,msg,module=None,**kwargs):
        if not self.on:
            return

        text_width, label, label_width = self._get_text_width(module)

        buf = io.StringIO()
        pprint(msg,stream=buf, width=text_width,**kwargs)
        txt = buf.getvalue()
        for line in txt.splitlines():
            print(f'{line:<{text_width}}{label:>{label_width}}')

label instance-attribute

label = f'|(Debug message from {self.module_name})'

label_width instance-attribute

label_width = len(self.label)

module_name instance-attribute

module_name = frame.f_globals.get('__name__', '<unknown>')

on instance-attribute

on = False

__init__

__init__()
Source code in FoSpy/_debug.py
def __init__(self):
    self.on = False

    frame = inspect.currentframe().f_back
    self.module_name = frame.f_globals.get("__name__", "<unknown>")
    self.label = f"|(Debug message from {self.module_name})"
    self.label_width = len(self.label)

_get_text_width

_get_text_width(module=None)
Source code in FoSpy/_debug.py
def _get_text_width(self, module=None):
    if module:
        label = f"|(Debug message from {module} via {self.module_name})"
        label_width = len(label)
    else:
        label = self.label
        label_width = self.label_width

    text_width = DEBUG_WIDTH - label_width
    return text_width, label, label_width

msg

msg(msg, module=None)
Source code in FoSpy/_debug.py
def msg(self,msg, module=None):
    if not self.on:
        return

    text_width, label, label_width = self._get_text_width(module)

    wrapped = textwrap.fill(str(msg), width=text_width)

    for line in wrapped.splitlines():
        print(f'{line:<{text_width}}{label:>{label_width}}')

pmsg

pmsg(msg, module=None, **kwargs)
Source code in FoSpy/_debug.py
def pmsg(self,msg,module=None,**kwargs):
    if not self.on:
        return

    text_width, label, label_width = self._get_text_width(module)

    buf = io.StringIO()
    pprint(msg,stream=buf, width=text_width,**kwargs)
    txt = buf.getvalue()
    for line in txt.splitlines():
        print(f'{line:<{text_width}}{label:>{label_width}}')

_indent

_indent(st, ind)
Source code in FoSpy/parsing/format_fos.py
def _indent(st:str, ind):
    return f"{' '*ind*SYNTAX["indent_size"]}{st}"

empty_nested

empty_nested(is_list)
Source code in FoSpy/parsing/format_fos.py
def empty_nested(is_list):
    spec = SYNTAX["nested"]
    open_br = spec["open"]
    opn = open_br * (2 if is_list else 1)
    close_br = spec["close"]
    close = close_br *(2 if is_list else 1)

    return f"{opn}{close}"

format_block_header

format_block_header(name, list_type)
Source code in FoSpy/parsing/format_fos.py
def format_block_header(name: str, list_type: str):
    spec = SYNTAX["block_header"]
    if list_type == "list":
        return f"{spec['list']['open']}{name.capitalize()}{spec['list']['close']}"
    else:
        return f"{spec['single']['open']}{name.capitalize()}{spec['single']['close']}"

format_calc_comment

format_calc_comment(text)
Source code in FoSpy/parsing/format_fos.py
def format_calc_comment(text:str):
    spec = SYNTAX["calc_comment"]
    prefix = spec["prefix"]
    return f"{prefix} {text}"

format_comment

format_comment(text, ind=0)
Source code in FoSpy/parsing/format_fos.py
def format_comment(text: str, ind: int = 0):
    spec = SYNTAX["comment"]
    prefix = spec["prefix"]
    return _indent(f"{prefix} {text}", ind)

format_embed_start

format_embed_start(key, ind=0, looped=False)
Source code in FoSpy/parsing/format_fos.py
def format_embed_start(key:str, ind=0, looped=False):
    prefix = SYNTAX["key_value"].get("prefix")
    delim = SYNTAX["key_value"]["delimiter"]
    if looped:
        return _indent(SYNTAX["embedded"]["open"], ind)

    if prefix:
        key = f"{prefix}{key}"
    return _indent(f"{key}{delim}{"" if " " in delim else " "}{SYNTAX["embedded"]["open"]}", ind)

format_field

format_field(label)
Source code in FoSpy/parsing/format_fos.py
def format_field(label:str):
    return f"<!{label.upper()}-FIELD>"

format_key_value

format_key_value(key, val, ind=0, looped=False)
Source code in FoSpy/parsing/format_fos.py
def format_key_value(key: str, val: str, ind=0, looped=False):
    spec = SYNTAX["key_value"]
    delim = spec["delimiter"]
    prefix = spec.get("prefix")
    if looped:
        return _indent(val, ind)

    if prefix:
        key = f"{prefix}{key}"

    return _indent(f"{key}{delim}{"" if " " in delim else " "}{val}",ind)

format_loop_key

format_loop_key(key, ind=0)
Source code in FoSpy/parsing/format_fos.py
def format_loop_key(key: str, ind=0):
    spec = SYNTAX["key_value"]
    prefix = spec.get("prefix")
    delim  = spec["delimiter"]

    if prefix:
        # key_
        return _indent(f"{key}{prefix}",ind)
    else:
        # :key
        return _indent(f"{delim}{key}",ind)

format_nested_end

format_nested_end(is_list, ind=0)
Source code in FoSpy/parsing/format_fos.py
def format_nested_end(is_list, ind=0):
    spec = SYNTAX["nested"]
    close_br = spec["close"]
    close = close_br *(2 if is_list else 1)
    return _indent(close, ind)

format_nested_start

format_nested_start(key, is_list, looped=False, ind=0)
Source code in FoSpy/parsing/format_fos.py
def format_nested_start(key, is_list: bool, looped=False, ind=0):
    spec = SYNTAX["nested"]
    open_br = spec["open"]

    val = open_br * (2 if is_list else 1)

    return format_key_value(key, val, ind, looped)