Skip to content

diff

dev_tool.tools.diff

DiffResult

Bases: TypedDict

added instance-attribute

removed instance-attribute

modified instance-attribute

EnvironmentDiff

A class for comparing and displaying environment variable differences.

This class provides static methods for parsing, comparing, and displaying differences between local and remote environment configurations.

compare staticmethod

A method that compares local and remote environment variables.

Parameters:

  • local (dict[str, str]) –

    The local environment variables.

  • remote (dict[str, str]) –

    The remote environment variables.

Returns:

  • DiffResult

    A dictionary with added, removed, and modified variables.

Source code in dev_tool/tools/diff.py
@staticmethod
def compare(local: dict[str, str], remote: dict[str, str]) -> DiffResult:
    """
    A method that compares local and remote environment variables.

    :param local: The local environment variables.
    :param remote: The remote environment variables.
    :return: A dictionary with added, removed, and modified variables.
    """

    added = {k: v for k, v in remote.items() if k not in local}
    removed = {k: v for k, v in local.items() if k not in remote}
    modified = {k: {'local': local[k], 'remote': remote[k]} for k in local if k in remote and local[k] != remote[k]}

    return {
        'added': added,
        'removed': removed,
        'modified': modified
    }

display staticmethod

A method that displays differences between local and remote environment variables.

Parameters:

  • terminal (Terminal) –

    The blessed Terminal instance.

  • local (dict[str, str]) –

    The local environment variables.

  • remote (dict[str, str]) –

    The remote environment variables.

Source code in dev_tool/tools/diff.py
@staticmethod
def display(terminal: Terminal, local: dict[str, str], remote: dict[str, str]) -> None:
    """
    A method that displays differences between local and remote environment variables.

    :param terminal: The blessed Terminal instance.
    :param local: The local environment variables.
    :param remote: The remote environment variables.
    """

    differences = EnvironmentDiff.compare(local, remote)

    if not any(differences.values()):
        print('No differences found between local and remote environment variables.')  # noqa: T201
        return

    print('\n' + terminal.bold('Environment Variable Changes:') + '\n')  # noqa: T201

    if differences['added']:
        print(terminal.color_rgb(40, 167, 69)('Added variables:'))  # noqa: T201

        for key, value in sorted(differences['added'].items()):
            truncated = value[:60] + '...' if len(value) > 60 else value
            print(terminal.color_rgb(40, 167, 69)(f'  + {key} = {truncated}'))  # noqa: T201

        print()  # noqa: T201

    if differences['removed']:
        print(terminal.color_rgb(220, 53, 69)('Removed variables:'))  # noqa: T201

        for key in sorted(differences['removed'].keys()):
            print(terminal.color_rgb(220, 53, 69)(f'  - {key}'))  # noqa: T201

        print()  # noqa: T201

    if differences['modified']:
        print(terminal.color_rgb(255, 193, 7)('Modified variables:'))  # noqa: T201

        for key, modification in sorted(differences['modified'].items()):
            local_value = modification['local'][:40] + '...' if len(modification['local']) > 40 else modification['local']
            remote_value = modification['remote'][:40] + '...' if len(modification['remote']) > 40 else modification['remote']

            print(terminal.color_rgb(255, 193, 7)(f'  ~ {key}'))  # noqa: T201
            print(f'      Local:  {local_value}')  # noqa: T201
            print(f'      Remote: {remote_value}')  # noqa: T201

        print()  # noqa: T201

has_differences staticmethod

A method that checks if there are differences between environments.

Parameters:

  • local (dict[str, str]) –

    The local environment variables.

  • remote (dict[str, str]) –

    The remote environment variables.

Returns:

  • bool

    True if differences exist, False otherwise.

Source code in dev_tool/tools/diff.py
@staticmethod
def has_differences(local: dict[str, str], remote: dict[str, str]) -> bool:
    """
    A method that checks if there are differences between environments.

    :param local: The local environment variables.
    :param remote: The remote environment variables.
    :return: True if differences exist, False otherwise.
    """

    differences = EnvironmentDiff.compare(local, remote)
    return any(differences.values())

parse_file staticmethod

A method that parses an environment file into a dictionary.

Parameters:

  • path (Path) –

    The path to the environment file.

Returns:

  • dict[str, str]

    A dictionary of environment variables.

Source code in dev_tool/tools/diff.py
@staticmethod
def parse_file(path: Path) -> dict[str, str]:
    """
    A method that parses an environment file into a dictionary.

    :param path: The path to the environment file.
    :return: A dictionary of environment variables.
    """

    variables = {}

    if not path.exists():
        return variables

    with open(path, 'r', encoding='utf-8') as handle:
        for line in handle:
            line = line.strip()

            if not line or line.startswith('#') or '=' not in line:
                continue

            key, value = line.split('=', 1)
            key = key.strip()
            value = value.strip()

            if key:
                variables[key] = value

    return variables

parse_string staticmethod

A method that parses environment variable values from a string.

Parameters:

  • values (str) –

    The environment values string with newline-separated key=value pairs.

Returns:

  • dict[str, str]

    A dictionary of environment variables.

Source code in dev_tool/tools/diff.py
@staticmethod
def parse_string(values: str) -> dict[str, str]:
    """
    A method that parses environment variable values from a string.

    :param values: The environment values string with newline-separated key=value pairs.
    :return: A dictionary of environment variables.
    """

    variables = {}

    for line in values.splitlines():
        line = line.strip()

        if not line or line.startswith('#') or '=' not in line:
            continue

        key, value = line.split('=', 1)
        key = key.strip()
        value = value.strip()

        if key:
            variables[key] = value

    return variables