sqlmesh.utils.yaml API documentation

Edit on GitHub

# sqlmesh.utils.yaml

View Source

     1from __future__ import annotations
     2
     3import io
     4import typing as t
     5from collections import OrderedDict
     6from os import getenv
     7from pathlib import Path
     8
     9from ruamel.yaml import YAML, CommentedMap
    10
    11from sqlmesh.utils.errors import SQLMeshError
    12from sqlmesh.utils.jinja import ENVIRONMENT
    13
    14JINJA_METHODS = {
    15    "env_var": lambda key, default=None: getenv(key, default),
    16}
    17
    18
    19def load(
    20    source: str | Path, raise_if_empty: bool = True, render_jinja: bool = True
    21) -> t.OrderedDict:
    22    """Loads a YAML object from either a raw string or a file."""
    23    path: t.Optional[Path] = None
    24
    25    if isinstance(source, Path):
    26        path = source
    27        with open(source, "r", encoding="utf-8") as file:
    28            source = file.read()
    29
    30    if render_jinja:
    31        source = ENVIRONMENT.from_string(source).render(JINJA_METHODS)
    32
    33    contents = YAML().load(source)
    34    if contents is None:
    35        if raise_if_empty:
    36            error_path = f" '{path}'" if path else ""
    37            raise SQLMeshError(f"YAML source{error_path} can't be empty.")
    38        return OrderedDict()
    39
    40    return contents
    41
    42
    43def dumps(value: CommentedMap | OrderedDict) -> str:
    44    """Dumps a ruamel.yaml loaded object and converts it into a string"""
    45    result = io.StringIO()
    46    YAML().dump(value, result)
    47    return result.getvalue()

def load( source: str | pathlib.Path, raise_if_empty: bool = True, render_jinja: bool = True) -> OrderedDict: View Source

    20def load(
    21    source: str | Path, raise_if_empty: bool = True, render_jinja: bool = True
    22) -> t.OrderedDict:
    23    """Loads a YAML object from either a raw string or a file."""
    24    path: t.Optional[Path] = None
    25
    26    if isinstance(source, Path):
    27        path = source
    28        with open(source, "r", encoding="utf-8") as file:
    29            source = file.read()
    30
    31    if render_jinja:
    32        source = ENVIRONMENT.from_string(source).render(JINJA_METHODS)
    33
    34    contents = YAML().load(source)
    35    if contents is None:
    36        if raise_if_empty:
    37            error_path = f" '{path}'" if path else ""
    38            raise SQLMeshError(f"YAML source{error_path} can't be empty.")
    39        return OrderedDict()
    40
    41    return contents

Loads a YAML object from either a raw string or a file.

def dumps( value: ruamel.yaml.comments.CommentedMap | collections.OrderedDict) -> str: View Source

    44def dumps(value: CommentedMap | OrderedDict) -> str:
    45    """Dumps a ruamel.yaml loaded object and converts it into a string"""
    46    result = io.StringIO()
    47    YAML().dump(value, result)
    48    return result.getvalue()

Dumps a ruamel.yaml loaded object and converts it into a string