spdx_tools.spdx.model.version

 1# SPDX-FileCopyrightText: 2022 spdx contributors
 2#
 3# SPDX-License-Identifier: Apache-2.0
 4
 5
 6import re
 7from re import Pattern
 8
 9
10class Version:
11    VERSION_REGEX: Pattern = re.compile(r"^(\d+)\.(\d+)$")
12
13    major: int
14    minor: int
15
16    @classmethod
17    def is_valid_version_string(cls, value: str) -> bool:
18        return cls.VERSION_REGEX.match(value) is not None
19
20    # No type hint for Python reasons.
21    # See https://stackoverflow.com/questions/33533148/how-do-i-type-hint-a-method-with-the-type-of-the-enclosing-class
22    @classmethod
23    def from_string(cls, value: str):
24        if not Version.is_valid_version_string(value):
25            raise ValueError(f"{value} is not a valid version string")
26
27        match = cls.VERSION_REGEX.match(value)
28        return cls(int(match.group(1)), int(match.group(2)))
29
30    def __init__(self, major: int, minor: int):
31        self.major = major
32        self.minor = minor
33
34    def __str__(self):
35        return f"{self.major}.{self.minor}"
36
37    def __eq__(self, other):
38        if not isinstance(other, Version):
39            return False
40        return self.major == other.major and self.minor == other.minor
class Version:
11class Version:
12    VERSION_REGEX: Pattern = re.compile(r"^(\d+)\.(\d+)$")
13
14    major: int
15    minor: int
16
17    @classmethod
18    def is_valid_version_string(cls, value: str) -> bool:
19        return cls.VERSION_REGEX.match(value) is not None
20
21    # No type hint for Python reasons.
22    # See https://stackoverflow.com/questions/33533148/how-do-i-type-hint-a-method-with-the-type-of-the-enclosing-class
23    @classmethod
24    def from_string(cls, value: str):
25        if not Version.is_valid_version_string(value):
26            raise ValueError(f"{value} is not a valid version string")
27
28        match = cls.VERSION_REGEX.match(value)
29        return cls(int(match.group(1)), int(match.group(2)))
30
31    def __init__(self, major: int, minor: int):
32        self.major = major
33        self.minor = minor
34
35    def __str__(self):
36        return f"{self.major}.{self.minor}"
37
38    def __eq__(self, other):
39        if not isinstance(other, Version):
40            return False
41        return self.major == other.major and self.minor == other.minor
Version(major: int, minor: int)
31    def __init__(self, major: int, minor: int):
32        self.major = major
33        self.minor = minor
VERSION_REGEX: re.Pattern = re.compile('^(\\d+)\\.(\\d+)$')
major: int
minor: int
@classmethod
def is_valid_version_string(cls, value: str) -> bool:
17    @classmethod
18    def is_valid_version_string(cls, value: str) -> bool:
19        return cls.VERSION_REGEX.match(value) is not None
@classmethod
def from_string(cls, value: str):
23    @classmethod
24    def from_string(cls, value: str):
25        if not Version.is_valid_version_string(value):
26            raise ValueError(f"{value} is not a valid version string")
27
28        match = cls.VERSION_REGEX.match(value)
29        return cls(int(match.group(1)), int(match.group(2)))