Skip to content

Commit 8cf92b3

Browse files
committed
chore: use StatusStr for known status values
1 parent 99d8fc1 commit 8cf92b3

3 files changed

Lines changed: 50 additions & 20 deletions

File tree

juju/status.py

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,70 @@
33
from __future__ import annotations
44

55
import logging
6+
import sys
67
import warnings
78

9+
if sys.version_info >= (3, 11):
10+
from enum import StrEnum
11+
else:
12+
from backports.strenum import StrEnum
13+
814
from .client import client
915

1016
log = logging.getLogger(__name__)
1117

1218

13-
def derive_status(statuses: list[str]):
19+
class StatusStr(StrEnum):
20+
"""Recognised status values.
21+
22+
Please keep this set exact same as the severity map below.
23+
"""
24+
25+
error = "error"
26+
blocked = "blocked"
27+
waiting = "waiting"
28+
maintenance = "maintenance"
29+
active = "active"
30+
terminated = "terminated"
31+
unknown = "unknown"
32+
33+
34+
""" severity_map holds status values with a severity measure.
35+
Status values with higher severity are used in preference to others.
36+
"""
37+
severity_map: dict[StatusStr, int] = {
38+
# FIXME: Juju defines a lot more status values #1204
39+
StatusStr.error: 100,
40+
StatusStr.blocked: 90,
41+
StatusStr.waiting: 80,
42+
StatusStr.maintenance: 70,
43+
StatusStr.active: 60,
44+
StatusStr.terminated: 50,
45+
StatusStr.unknown: 40,
46+
}
47+
48+
49+
def derive_status(statuses: list[str | StatusStr]) -> StatusStr:
1450
"""Derive status from a set.
1551
1652
derive_status is used to determine the application status from a set of unit
1753
status values.
1854
1955
:param statuses: list of known unit workload statuses
2056
"""
21-
current = "unknown"
57+
current: StatusStr = StatusStr.unknown
2258
for status in statuses:
23-
if status in severity_map and severity_map[status] > severity_map[current]:
59+
try:
60+
status = StatusStr(status)
61+
except ValueError:
62+
continue
63+
if (new_level := severity_map.get(status)) and new_level > severity_map[
64+
current
65+
]:
2466
current = status
2567
return current
2668

2769

28-
""" severity_map holds status values with a severity measure.
29-
Status values with higher severity are used in preference to others.
30-
"""
31-
severity_map = {
32-
"error": 100,
33-
"blocked": 90,
34-
"waiting": 80,
35-
"maintenance": 70,
36-
"active": 60,
37-
"terminated": 50,
38-
"unknown": 40,
39-
}
40-
41-
4270
async def formatted_status(model, target=None, raw=False, filters=None):
4371
"""Returns a string that mimics the content of the information
4472
returned in the juju status command. If the raw parameter is

pyproject.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description = "Python library for Juju"
55
readme = "docs/readme.rst"
66
license = { file = "LICENSE" }
77
maintainers = [{name = "Juju Ecosystem Engineering", email = "juju@lists.ubuntu.com"}]
8-
requires-python = ">=3.8"
8+
requires-python = ">=3.8.6"
99
classifiers = [
1010
"Development Status :: 5 - Production/Stable",
1111
"Intended Audience :: Developers",
@@ -33,7 +33,8 @@ dependencies = [
3333
"kubernetes>=12.0.1,<31.0.0",
3434
"hvac",
3535
"packaging",
36-
"typing-extensions>=4.5.0"
36+
"typing-extensions>=4.5.0",
37+
"backports.strenum>=1.3.1",
3738
]
3839

3940
[project.urls]
@@ -212,7 +213,7 @@ ignore = [
212213
[tool.pyright]
213214
# These are tentative
214215
include = ["**/*.py"]
215-
pythonVersion = "3.8"
216+
pythonVersion = "3.8.6"
216217
typeCheckingMode = "strict"
217218
useLibraryCodeForTypes = true
218219
reportGeneralTypeIssues = true

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"hvac",
3333
"packaging",
3434
"typing-extensions>=4.5.0",
35+
"backports.strenum",
3536
],
3637
include_package_data=True,
3738
maintainer="Juju Ecosystem Engineering",

0 commit comments

Comments
 (0)