22"""
33Track version information for sightmachine-sdk module here.
44"""
5- import typing
5+ import datetime
6+ import typing as t_
67import itertools
78import requests
89import warnings
910import functools
1011import re
1112
1213
13- class VersionInfo (typing .NamedTuple ):
14- major : typing . Any
15- minor : typing . Any
16- patchlevel : typing . Any
17- releaselevel : typing . Any
18- serial : typing . Any
14+ class VersionInfo (t_ .NamedTuple ):
15+ major : int
16+ minor : int
17+ patchlevel : int
18+ releaselevel : t_ . Optional [ str ]
19+ serial : t_ . Optional [ int ]
1920
2021 def __str__ (self ) -> str :
2122 result = "" .join (
@@ -32,19 +33,14 @@ def __str__(self) -> str:
3233
3334
3435def sort_releases_descending (
35- releases : typing .List [typing .Dict [str , typing .Any ]]
36- ) -> typing .List [typing .Dict [str , typing .Any ]]:
37- def version_key (
38- release : typing .Dict [str , typing .Any ]
39- ) -> typing .Union [typing .Tuple [int , int , int , str ], typing .Tuple [()]]:
40- version = release ["tag_name" ]
41- # Use regular expression to extract numerical and string parts
42- match = re .match (r"(?i)v(\d+)\.(\d+)\.(\d+)(\D*)" , version )
43- if match :
44- major , minor , patch , suffix = map (match .group , (1 , 2 , 3 , 4 ))
45- return int (major ), int (minor ), int (patch ), suffix
46- else :
47- return ()
36+ releases : t_ .List [t_ .Dict [str , t_ .Any ]]
37+ ) -> t_ .List [t_ .Dict [str , t_ .Any ]]:
38+ def version_key (release : t_ .Dict [str , t_ .Any ]) -> t_ .List [t_ .Union [int , str ]]:
39+ return [
40+ (int (i ) if i .isdigit () else i )
41+ for i in re .split (r"(\d+|\W+)" , release ["tag_name" ].lower ())
42+ if i
43+ ]
4844
4945 return (
5046 sorted (releases , key = version_key , reverse = True )
@@ -54,15 +50,15 @@ def version_key(
5450
5551
5652def get_latest_release_version (
57- releases : typing .List [typing .Dict [str , typing .Any ]]
58- ) -> typing .Optional [typing .Any ]:
53+ releases : t_ .List [t_ .Dict [str , t_ .Any ]]
54+ ) -> t_ .Optional [t_ .Any ]:
5955 if releases :
6056 sorted_releases = sort_releases_descending (releases )
6157 return sorted_releases [0 ]["tag_name" ]
6258 return None
6359
6460
65- def get_latest_sdk_release () -> typing .Optional [typing .Any ]:
61+ def get_latest_sdk_release () -> t_ .Optional [t_ .Any ]:
6662 api_url = f"https://api.github.com/repos/{ owner } /{ repo } /releases"
6763
6864 try :
@@ -80,11 +76,21 @@ def get_latest_sdk_release() -> typing.Optional[typing.Any]:
8076
8177class VersionCheckDecorator :
8278 api_version_printed = False
79+ last_version_check_time = None
8380
8481 @classmethod
85- def version_check_decorator (cls , func : typing .Any ) -> typing .Any :
82+ def version_check_decorator (cls , func : t_ .Any ) -> t_ .Any :
8683 @functools .wraps (func )
87- def wrapper (* args : typing .Any , ** kwargs : typing .Any ) -> typing .Any :
84+ def wrapper (* args : t_ .Any , ** kwargs : t_ .Any ) -> t_ .Any :
85+ current_time = datetime .datetime .now ()
86+
87+ # Check if a week has passed since the last version check
88+ if cls .last_version_check_time is None or (
89+ current_time - cls .last_version_check_time > datetime .timedelta (days = 7 )
90+ ):
91+ cls .api_version_printed = False
92+ cls .last_version_check_time = current_time
93+
8894 if not cls .api_version_printed :
8995 latest_sdk_release = get_latest_sdk_release ()
9096 installed_sdk_release = version
@@ -111,3 +117,6 @@ def wrapper(*args: typing.Any, **kwargs: typing.Any) -> typing.Any:
111117
112118owner = "sightmachine"
113119repo = "sightmachine-sdk"
120+
121+ # Define version_check_decorator here
122+ version_check_decorator = VersionCheckDecorator .version_check_decorator
0 commit comments