Skip to content

context

FormatArgs(prefix)

Data structure for recording values in jinja blocks to be formatted by psycopg

Parameters:

  • prefix (str) –

    Prefix used in dictionary keys

Source code in jinja_psycopg/context.py
def __init__(self, prefix: str) -> None:
    """Data structure for recording values in jinja blocks to be formatted by psycopg

    Args:
        prefix: Prefix used in dictionary keys
    """

    self._prefix = prefix
    self._dictionary = {}
    self._num_values = 0

dictionary: dict property

Returns:

  • dict

    saved values

save_value(value)

Parameters:

  • value (Any) –

    value to save

Returns:

  • str

    generated key in the format of prefix#number

Source code in jinja_psycopg/context.py
def save_value(self, value: Any) -> str:
    """
    Args:
        value: value to save

    Returns:
        generated key in the format of `prefix#number`
    """
    key = f"{self._prefix}#{self._num_values}"
    self._dictionary[key] = value

    self._num_values += 1
    return key

FormatArgsContext(name)

Wrapper for contextvars.ContextVar used for saving format args from within psycopg filter and for creating argument recorders

Parameters:

  • name (str) –

    name used by the ContextVar

Source code in jinja_psycopg/context.py
def __init__(self, name: str) -> None:
    """Wrapper for [contextvars.ContextVar][] used for saving format args from within
        [`psycopg`][jinja_psycopg.renderer.psycopg_filter] filter and for creating argument recorders

    Args:
        name: name used by the ContextVar
    """
    self._context_var = ContextVar[Optional[FormatArgs]](name, default=None)

save_value(value)

Parameters:

  • value (Any) –

    value to save

Returns:

  • str

    generated key in the format of prefix#number

Raises:

Source code in jinja_psycopg/context.py
def save_value(self, value: Any) -> str:
    """
    Args:
        value: value to save

    Returns:
        generated key in the format of `prefix#number`

    Raises:
        RuntimeError: if ContextVar was empty
    """

    context = self._context_var.get()
    if context is None:
        raise RuntimeError(
            "Called ContextWriter.save_value, but no context was found"
        )

    return context.save_value(value)

recorder(prefix)

Parameters:

  • prefix (str) –

    Prefix for the keys in the resulting dictionary

Returns:

Source code in jinja_psycopg/context.py
def recorder(self, prefix: str) -> FormatArgsRecorder:
    """
    Args:
        prefix: Prefix for the keys in the resulting dictionary

    Returns:
        new recorder with the given prefix
    """
    return FormatArgsRecorder(self._context_var, prefix)

FormatArgsRecorder(context_var, prefix)

contextvars.ContextVar wrapper that works as a context manager and records arguments saved within its scope into a dictionary

Parameters:

Source code in jinja_psycopg/context.py
def __init__(
    self, context_var: ContextVar[Optional[FormatArgs]], prefix: str
) -> None:
    """[contextvars.ContextVar][] wrapper that works as a context manager
    and records arguments saved within its scope into a dictionary

    Args:
        context_var: Inner ContextVar
        prefix: Prefix used in dictionary keys
    """

    self._context_var = context_var
    self._prefix = prefix
    self._recorded = None

unwrap()

Returns:

Raises:

Source code in jinja_psycopg/context.py
def unwrap(self) -> dict[str, Any]:
    """
    Returns:
        the recorded arguments

    Raises:
        RuntimeError: if nothing was recorded
    """
    if self._recorded is None:
        raise RuntimeError(
            "Called ContextDictRecorder.unwrap(), but nothing was recorded"
        )

    return self._recorded