1+ from typing import Self
2+
3+
14class UnsupportedDistroVersion (Exception ):
25 pass
36
@@ -6,63 +9,65 @@ class NotAPatchRelease(Exception):
69 pass
710
811
9- def DistroVersion (maybe_distro_version ):
10- version_components = maybe_distro_version .split ("." )
11- if len (version_components ) > 3 or len (version_components ) < 2 :
12- raise UnsupportedDistroVersion (
13- f"Unexpected version number format { maybe_distro_version } "
14- )
15-
16- if not all (map (lambda x : x .isdigit (), version_components )):
17- raise UnsupportedDistroVersion (
18- f"Unexpected version number format { maybe_distro_version } "
19- )
20-
21- if len (version_components ) == 2 :
22- return LegacyDistroVersion (* (int (c ) for c in version_components ))
23- elif len (version_components ) == 3 :
24- return SemverDistroVersion (* (int (c ) for c in version_components ))
25- else :
26- raise UnsupportedDistroVersion (
27- f"Unexpected number of version components: { maybe_distro_version } "
28- )
29-
30-
3112class BaseDistroVersion :
32- major = None
33- minor = None
34- patch = None
13+ major : int = 0
14+ minor : int = 0
15+ patch : int = 0
3516
36- def is_patch_release (self ):
17+ def is_patch_release (self ) -> int :
3718 return self .patch and self .patch > 0
3819
3920
4021class LegacyDistroVersion (BaseDistroVersion ):
41- def __init__ (self , major , patch ) :
22+ def __init__ (self : Self , major : int , patch : int ) -> None :
4223 self .major = major
4324 self .patch = patch
4425
45- def __str__ (self ):
26+ def __str__ (self ) -> str :
4627 return f"{ self .major } .{ self .patch } "
4728
48- def previous_patch_release (self ):
29+ def previous_patch_release (self ) -> "LegacyDistroVersion" :
4930 if not self .is_patch_release ():
5031 raise NotAPatchRelease (f"{ self } is not a patch release" )
5132
5233 return LegacyDistroVersion (self .major , self .patch - 1 )
5334
5435
5536class SemverDistroVersion (BaseDistroVersion ):
56- def __init__ (self , major , minor , patch ) :
37+ def __init__ (self , major : int , minor : int , patch : int ) -> None :
5738 self .major = major
5839 self .minor = minor
5940 self .patch = patch
6041
61- def __str__ (self ):
42+ def __str__ (self ) -> str :
6243 return f"{ self .major } .{ self .minor } .{ self .patch } "
6344
64- def previous_patch_release (self ):
45+ def previous_patch_release (self ) -> "SemverDistroVersion" :
6546 if not self .is_patch_release ():
6647 raise NotAPatchRelease (f"{ self } is not a patch release" )
6748
6849 return SemverDistroVersion (self .major , self .minor , self .patch - 1 )
50+
51+
52+ def DistroVersion (
53+ maybe_distro_version : str ,
54+ ) -> LegacyDistroVersion | SemverDistroVersion :
55+ version_components = maybe_distro_version .split ("." )
56+ if len (version_components ) > 3 or len (version_components ) < 2 :
57+ raise UnsupportedDistroVersion (
58+ f"Unexpected version number format { maybe_distro_version } "
59+ )
60+
61+ if not all (map (lambda x : x .isdigit (), version_components )):
62+ raise UnsupportedDistroVersion (
63+ f"Unexpected version number format { maybe_distro_version } "
64+ )
65+
66+ if len (version_components ) == 2 :
67+ return LegacyDistroVersion (* (int (c ) for c in version_components ))
68+ elif len (version_components ) == 3 :
69+ return SemverDistroVersion (* (int (c ) for c in version_components ))
70+ else :
71+ raise UnsupportedDistroVersion (
72+ f"Unexpected number of version components: { maybe_distro_version } "
73+ )
0 commit comments