|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | | -import contextlib |
4 | | -import re |
5 | 3 | from abc import ABC |
6 | | -from typing import Tuple, Union |
7 | 4 |
|
8 | | -# TODO Is this base class (still) useful? |
| 5 | +from openeo.utils.version import ApiVersionException, ComparableVersion |
| 6 | + |
| 7 | +__all__ = ["Capabilities", "ComparableVersion", "ApiVersionException"] |
9 | 8 |
|
10 | 9 |
|
11 | 10 | class Capabilities(ABC): |
12 | 11 | """Represents capabilities of a connection / back end.""" |
13 | 12 |
|
| 13 | + # TODO Is this base class (still) useful? |
| 14 | + |
14 | 15 | def __init__(self, data): |
15 | 16 | pass |
16 | 17 |
|
@@ -51,159 +52,3 @@ def list_plans(self): |
51 | 52 | """ List all billing plans.""" |
52 | 53 | # Field: billing > plans |
53 | 54 | pass |
54 | | - |
55 | | - |
56 | | -# Type annotation aliases |
57 | | -_VersionTuple = Tuple[Union[int, str], ...] |
58 | | - |
59 | | - |
60 | | -class ComparableVersion: |
61 | | - """ |
62 | | - Helper to compare a version (e.g. API version) against another (threshold) version |
63 | | -
|
64 | | - >>> v = ComparableVersion('1.2.3') |
65 | | - >>> v.at_least('1.2.1') |
66 | | - True |
67 | | - >>> v.at_least('1.10.2') |
68 | | - False |
69 | | - >>> v > "2.0" |
70 | | - False |
71 | | -
|
72 | | - To express a threshold condition you sometimes want the reference or threshold value on |
73 | | - the left hand side or right hand side of the logical expression. |
74 | | - There are two groups of methods to handle each case: |
75 | | -
|
76 | | - - right hand side referencing methods. These read more intuitively. For example: |
77 | | -
|
78 | | - `a.at_least(b)`: a is equal or higher than b |
79 | | - `a.below(b)`: a is lower than b |
80 | | -
|
81 | | - - left hand side referencing methods. These allow "currying" a threshold value |
82 | | - in a reusable condition callable. For example: |
83 | | -
|
84 | | - `a.or_higher(b)`: b is equal or higher than a |
85 | | - `a.accept_lower(b)`: b is lower than a |
86 | | -
|
87 | | - Implementation is loosely based on (now deprecated) `distutils.version.LooseVersion`, |
88 | | - which pragmatically parses version strings as a sequence of numbers (compared numerically) |
89 | | - or alphabetic strings (compared lexically), e.g.: 1.5.1, 1.5.2b2, 161, 8.02, 2g6, 2.2beta29. |
90 | | - """ |
91 | | - |
92 | | - _component_re = re.compile(r'(\d+ | [a-zA-Z]+ | \.)', re.VERBOSE) |
93 | | - |
94 | | - def __init__(self, version: Union[str, 'ComparableVersion', tuple]): |
95 | | - if isinstance(version, ComparableVersion): |
96 | | - self._version = version._version |
97 | | - elif isinstance(version, tuple): |
98 | | - self._version = version |
99 | | - elif isinstance(version, str): |
100 | | - self._version = self._parse(version) |
101 | | - else: |
102 | | - raise ValueError(version) |
103 | | - |
104 | | - @classmethod |
105 | | - def _parse(cls, version_string: str) -> _VersionTuple: |
106 | | - components = [ |
107 | | - x for x in cls._component_re.split(version_string) |
108 | | - if x and x != '.' |
109 | | - ] |
110 | | - for i, obj in enumerate(components): |
111 | | - with contextlib.suppress(ValueError): |
112 | | - components[i] = int(obj) |
113 | | - return tuple(components) |
114 | | - |
115 | | - @property |
116 | | - def parts(self) -> _VersionTuple: |
117 | | - """Version components as a tuple""" |
118 | | - return self._version |
119 | | - |
120 | | - def __repr__(self): |
121 | | - return '{c}({v!r})'.format(c=type(self).__name__, v=self._version) |
122 | | - |
123 | | - def __str__(self): |
124 | | - return ".".join(map(str, self._version)) |
125 | | - |
126 | | - def __hash__(self): |
127 | | - return hash(self._version) |
128 | | - |
129 | | - def to_string(self): |
130 | | - return str(self) |
131 | | - |
132 | | - @staticmethod |
133 | | - def _pad(a: Union[str, ComparableVersion], b: Union[str, ComparableVersion]) -> Tuple[_VersionTuple, _VersionTuple]: |
134 | | - """Pad version tuples with zero/empty to get same length for intuitive comparison""" |
135 | | - a = ComparableVersion(a)._version |
136 | | - b = ComparableVersion(b)._version |
137 | | - if len(a) > len(b): |
138 | | - b = b + tuple(0 if isinstance(x, int) else "" for x in a[len(b) :]) |
139 | | - elif len(b) > len(a): |
140 | | - a = a + tuple(0 if isinstance(x, int) else "" for x in b[len(a) :]) |
141 | | - return a, b |
142 | | - |
143 | | - def __eq__(self, other: Union[str, ComparableVersion]) -> bool: |
144 | | - a, b = self._pad(self, other) |
145 | | - return a == b |
146 | | - |
147 | | - def __ge__(self, other: Union[str, ComparableVersion]) -> bool: |
148 | | - a, b = self._pad(self, other) |
149 | | - return a >= b |
150 | | - |
151 | | - def __gt__(self, other: Union[str, ComparableVersion]) -> bool: |
152 | | - a, b = self._pad(self, other) |
153 | | - return a > b |
154 | | - |
155 | | - def __le__(self, other: Union[str, ComparableVersion]) -> bool: |
156 | | - a, b = self._pad(self, other) |
157 | | - return a <= b |
158 | | - |
159 | | - def __lt__(self, other: Union[str, ComparableVersion]) -> bool: |
160 | | - a, b = self._pad(self, other) |
161 | | - return a < b |
162 | | - |
163 | | - def equals(self, other: Union[str, 'ComparableVersion']): |
164 | | - return self == other |
165 | | - |
166 | | - # Right hand side referencing expressions. |
167 | | - def at_least(self, other: Union[str, 'ComparableVersion']): |
168 | | - """Self is at equal or higher than other.""" |
169 | | - return self >= other |
170 | | - |
171 | | - def above(self, other: Union[str, 'ComparableVersion']): |
172 | | - """Self is higher than other.""" |
173 | | - return self > other |
174 | | - |
175 | | - def at_most(self, other: Union[str, 'ComparableVersion']): |
176 | | - """Self is equal or lower than other.""" |
177 | | - return self <= other |
178 | | - |
179 | | - def below(self, other: Union[str, 'ComparableVersion']): |
180 | | - """Self is lower than other.""" |
181 | | - return self < other |
182 | | - |
183 | | - # Left hand side referencing expressions. |
184 | | - def or_higher(self, other: Union[str, 'ComparableVersion']): |
185 | | - """Other is equal or higher than self.""" |
186 | | - return ComparableVersion(other) >= self |
187 | | - |
188 | | - def or_lower(self, other: Union[str, 'ComparableVersion']): |
189 | | - """Other is equal or lower than self""" |
190 | | - return ComparableVersion(other) <= self |
191 | | - |
192 | | - def accept_lower(self, other: Union[str, 'ComparableVersion']): |
193 | | - """Other is lower than self.""" |
194 | | - return ComparableVersion(other) < self |
195 | | - |
196 | | - def accept_higher(self, other: Union[str, 'ComparableVersion']): |
197 | | - """Other is higher than self.""" |
198 | | - return ComparableVersion(other) > self |
199 | | - |
200 | | - def require_at_least(self, other: Union[str, "ComparableVersion"]): |
201 | | - """Raise exception if self is not at least other.""" |
202 | | - if not self.at_least(other): |
203 | | - raise ApiVersionException( |
204 | | - f"openEO API version should be at least {other!s}, but got {self!s}." |
205 | | - ) |
206 | | - |
207 | | - |
208 | | -class ApiVersionException(RuntimeError): |
209 | | - pass |
0 commit comments