|
21 | 21 | """
|
22 | 22 |
|
23 | 23 | import re
|
24 |
| -from datetime import datetime, timezone |
| 24 | +from datetime import datetime |
25 | 25 | from enum import Enum
|
26 | 26 | from functools import reduce
|
27 |
| -from hashlib import sha1 |
28 |
| -from itertools import zip_longest |
29 | 27 | from json import loads as json_loads
|
30 |
| -from typing import Any, Dict, FrozenSet, Generator, Iterable, List, Optional, Tuple, Type, TypeVar |
| 28 | +from typing import Any, Dict, FrozenSet, Generator, Iterable, List, Optional, Tuple, Type |
31 | 29 | from warnings import warn
|
32 | 30 | from xml.etree.ElementTree import Element as XmlElement # nosec B405
|
33 | 31 |
|
34 | 32 | import serializable
|
35 | 33 | from sortedcontainers import SortedSet
|
36 | 34 |
|
37 | 35 | from .. import __version__ as __ThisToolVersion # noqa: N812
|
| 36 | +from .._internal.compare import ComparableTuple as _ComparableTuple |
38 | 37 | from ..exception.model import (
|
39 | 38 | InvalidLocaleTypeException,
|
40 | 39 | InvalidUriException,
|
|
52 | 51 | )
|
53 | 52 |
|
54 | 53 |
|
55 |
| -def get_now_utc() -> datetime: |
56 |
| - return datetime.now(tz=timezone.utc) |
57 |
| - |
58 |
| - |
59 |
| -def sha1sum(filename: str) -> str: |
60 |
| - """ |
61 |
| - Generate a SHA1 hash of the provided file. |
62 |
| -
|
63 |
| - Args: |
64 |
| - filename: |
65 |
| - Absolute path to file to hash as `str` |
66 |
| -
|
67 |
| - Returns: |
68 |
| - SHA-1 hash |
69 |
| - """ |
70 |
| - h = sha1() # nosec B303, B324 |
71 |
| - with open(filename, 'rb') as f: |
72 |
| - for byte_block in iter(lambda: f.read(4096), b''): |
73 |
| - h.update(byte_block) |
74 |
| - return h.hexdigest() |
75 |
| - |
76 |
| - |
77 |
| -_T = TypeVar('_T') |
78 |
| - |
79 |
| - |
80 |
| -class ComparableTuple(Tuple[Optional[_T], ...]): |
81 |
| - """ |
82 |
| - Allows comparison of tuples, allowing for None values. |
83 |
| - """ |
84 |
| - |
85 |
| - def __lt__(self, other: Any) -> bool: |
86 |
| - for s, o in zip_longest(self, other): |
87 |
| - if s == o: |
88 |
| - continue |
89 |
| - # the idea is to have any consistent order, not necessarily "natural" order. |
90 |
| - if s is None: |
91 |
| - return False |
92 |
| - if o is None: |
93 |
| - return True |
94 |
| - return True if s < o else False |
95 |
| - return False |
96 |
| - |
97 |
| - def __gt__(self, other: Any) -> bool: |
98 |
| - for s, o in zip_longest(self, other): |
99 |
| - if s == o: |
100 |
| - continue |
101 |
| - # the idea is to have any consistent order, not necessarily "natural" order. |
102 |
| - if s is None: |
103 |
| - return True |
104 |
| - if o is None: |
105 |
| - return False |
106 |
| - return True if s > o else False |
107 |
| - return False |
108 |
| - |
109 |
| - |
110 | 54 | @serializable.serializable_enum
|
111 | 55 | class DataFlow(str, Enum):
|
112 | 56 | """
|
@@ -184,8 +128,11 @@ def __eq__(self, other: object) -> bool:
|
184 | 128 |
|
185 | 129 | def __lt__(self, other: object) -> bool:
|
186 | 130 | if isinstance(other, DataClassification):
|
187 |
| - return ComparableTuple((self.flow, self.classification)) < \ |
188 |
| - ComparableTuple((other.flow, other.classification)) |
| 131 | + return _ComparableTuple(( |
| 132 | + self.flow, self.classification |
| 133 | + )) < _ComparableTuple(( |
| 134 | + other.flow, other.classification |
| 135 | + )) |
189 | 136 | return NotImplemented
|
190 | 137 |
|
191 | 138 | def __hash__(self) -> int:
|
@@ -279,8 +226,11 @@ def __eq__(self, other: object) -> bool:
|
279 | 226 |
|
280 | 227 | def __lt__(self, other: Any) -> bool:
|
281 | 228 | if isinstance(other, AttachedText):
|
282 |
| - return ComparableTuple((self.content_type, self.content, self.encoding)) < \ |
283 |
| - ComparableTuple((other.content_type, other.content, other.encoding)) |
| 229 | + return _ComparableTuple(( |
| 230 | + self.content_type, self.content, self.encoding |
| 231 | + )) < _ComparableTuple(( |
| 232 | + other.content_type, other.content, other.encoding |
| 233 | + )) |
284 | 234 | return NotImplemented
|
285 | 235 |
|
286 | 236 | def __hash__(self) -> int:
|
@@ -481,7 +431,11 @@ def __eq__(self, other: object) -> bool:
|
481 | 431 |
|
482 | 432 | def __lt__(self, other: Any) -> bool:
|
483 | 433 | if isinstance(other, HashType):
|
484 |
| - return ComparableTuple((self.alg, self.content)) < ComparableTuple((other.alg, other.content)) |
| 434 | + return _ComparableTuple(( |
| 435 | + self.alg, self.content |
| 436 | + )) < _ComparableTuple(( |
| 437 | + other.alg, other.content |
| 438 | + )) |
485 | 439 | return NotImplemented
|
486 | 440 |
|
487 | 441 | def __hash__(self) -> int:
|
@@ -806,8 +760,11 @@ def __eq__(self, other: object) -> bool:
|
806 | 760 |
|
807 | 761 | def __lt__(self, other: Any) -> bool:
|
808 | 762 | if isinstance(other, ExternalReference):
|
809 |
| - return ComparableTuple((self._type, self._url, self._comment)) < \ |
810 |
| - ComparableTuple((other._type, other._url, other._comment)) |
| 763 | + return _ComparableTuple(( |
| 764 | + self._type, self._url, self._comment |
| 765 | + )) < _ComparableTuple(( |
| 766 | + other._type, other._url, other._comment |
| 767 | + )) |
811 | 768 | return NotImplemented
|
812 | 769 |
|
813 | 770 | def __hash__(self) -> int:
|
@@ -875,7 +832,11 @@ def __eq__(self, other: object) -> bool:
|
875 | 832 |
|
876 | 833 | def __lt__(self, other: Any) -> bool:
|
877 | 834 | if isinstance(other, Property):
|
878 |
| - return ComparableTuple((self.name, self.value)) < ComparableTuple((other.name, other.value)) |
| 835 | + return _ComparableTuple(( |
| 836 | + self.name, self.value |
| 837 | + )) < _ComparableTuple(( |
| 838 | + other.name, other.value |
| 839 | + )) |
879 | 840 | return NotImplemented
|
880 | 841 |
|
881 | 842 | def __hash__(self) -> int:
|
@@ -958,8 +919,11 @@ def __eq__(self, other: object) -> bool:
|
958 | 919 |
|
959 | 920 | def __lt__(self, other: Any) -> bool:
|
960 | 921 | if isinstance(other, NoteText):
|
961 |
| - return ComparableTuple((self.content, self.content_type, self.encoding)) < \ |
962 |
| - ComparableTuple((other.content, other.content_type, other.encoding)) |
| 922 | + return _ComparableTuple(( |
| 923 | + self.content, self.content_type, self.encoding |
| 924 | + )) < _ComparableTuple(( |
| 925 | + other.content, other.content_type, other.encoding |
| 926 | + )) |
963 | 927 | return NotImplemented
|
964 | 928 |
|
965 | 929 | def __hash__(self) -> int:
|
@@ -1035,7 +999,11 @@ def __eq__(self, other: object) -> bool:
|
1035 | 999 |
|
1036 | 1000 | def __lt__(self, other: Any) -> bool:
|
1037 | 1001 | if isinstance(other, Note):
|
1038 |
| - return ComparableTuple((self.locale, self.text)) < ComparableTuple((other.locale, other.text)) |
| 1002 | + return _ComparableTuple(( |
| 1003 | + self.locale, self.text |
| 1004 | + )) < _ComparableTuple(( |
| 1005 | + other.locale, other.text |
| 1006 | + )) |
1039 | 1007 | return NotImplemented
|
1040 | 1008 |
|
1041 | 1009 | def __hash__(self) -> int:
|
@@ -1116,8 +1084,11 @@ def __eq__(self, other: object) -> bool:
|
1116 | 1084 |
|
1117 | 1085 | def __lt__(self, other: Any) -> bool:
|
1118 | 1086 | if isinstance(other, OrganizationalContact):
|
1119 |
| - return ComparableTuple((self.name, self.email, self.phone)) < \ |
1120 |
| - ComparableTuple((other.name, other.email, other.phone)) |
| 1087 | + return _ComparableTuple(( |
| 1088 | + self.name, self.email, self.phone |
| 1089 | + )) < _ComparableTuple(( |
| 1090 | + other.name, other.email, other.phone |
| 1091 | + )) |
1121 | 1092 | return NotImplemented
|
1122 | 1093 |
|
1123 | 1094 | def __hash__(self) -> int:
|
@@ -1323,8 +1294,11 @@ def __eq__(self, other: object) -> bool:
|
1323 | 1294 |
|
1324 | 1295 | def __lt__(self, other: Any) -> bool:
|
1325 | 1296 | if isinstance(other, Tool):
|
1326 |
| - return ComparableTuple((self.vendor, self.name, self.version)) < \ |
1327 |
| - ComparableTuple((other.vendor, other.name, other.version)) |
| 1297 | + return _ComparableTuple(( |
| 1298 | + self.vendor, self.name, self.version |
| 1299 | + )) < _ComparableTuple(( |
| 1300 | + other.vendor, other.name, other.version |
| 1301 | + )) |
1328 | 1302 | return NotImplemented
|
1329 | 1303 |
|
1330 | 1304 | def __hash__(self) -> int:
|
@@ -1404,8 +1378,11 @@ def __eq__(self, other: object) -> bool:
|
1404 | 1378 |
|
1405 | 1379 | def __lt__(self, other: Any) -> bool:
|
1406 | 1380 | if isinstance(other, IdentifiableAction):
|
1407 |
| - return ComparableTuple((self.timestamp, self.name, self.email)) < \ |
1408 |
| - ComparableTuple((other.timestamp, other.name, other.email)) |
| 1381 | + return _ComparableTuple(( |
| 1382 | + self.timestamp, self.name, self.email |
| 1383 | + )) < _ComparableTuple(( |
| 1384 | + other.timestamp, other.name, other.email |
| 1385 | + )) |
1409 | 1386 | return NotImplemented
|
1410 | 1387 |
|
1411 | 1388 | def __hash__(self) -> int:
|
|
0 commit comments