Skip to content

result

nornir_scrapli.result

ScrapliResult

Bases: Result

Source code in nornir_scrapli/result.py
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
class ScrapliResult(Result):
    def __init__(
        self,
        host: "Host",
        result: Optional[str],
        scrapli_response: Optional[Union[Response, MultiResponse, ScrapliCfgResponse]] = None,
        changed: bool = False,
        **kwargs: Any,
    ):
        """
        Scrapli Nornir Result object

        A "normal" nornir result object with an additional attribute "scrapli_response" which houses
        the original response object returned from scrapli

        Args:
            host: nornir task host object
            result: result text returned from scrapli task
            scrapli_response: original response object returned from scrapli task
            changed: bool indicating if a change has occurred
            kwargs: keyword arguments to pass to nornir Result

        Returns:
            N/A  # noqa: DAR202

        Raises:
            N/A

        """
        failed = self._process_failed(scrapli_response=scrapli_response)

        super().__init__(host=host, result=result, failed=failed, changed=changed, **kwargs)

        self.scrapli_response = scrapli_response

    @staticmethod
    def _process_failed(
        scrapli_response: Optional[Union[Response, MultiResponse, ScrapliCfgResponse]]
    ) -> bool:
        """
        Process and return string of scrapli response(s)

        Args:
            scrapli_response: scrapli Response or MultiResponse object

        Returns:
            bool: bool indicating if the nornir task failed

        Raises:
            N/A

        """
        if scrapli_response is None:
            return False
        if isinstance(scrapli_response, (Response, ScrapliCfgResponse)):
            failed: bool = scrapli_response.failed
            return failed
        if any(response.failed for response in scrapli_response):
            return True
        return False

__init__(host: Host, result: Optional[str], scrapli_response: Optional[Union[Response, MultiResponse, ScrapliCfgResponse]] = None, changed: bool = False, **kwargs: Any)

Scrapli Nornir Result object

A "normal" nornir result object with an additional attribute "scrapli_response" which houses the original response object returned from scrapli

Parameters:

Name Type Description Default
host Host

nornir task host object

required
result Optional[str]

result text returned from scrapli task

required
scrapli_response Optional[Union[Response, MultiResponse, ScrapliCfgResponse]]

original response object returned from scrapli task

None
changed bool

bool indicating if a change has occurred

False
kwargs

keyword arguments to pass to nornir Result

required

Returns:

Type Description

N/A # noqa: DAR202

Source code in nornir_scrapli/result.py
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def __init__(
    self,
    host: "Host",
    result: Optional[str],
    scrapli_response: Optional[Union[Response, MultiResponse, ScrapliCfgResponse]] = None,
    changed: bool = False,
    **kwargs: Any,
):
    """
    Scrapli Nornir Result object

    A "normal" nornir result object with an additional attribute "scrapli_response" which houses
    the original response object returned from scrapli

    Args:
        host: nornir task host object
        result: result text returned from scrapli task
        scrapli_response: original response object returned from scrapli task
        changed: bool indicating if a change has occurred
        kwargs: keyword arguments to pass to nornir Result

    Returns:
        N/A  # noqa: DAR202

    Raises:
        N/A

    """
    failed = self._process_failed(scrapli_response=scrapli_response)

    super().__init__(host=host, result=result, failed=failed, changed=changed, **kwargs)

    self.scrapli_response = scrapli_response

process_command_result(scrapli_response: Union[Response, MultiResponse]) -> str

Process and return string of scrapli response(s)

Parameters:

Name Type Description Default
scrapli_response Union[Response, MultiResponse]

scrapli Response or MultiResponse object

required

Returns:

Name Type Description
str str

string result from nornir task or None

Source code in nornir_scrapli/result.py
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def process_command_result(scrapli_response: Union[Response, MultiResponse]) -> str:
    """
    Process and return string of scrapli response(s)

    Args:
        scrapli_response: scrapli Response or MultiResponse object

    Returns:
        str: string result from nornir task or None

    Raises:
        N/A

    """
    if isinstance(scrapli_response, Response):
        result: str = scrapli_response.result
        return result
    return "\n\n".join([response.result for response in scrapli_response])

process_config_result(scrapli_response: Union[Response, MultiResponse]) -> str

Process and return string of scrapli response(s)

Parameters:

Name Type Description Default
scrapli_response Union[Response, MultiResponse]

scrapli Response or MultiResponse object

required

Returns:

Name Type Description
str str

string result from nornir task or None

Source code in nornir_scrapli/result.py
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def process_config_result(scrapli_response: Union[Response, MultiResponse]) -> str:
    """
    Process and return string of scrapli response(s)

    Args:
        scrapli_response: scrapli Response or MultiResponse object

    Returns:
        str: string result from nornir task or None

    Raises:
        N/A

    """
    full_results = ""
    if isinstance(scrapli_response, Response):
        for config_input, config_result in zip(
            scrapli_response.channel_input.split("\n"), scrapli_response.result.split("\n")
        ):
            if config_input == config_result:
                full_results += f"{config_input}\n"
            else:
                full_results += "\n".join([config_input, config_result])
    else:
        for response in scrapli_response:
            full_results += "\n".join([response.channel_input, response.result])
    return full_results