|
| 1 | +# Copyright (C) 2023 Greenbone Networks GmbH |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | +# |
| 5 | +# This program is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# This program is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU General Public License |
| 16 | +# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + |
| 18 | +from abc import ABC |
| 19 | +from typing import Type |
| 20 | + |
| 21 | +from ..calculator import VersionCalculator |
| 22 | +from ..version import Version |
| 23 | + |
| 24 | + |
| 25 | +class VersioningScheme(ABC): |
| 26 | + """ |
| 27 | + An abstract base class for versioning schemes |
| 28 | +
|
| 29 | + Example: |
| 30 | + Example on how to implement a new VersioningScheme |
| 31 | +
|
| 32 | + .. code-block:: python |
| 33 | +
|
| 34 | + from pontos.version.scheme import VersioningScheme |
| 35 | +
|
| 36 | + class MyVersioningScheme(VersioningScheme): |
| 37 | + version_cls = MyVersion |
| 38 | + version_calculator_cls = MyVersionCalculator |
| 39 | + """ |
| 40 | + |
| 41 | + version_cls: Type[Version] |
| 42 | + version_calculator_cls: Type[VersionCalculator] |
| 43 | + |
| 44 | + @classmethod |
| 45 | + def parse_version(cls, version: str) -> Version: |
| 46 | + """ |
| 47 | + Parse a version from a version string |
| 48 | +
|
| 49 | + Raises: |
| 50 | + :py:class:`pontos.version.error.VersionError`: If the version |
| 51 | + string contains an invalid version |
| 52 | +
|
| 53 | + Returns: |
| 54 | + A version instance |
| 55 | + """ |
| 56 | + return cls.version_cls.from_string(version) |
| 57 | + |
| 58 | + @classmethod |
| 59 | + def from_version(cls, version: Version) -> Version: |
| 60 | + return cls.version_cls.from_version(version) |
| 61 | + |
| 62 | + @classmethod |
| 63 | + def calculator(cls) -> Type[VersionCalculator]: |
| 64 | + """ |
| 65 | + Return a matching version calculator for the implemented versioning |
| 66 | + schema. |
| 67 | +
|
| 68 | + Returns: |
| 69 | + A version calculator |
| 70 | + """ |
| 71 | + return cls.version_calculator_cls |
0 commit comments