-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path_check_attributes.py
68 lines (46 loc) · 1.82 KB
/
_check_attributes.py
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
62
63
64
65
66
67
68
# Copyright 2018-present Network Optix, Inc. Licensed under MPL 2.0: www.mozilla.org/MPL/2.0/
import json
from abc import ABCMeta
from abc import abstractmethod
from typing import Any
from typing import Collection
from typing import Mapping
from typing import Union
class _Expectation(metaclass=ABCMeta):
@abstractmethod
def compare(self, actual):
pass
class EqualTo(_Expectation):
def __init__(self, expected: Any):
self._expected = expected
def compare(self, actual: Union[str, int]):
return self._expected == actual
def __repr__(self):
return f"{self.__class__.__name__}({self._expected!r})"
class Vendor(_Expectation):
def __init__(self, expected: Collection[str]):
self._expected = expected
def compare(self, actual: str):
return any(actual.lower() == value.lower() for value in self._expected)
def __repr__(self):
return f"{self.__class__.__name__}({self._expected!r})"
class ExpectedAttributes:
def __init__(self, expected: Mapping[str, _Expectation]):
self._expected = expected
def validate(self, actual: Mapping[str, Union[str, int]]):
errors = []
for key, checker in self._expected.items():
actual_value = actual.get(key)
if not checker.compare(actual_value):
errors.append({key: f"Expected: {checker!r}, Actual: {actual_value}"})
return errors
def _as_str_map(self):
return {key: f"{checker!r}" for key, checker in self._expected.items()}
def __repr__(self):
return f"{self.__class__.__name__}({json.dumps(self._as_str_map(), indent=4)})"
def model(self):
return self._expected.get('model')
def firmware(self):
return self._expected.get('firmware')
def vendor(self):
return self._expected.get('vendor')