Coverage for python/lsst/daf/butler/registry/interfaces/_versioning.py: 55%
27 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-03-28 04:40 -0700
« prev ^ index » next coverage.py v6.5.0, created at 2023-03-28 04:40 -0700
1# This file is part of daf_butler.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (http://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# This program is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 3 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program. If not, see <http://www.gnu.org/licenses/>.
22from __future__ import annotations
24__all__ = [
25 "VersionTuple",
26 "VersionedExtension",
27]
29from abc import ABC, abstractmethod
30from typing import NamedTuple, Optional
33class VersionTuple(NamedTuple):
34 """Class representing a version number.
36 Parameters
37 ----------
38 major, minor, patch : `int`
39 Version number components
40 """
42 major: int
43 minor: int
44 patch: int
46 @classmethod
47 def fromString(cls, versionStr: str) -> VersionTuple:
48 """Extract version number from a string.
50 Parameters
51 ----------
52 versionStr : `str`
53 Version number in string form "X.Y.Z", all components must be
54 present.
56 Returns
57 -------
58 version : `VersionTuple`
59 Parsed version tuple.
61 Raises
62 ------
63 ValueError
64 Raised if string has an invalid format.
65 """
66 try:
67 version = tuple(int(v) for v in versionStr.split("."))
68 except ValueError as exc:
69 raise ValueError(f"Invalid version string '{versionStr}'") from exc
70 if len(version) != 3:
71 raise ValueError(f"Invalid version string '{versionStr}', must consist of three numbers")
72 return cls(*version)
74 def __str__(self) -> str:
75 """Transform version tuple into a canonical string form."""
76 return f"{self.major}.{self.minor}.{self.patch}"
79class VersionedExtension(ABC):
80 """Interface for extension classes with versions."""
82 @classmethod
83 @abstractmethod
84 def currentVersion(cls) -> Optional[VersionTuple]:
85 """Return extension version as defined by current implementation.
87 This method can return ``None`` if an extension does not require
88 its version to be saved or checked.
90 Returns
91 -------
92 version : `VersionTuple`
93 Current extension version or ``None``.
94 """
95 raise NotImplementedError()
97 @classmethod
98 def extensionName(cls) -> str:
99 """Return full name of the extension.
101 This name should match the name defined in registry configuration. It
102 is also stored in registry attributes. Default implementation returns
103 full class name.
105 Returns
106 -------
107 name : `str`
108 Full extension name.
109 """
110 return f"{cls.__module__}.{cls.__name__}"