-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathswift_version_range.py
More file actions
55 lines (49 loc) · 2.47 KB
/
Copy pathswift_version_range.py
File metadata and controls
55 lines (49 loc) · 2.47 KB
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
from univers.versions import SwiftVersion
from univers.version_range import VersionRange
from univers.version_constraint import VersionConstraint
import attr
@attr.s(auto_attribs=True, frozen=True)
class SwiftVersionRange(VersionRange):
expression: str
scheme: str = 'swift'
version_class: type = SwiftVersion
constraints: list = attr.ib(init=False)
def __attrs_post_init__(self):
object.__setattr__(self, 'constraints', self.parse(self.expression))
@staticmethod
def parse(expression):
constraints = []
# Remove quotes for parsing.
expression = expression.replace('"', '').strip()
# Handle different range types.
if "..<" in expression:
parts = expression.split("..<")
if len(parts) == 2:
lower = SwiftVersion(parts[0].strip())
upper = SwiftVersion(parts[1].strip())
constraints.append(VersionConstraint(comparator=">=", version=lower))
constraints.append(VersionConstraint(comparator="<", version=upper))
elif "..." in expression:
parts = expression.split("...")
if len(parts) == 2:
lower = SwiftVersion(parts[0].strip())
upper = SwiftVersion(parts[1].strip())
constraints.append(VersionConstraint(comparator=">=", version=lower))
constraints.append(VersionConstraint(comparator="<=", version=upper))
else:
# Handle other cases such as 'exact:' and 'from:' prefixes.
if 'exact:' in expression:
version = SwiftVersion(expression.split('exact:')[1].strip())
constraints.append(VersionConstraint(comparator="=", version=version))
elif 'from:' in expression:
version = SwiftVersion(expression.split('from:')[1].strip())
next_major_version = version.next_major()
constraints.append(VersionConstraint(comparator=">=", version=version))
constraints.append(VersionConstraint(comparator="<", version=next_major_version))
else:
# Handle a single version without any prefix.
version = SwiftVersion(expression)
constraints.append(VersionConstraint(comparator="=", version=version))
return constraints
def __str__(self):
return f"vers:swift/{'|'.join([c.to_string() for c in self.constraints])}"