Skip to content

helper

nornir_scrapli.helper

diff_xml_text(document_one: str, document_two: str) -> str

Diff xml text strings

Really could be just "diff text" but also ensuring we ignore the "message-id" lines. This is really pretty simple and not always super great, but better than nothing for now!

Parameters:

Name Type Description Default
document_one str

string of xml doc 1

required
document_two str

string of xml doc 2

required

Returns:

Name Type Description
str str

unified diff of the two input documents

Source code in nornir_scrapli/helper.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
def diff_xml_text(document_one: str, document_two: str) -> str:
    """
    Diff xml text strings

    Really could be just "diff text" but also ensuring we ignore the "message-id" lines. This is
    really pretty simple and not always super great, but better than nothing for now!

    Args:
        document_one: string of xml doc 1
        document_two: string of xml doc 2

    Returns:
        str: unified diff of the two input documents

    Raises:
        N/A

    """
    # ignore message-id stuff -- maybe more in the future?
    document_one_lines = [line for line in document_one.splitlines() if "message-id" not in line]
    document_two_lines = [line for line in document_two.splitlines() if "message-id" not in line]
    diff = difflib.unified_diff(document_one_lines, document_two_lines)

    diff_lines = []
    for line in diff:
        if line.startswith("---") or line.startswith("+++"):
            # may as well just strip out the header lines and such, we dont care about them
            continue
        if line.startswith("+"):
            diff_lines.append(f"{ANSI_GREEN}{line}{ANSI_END}")
        elif line.startswith("-"):
            diff_lines.append(f"{ANSI_RED}{line}{ANSI_END}")
        else:
            diff_lines.append(line)

    return "\n".join(diff_lines)