Skip to content

Commit fbc9d24

Browse files
committed
Add support for Python 3.12, remove use of deprecated distutils
1 parent 83b5f18 commit fbc9d24

File tree

9 files changed

+27
-22
lines changed

9 files changed

+27
-22
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ jobs:
3333
matrix:
3434
os: ["ubuntu-20.04"]
3535
python-version: [
36-
'3.6', '3.11',
36+
'3.6', '3.12',
3737
'pypy-3.6', 'pypy-3.10',
3838
]
3939
steps:

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## unreleased
44

5+
* Add support for Python 3.12, remove use of deprecated `distutils`.
6+
Thanks, @foreverhy.
7+
58

69
## 3.9.1 (2023-09-21)
710

examples/datasource-health-check.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
import json
77
import logging
88
import sys
9-
from distutils.version import LooseVersion
109
from optparse import OptionParser
1110

1211
import requests
12+
from verlib2 import Version
1313

1414
from grafana_client import GrafanaApi
1515
from grafana_client.model import DatasourceHealthResponse
1616
from grafana_client.util import setup_logging
1717

1818
logger = logging.getLogger(__name__)
1919

20-
VERSION_7 = LooseVersion("7")
20+
VERSION_7 = Version("7")
2121

2222

2323
def run(grafana: GrafanaApi):
@@ -82,7 +82,7 @@ def run(grafana: GrafanaApi):
8282
logger.exception("Connecting to Grafana failed")
8383
raise SystemExit(1)
8484

85-
grafana_version = LooseVersion(grafana_client.version)
85+
grafana_version = Version(grafana_client.version)
8686
if grafana_version < VERSION_7:
8787
raise NotImplementedError(f"Data source health check subsystem not ready for Grafana version {grafana_version}")
8888

examples/datasource-health-probe.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
import json
77
import logging
88
import sys
9-
from distutils.version import LooseVersion
109
from optparse import OptionParser
1110

1211
import requests
12+
from verlib2 import Version
1313

1414
from grafana_client import GrafanaApi
1515
from grafana_client.client import GrafanaClientError
@@ -20,9 +20,9 @@
2020
logger = logging.getLogger(__name__)
2121

2222

23-
VERSION_7 = LooseVersion("7")
24-
VERSION_8 = LooseVersion("8")
25-
VERSION_9 = LooseVersion("9")
23+
VERSION_7 = Version("7")
24+
VERSION_8 = Version("8")
25+
VERSION_9 = Version("9")
2626

2727

2828
def ensure_datasource(grafana: GrafanaApi, datasource: DatasourceModel):
@@ -81,7 +81,7 @@ def prometheus_demo(grafana: GrafanaApi):
8181
return health_info
8282

8383

84-
def run(grafana: GrafanaApi, grafana_version: LooseVersion = None):
84+
def run(grafana: GrafanaApi, grafana_version: Version = None):
8585
# When called without options, invoke the Prometheus demo.
8686
if len(sys.argv) == 1:
8787
if grafana_version < VERSION_8:
@@ -132,7 +132,7 @@ def run(grafana: GrafanaApi, grafana_version: LooseVersion = None):
132132
logger.exception("Connecting to Grafana failed")
133133
raise SystemExit(1)
134134

135-
grafana_version = LooseVersion(grafana_client.version)
135+
grafana_version = Version(grafana_client.version)
136136
if grafana_version < VERSION_7:
137137
raise NotImplementedError(f"Data source health check subsystem not ready for Grafana version {grafana_version}")
138138

grafana_client/__init__.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import warnings
2-
31
try:
42
from importlib.metadata import PackageNotFoundError, version
53
except ModuleNotFoundError: # pragma:nocover
64
from importlib_metadata import PackageNotFoundError, version
75

8-
warnings.filterwarnings("ignore", category=Warning, message="distutils Version classes are deprecated")
96
from .api import GrafanaApi # noqa:E402,F401
107
from .client import HeaderAuth, TokenAuth # noqa:E402,F401
118

