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-23 02:06 -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/>. 

21 

22from __future__ import annotations 

23 

24__all__ = [ 

25 "VersionTuple", 

26 "VersionedExtension", 

27] 

28 

29from abc import ABC, abstractmethod 

30from typing import NamedTuple, Optional 

31 

32 

33class VersionTuple(NamedTuple): 

34 """Class representing a version number. 

35 

36 Parameters 

37 ---------- 

38 major, minor, patch : `int` 

39 Version number components 

40 """ 

41 

42 major: int 

43 minor: int 

44 patch: int 

45 

46 @classmethod 

47 def fromString(cls, versionStr: str) -> VersionTuple: 

48 """Extract version number from a string. 

49 

50 Parameters 

51 ---------- 

52 versionStr : `str` 

53 Version number in string form "X.Y.Z", all components must be 

54 present. 

55 

56 Returns 

57 ------- 

58 version : `VersionTuple` 

59 Parsed version tuple. 

60 

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) 

73 

74 def __str__(self) -> str: 

75 """Transform version tuple into a canonical string form.""" 

76 return f"{self.major}.{self.minor}.{self.patch}" 

77 

78 

79class VersionedExtension(ABC): 

80 """Interface for extension classes with versions.""" 

81 

82 @classmethod 

83 @abstractmethod 

84 def currentVersion(cls) -> Optional[VersionTuple]: 

85 """Return extension version as defined by current implementation. 

86 

87 This method can return ``None`` if an extension does not require 

88 its version to be saved or checked. 

89 

90 Returns 

91 ------- 

92 version : `VersionTuple` 

93 Current extension version or ``None``. 

94 """ 

95 raise NotImplementedError() 

96 

97 @classmethod 

98 def extensionName(cls) -> str: 

99 """Return full name of the extension. 

100 

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. 

104 

105 Returns 

106 ------- 

107 name : `str` 

108 Full extension name. 

109 """ 

110 return f"{cls.__module__}.{cls.__name__}"