File size: 1,782 Bytes
b40c49c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""

from __future__ import annotations

from typing import Iterator


def _get_new_status_message(
    current_status: dict[str, bool], new_status: dict[str, bool]
) -> Iterator[str]:
    for k, v in new_status.items():
        if v is not None:
            if current_status.get(k) is not None and current_status.get(k) != v:
                yield (
                    f"condition {k!r} to {v!r} from current "
                    f"status {current_status.get(k)!r}"
                )
            else:
                yield f"condition {k!r} to {v!r}"


def _get_current_status_message(
    current_status: dict[str, bool], new_status: dict[str, bool]
) -> Iterator[str]:
    for k, v in current_status.items():
        if v is not None and k not in new_status:
            yield f"condition {k!r} is {v!r}"


def _build_message(current_status: dict[str, bool], new_status: dict[str, bool]) -> str:
    message = ", and ".join(_get_new_status_message(current_status, new_status))

    current_message = " and ".join(
        _get_current_status_message(current_status, new_status)
    )

    if current_message:
        message += f". Where existing status for {current_message}"

    return f"When setting {message}"


class Unsatisfiable(ValueError):

    def __init__(
        self,
        new_status: dict[str, bool],
        current_status: dict[str, bool],
    ) -> None:
        message = _build_message(current_status, new_status)
        super().__init__(message)
        self.message = message
        self.current_status = current_status
        self.new_status = new_status

    def __repr__(self):
        return f"<{self.__class__.__name__}: {self.message!r}>"