grafana_client/elements/dashboard.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
from distutils.version import LooseVersion
1+
from verlib2 import Version
22

33
from .base import Base
44

5+
VERSION_8 = Version("8")
6+
57

68
class Dashboard(Base):
79
def __init__(self, client, api):
@@ -25,7 +27,7 @@ def get_dashboard_by_name(self, dashboard_name):
2527
:param dashboard_name:
2628
:return:
2729
"""
28-
if self.api.version and LooseVersion(self.api.version) >= LooseVersion("8"):
30+
if self.api.version and Version(self.api.version) >= VERSION_8:
2931
raise DeprecationWarning("Grafana 8 and higher does not support getting dashboards by slug")
3032
get_dashboard_path = "/dashboards/db/%s" % dashboard_name
3133
r = self.client.GET(get_dashboard_path)

grafana_client/elements/datasource.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
import logging
33
import time
44
import warnings
5-
from distutils.version import LooseVersion
65
from typing import Dict, Optional, Tuple, Union
76
from urllib.parse import urlencode
87

98
from requests import ReadTimeout
9+
from verlib2 import Version
1010

1111
from ..client import GrafanaBadInputError, GrafanaClientError, GrafanaServerError
1212
from ..knowledge import get_healthcheck_expression, query_factory
@@ -15,9 +15,9 @@
1515

1616
logger = logging.getLogger(__name__)
1717

18-
VERSION_9 = LooseVersion("9")
19-
VERSION_8 = LooseVersion("8")
20-
VERSION_7 = LooseVersion("7")
18+
VERSION_9 = Version("9")
19+
VERSION_8 = Version("8")
20+
VERSION_7 = Version("7")
2121
VERBOSE = False
2222

2323

@@ -375,7 +375,7 @@ def smartquery(
375375
request_kwargs = {}
376376
send_request = self.client.GET
377377

378-
elif datasource_type in ("prometheus", "loki") and LooseVersion(self.api.version) <= VERSION_7:
378+
elif datasource_type in ("prometheus", "loki") and Version(self.api.version) <= VERSION_7:
379379
if (
380380
"queries" in request["data"]
381381
and len(request["data"]["queries"]) > 0
@@ -490,7 +490,7 @@ def health_check(self, datasource: Union[DatasourceIdentifier, Dict]) -> Datasou
490490
message = f"Invalid response. {reason}"
491491

492492
elif datasource_type == "loki":
493-
if self.api.version and VERSION_7 <= LooseVersion(self.api.version) < VERSION_8:
493+
if self.api.version and VERSION_7 <= Version(self.api.version) < VERSION_8:
494494
if "status" in response and response["status"] == "success":
495495
message = "Success"
496496
success = True
@@ -607,7 +607,7 @@ def health_inquiry(self, datasource_uid: str) -> DatasourceHealthResponse:
607607
start = time.time()
608608
raised = True
609609
noop = False
610-
if self.api.version and LooseVersion(self.api.version) >= VERSION_9:
610+
if self.api.version and Version(self.api.version) >= VERSION_9:
611611
try:
612612
health_native = self.health(datasource_uid=datasource_uid)
613613
logger.debug(f"Response from native data source health check: {health_native}")

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,4 @@ test-coverage = [
5656
{cmd="coverage report"},
5757
]
5858
build = {cmd="python -m build"}
59+
check = ["lint", "test"]

setup.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ classifiers =
3636
Programming Language :: Python :: 3.9
3737
Programming Language :: Python :: 3.10
3838
Programming Language :: Python :: 3.11
39+
Programming Language :: Python :: 3.12
3940
Programming Language :: Python :: Implementation :: CPython
4041
Programming Language :: Python :: Implementation :: PyPy
4142
Topic :: Communications
@@ -73,6 +74,7 @@ install_requires =
7374
requests>=2.23.0,<3
7475
dataclasses;python_version<='3.6'
7576
importlib-metadata;python_version<='3.7'
77+
verlib2==0.1.0
7678

7779
[options.extras_require]
7880
test =

0 commit comments

Comments
 (0)