Skip to content

Commit

Permalink
Delete ignored keys from diff (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
disrupted authored Jul 9, 2024
1 parent a61de1b commit ae894d1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
28 changes: 24 additions & 4 deletions kpops/utils/dict_differ.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from __future__ import annotations

from collections.abc import Mapping
from collections.abc import Mapping, MutableMapping
from dataclasses import dataclass
from difflib import Differ
from enum import Enum
from typing import TYPE_CHECKING, Generic, TypeVar
from typing import TYPE_CHECKING, Any, Generic, TypeVar

import typer
import yaml
Expand Down Expand Up @@ -79,8 +79,28 @@ def __find_changed_key(key_1: list[str] | str, key_2: str = "") -> str:
return f"{key_1}.{key_2}"


def render_diff(d1: Mapping, d2: Mapping, ignore: set[str] | None = None) -> str | None:
differences = list(diff(d1, d2, ignore=ignore))
def render_diff(
d1: MutableMapping[str, Any],
d2: MutableMapping[str, Any],
ignore: set[str] | None = None,
) -> str | None:
def del_ignored_keys(d: MutableMapping[str, Any]) -> None:
"""Delete key to be ignored, dictionary is modified in-place."""
if ignore:
for i in ignore:
key_path = i.split(".")
nested = d
try:
for key in key_path[:-1]:
nested = nested[key]
del nested[key_path[-1]]
except KeyError:
continue

del_ignored_keys(d1)
del_ignored_keys(d2)

differences = list(diff(d1, d2))
if not differences:
return None

Expand Down
2 changes: 0 additions & 2 deletions tests/utils/test_diff.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
{"a": 1, "b": 2, "c": 3},
{"a": 2, "d": 1},
{"a"},
" a: 1\n"
"\x1b[32m+ d: 1\n"
"\x1b[0m\x1b[31m- b: 2\n"
"\x1b[0m\x1b[31m- c: 3\n"
Expand All @@ -36,7 +35,6 @@
{"a": {"a": 9, "b": 8}, "d": 1},
{"a.a"},
" a:\n"
" a: 1\n"
"\x1b[31m- b: 2\n"
"\x1b[0m\x1b[33m? ^\n"
"\x1b[0m\x1b[32m+ b: 8\n"
Expand Down

0 comments on commit ae894d1

Please sign in to comment.