Skip to content

Commit 2fb6f54

Browse files
committed
Add missing type hints to sidebar snapins
Change-Id: Ib9a9715624498239129f65e7f8e4f68ae8baf53d
1 parent 979e9e4 commit 2fb6f54

7 files changed

Lines changed: 78 additions & 62 deletions

File tree

cmk/gui/nagvis/_nagvis_maps.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,44 @@
33
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
44
# conditions defined in the file COPYING, which is part of this source code package.
55

6+
from collections.abc import Mapping
7+
from typing import Any
8+
69
from cmk.gui.config import Config
710
from cmk.gui.htmllib.foldable_container import foldable_container
811
from cmk.gui.htmllib.html import html
912
from cmk.gui.http import request
1013
from cmk.gui.i18n import _
11-
from cmk.gui.sidebar._snapin._base import SidebarSnapin
12-
from cmk.gui.sidebar._snapin._helpers import footnotelinks
14+
from cmk.gui.sidebar import footnotelinks, PageHandlers, SidebarSnapin
1315

1416

1517
class NagVisMaps(SidebarSnapin):
1618
@staticmethod
17-
def type_name():
19+
def type_name() -> str:
1820
return "nagvis_maps"
1921

2022
@classmethod
21-
def title(cls):
23+
def title(cls) -> str:
2224
return _("NagVis maps")
2325

2426
@classmethod
25-
def description(cls):
27+
def description(cls) -> str:
2628
return _("List of available NagVis maps")
2729

2830
@classmethod
29-
def refresh_regularly(cls):
31+
def refresh_regularly(cls) -> bool:
3032
return False
3133

3234
def show(self, config: Config) -> None:
3335
html.div(_("Loading maps..."), class_="loading")
3436
html.javascript("cmk.sidebar.fetch_nagvis_snapin_contents()")
3537

36-
def page_handlers(self):
38+
def page_handlers(self) -> PageHandlers:
3739
return {
3840
"ajax_nagvis_maps_snapin": self._ajax_show_nagvis_maps_snapin,
3941
}
4042

41-
def _ajax_show_nagvis_maps_snapin(self):
43+
def _ajax_show_nagvis_maps_snapin(self) -> None:
4244
api_request = request.get_request()
4345
if api_request["type"] == "table":
4446
self._show_table(api_request)
@@ -51,7 +53,7 @@ def _ajax_show_nagvis_maps_snapin(self):
5153

5254
self._show_footnote_links()
5355

54-
def _show_table(self, api_request):
56+
def _show_table(self, api_request: Mapping[str, Any]) -> None:
5557
html.open_table(class_="allhosts")
5658
html.open_tbody()
5759

@@ -63,9 +65,9 @@ def _show_table(self, api_request):
6365
class_=[
6466
"statebullet",
6567
self._state_class(map_cfg),
66-
self._sub_state_class(map_cfg),
67-
self._stale_class(map_cfg),
68-
],
68+
]
69+
+ self._sub_state_class(map_cfg)
70+
+ self._stale_class(map_cfg),
6971
title=self._state_title(map_cfg),
7072
)
7173
html.a(map_cfg["alias"], href=map_cfg["url"], class_="link", target="main")
@@ -75,7 +77,7 @@ def _show_table(self, api_request):
7577
html.close_tbody()
7678
html.close_table()
7779

78-
def _state_class(self, map_cfg):
80+
def _state_class(self, map_cfg: Mapping[str, Any]) -> str:
7981
return {
8082
"OK": "state0",
8183
"UP": "state0",
@@ -86,19 +88,19 @@ def _state_class(self, map_cfg):
8688
"PENDING": "statep",
8789
}.get(map_cfg["summary_state"], "state3")
8890

89-
def _sub_state_class(self, map_cfg):
91+
def _sub_state_class(self, map_cfg: Mapping[str, Any]) -> list[str]:
9092
if map_cfg["summary_in_downtime"]:
91-
return "stated"
93+
return ["stated"]
9294
if map_cfg["summary_problem_has_been_acknowledged"]:
93-
return "statea"
94-
return None
95+
return ["statea"]
96+
return []
9597

96-
def _stale_class(self, map_cfg):
98+
def _stale_class(self, map_cfg: Mapping[str, Any]) -> list[str]:
9799
if map_cfg["summary_stale"]:
98-
return "stale"
99-
return None
100+
return ["stale"]
101+
return []
100102

101-
def _state_title(self, map_cfg):
103+
def _state_title(self, map_cfg: Mapping[str, Any]) -> str:
102104
title = map_cfg["summary_state"]
103105

104106
if map_cfg["summary_in_downtime"]:
@@ -115,16 +117,20 @@ def _state_title(self, map_cfg):
115117

116118
return title
117119

118-
def _show_footnote_links(self):
120+
def _show_footnote_links(self) -> None:
119121
edit_url = "../nagvis/"
120122
footnotelinks([(_("Edit"), edit_url)])
121123

122-
def _show_tree(self, api_request):
124+
def _show_tree(self, api_request: Mapping[str, Any]) -> None:
123125
html.open_ul()
124126
self._show_tree_nodes(api_request["maps"]["maps"], api_request["maps"]["childs"])
125127
html.close_ul()
126128

127-
def _show_tree_nodes(self, maps, children):
129+
def _show_tree_nodes(
130+
self,
131+
maps: Mapping[str, Mapping[str, Any]],
132+
children: Mapping[str, Mapping[str, Mapping[str, Any]]],
133+
) -> None:
128134
for map_name, map_cfg in maps.items():
129135
html.open_li()
130136
if map_name in children:

cmk/gui/sidebar/_snapin/_groups.py

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
from cmk.gui import sites
99
from cmk.gui.config import Config
10+
from cmk.gui.groups import GroupType
1011
from cmk.gui.htmllib.html import html
1112
from cmk.gui.i18n import _
1213
from cmk.gui.utils.urls import urlencode
@@ -17,13 +18,22 @@
1718

1819
class GroupSnapin(SidebarSnapin, abc.ABC):
1920
@abc.abstractmethod
20-
def _group_type_ident(self):
21-
raise NotImplementedError()
21+
def _group_type_ident(self) -> str: ...
2222

2323
def show(self, config: Config) -> None:
2424
group_type = self._group_type_ident()
25+
26+
grouped_type: GroupType
27+
match group_type:
28+
case "hostgroup":
29+
grouped_type = "host"
30+
case "servicegroup":
31+
grouped_type = "service"
32+
case _:
33+
raise ValueError(f"Unknown group type: {group_type}")
34+
2535
html.open_ul()
26-
for name, alias in sites.all_groups(group_type.replace("group", "")):
36+
for name, alias in sites.all_groups(grouped_type):
2737
url = f"view.py?view_name={group_type}&{group_type}={urlencode(name)}"
2838
bulletlink(alias or name, url)
2939
html.close_ul()
@@ -34,34 +44,34 @@ def refresh_on_restart(cls):
3444

3545

3646
class HostGroups(GroupSnapin):
37-
def _group_type_ident(self):
47+
def _group_type_ident(self) -> str:
3848
return "hostgroup"
3949

4050
@staticmethod
41-
def type_name():
51+
def type_name() -> str:
4252
return "hostgroups"
4353

4454
@classmethod
45-
def title(cls):
55+
def title(cls) -> str:
4656
return _("Host groups")
4757

4858
@classmethod
49-
def description(cls):
59+
def description(cls) -> str:
5060
return _("Directs links to all host groups")
5161

5262

5363
class ServiceGroups(GroupSnapin):
54-
def _group_type_ident(self):
64+
def _group_type_ident(self) -> str:
5565
return "servicegroup"
5666

5767
@staticmethod
58-
def type_name():
68+
def type_name() -> str:
5969
return "servicegroups"
6070

6171
@classmethod
62-
def title(cls):
72+
def title(cls) -> str:
6373
return _("Service groups")
6474

6575
@classmethod
66-
def description(cls):
76+
def description(cls) -> str:
6777
return _("Direct links to all service groups")

cmk/gui/sidebar/_snapin/_performance.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,23 @@
1616

1717
class Performance(SidebarSnapin):
1818
@staticmethod
19-
def type_name():
19+
def type_name() -> str:
2020
return "performance"
2121

2222
@classmethod
23-
def title(cls):
23+
def title(cls) -> str:
2424
return _("Server performance")
2525

2626
@classmethod
2727
def has_show_more_items(cls) -> bool:
2828
return True
2929

3030
@classmethod
31-
def description(cls):
31+
def description(cls) -> str:
3232
return _("Live monitor of the overall performance of all monitoring servers")
3333

3434
@classmethod
35-
def refresh_regularly(cls):
35+
def refresh_regularly(cls) -> bool:
3636
return True
3737

3838
def show(self, config: Config) -> None:
@@ -94,7 +94,7 @@ def write_line(left, right, show_more):
9494
html.close_table()
9595

9696
@classmethod
97-
def refresh_on_restart(cls):
97+
def refresh_on_restart(cls) -> bool:
9898
return True
9999

100100
@classmethod

cmk/gui/sidebar/_snapin/_search.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -701,15 +701,15 @@ def __init__(self) -> None:
701701
super().__init__()
702702

703703
@classmethod
704-
def type_name(cls):
704+
def type_name(cls) -> str:
705705
return "search"
706706

707707
@classmethod
708-
def title(cls):
708+
def title(cls) -> str:
709709
return _("Quicksearch")
710710

711711
@classmethod
712-
def description(cls):
712+
def description(cls) -> str:
713713
return _(
714714
"Interactive search field for direct access to monitoring instances (hosts, services, "
715715
"host and service groups).<br>You can use the following filters: <i>h:</i> Host,<br> "
@@ -908,7 +908,7 @@ def get_matches(
908908

909909

910910
class MatchPluginRegistry(cmk.ccc.plugin_registry.Registry[ABCMatchPlugin]):
911-
def plugin_name(self, instance):
911+
def plugin_name(self, instance: ABCMatchPlugin) -> str:
912912
return instance.name
913913

914914
def get_livestatus_match_plugins(self) -> list[ABCLivestatusMatchPlugin]:

cmk/gui/sidebar/_snapin/_server_time.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@
1414

1515
class CurrentTime(SidebarSnapin):
1616
@staticmethod
17-
def type_name():
17+
def type_name() -> str:
1818
return "time"
1919

2020
@classmethod
21-
def title(cls):
21+
def title(cls) -> str:
2222
return _("Server time")
2323

2424
@classmethod
25-
def description(cls):
25+
def description(cls) -> str:
2626
return _("A large clock showing the current time of the web server")
2727

2828
@classmethod
29-
def refresh_regularly(cls):
29+
def refresh_regularly(cls) -> bool:
3030
return True
3131

3232
def show(self, config: Config) -> None:

cmk/gui/sidebar/_snapin/_site_status.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,25 @@
1818
from cmk.gui.utils.html import HTML
1919
from cmk.gui.utils.urls import makeuri_contextless
2020

21-
from ._base import SidebarSnapin
21+
from ._base import PageHandlers, SidebarSnapin
2222
from ._helpers import begin_footnote_links, end_footnote_links, link, render_link
2323

2424

2525
class SiteStatus(SidebarSnapin):
2626
@staticmethod
27-
def type_name():
27+
def type_name() -> str:
2828
return "sitestatus"
2929

3030
@classmethod
31-
def refresh_regularly(cls):
31+
def refresh_regularly(cls) -> bool:
3232
return True
3333

3434
@classmethod
35-
def title(cls):
35+
def title(cls) -> str:
3636
return _("Site status")
3737

3838
@classmethod
39-
def description(cls):
39+
def description(cls) -> str:
4040
return _(
4141
"Connection state of each site and button for enabling "
4242
"and disabling the site connection"
@@ -118,13 +118,13 @@ def show(self, config: Config) -> None:
118118
def allowed_roles(cls) -> list[RoleName]:
119119
return ["user", "admin"]
120120

121-
def page_handlers(self):
121+
def page_handlers(self) -> PageHandlers:
122122
return {
123123
"switch_site": self._ajax_switch_site,
124124
"set_all_sites": self._ajax_set_all_sites,
125125
}
126126

127-
def _ajax_switch_site(self):
127+
def _ajax_switch_site(self) -> None:
128128
check_csrf_token()
129129
response.set_content_type("application/json")
130130
# _site_switch=sitename1:on,sitename2:off,...
@@ -146,7 +146,7 @@ def _ajax_switch_site(self):
146146

147147
user.save_site_config()
148148

149-
def _ajax_set_all_sites(self):
149+
def _ajax_set_all_sites(self) -> None:
150150
sites.update_site_states_from_dead_sites()
151151
new_state = request.var("_new_state")
152152

cmk/gui/sidebar/_snapin/_speedometer.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@
1313
from cmk.gui.theme.current_theme import theme
1414
from cmk.gui.type_defs import RoleName
1515

16-
from ._base import SidebarSnapin
16+
from ._base import PageHandlers, SidebarSnapin
1717
from ._helpers import snapin_width
1818

1919

2020
class Speedometer(SidebarSnapin):
2121
@staticmethod
22-
def type_name():
22+
def type_name() -> str:
2323
return "speedometer"
2424

2525
@classmethod
26-
def title(cls):
26+
def title(cls) -> str:
2727
return _("Service Speed-O-Meter")
2828

2929
@classmethod
30-
def description(cls):
30+
def description(cls) -> str:
3131
return _(
3232
"A gadget that shows your current service check rate in relation to "
3333
"the scheduled check rate. If the Speed-O-Meter shows a speed "
@@ -47,12 +47,12 @@ def show(self, config: Config) -> None:
4747
def allowed_roles(cls) -> list[RoleName]:
4848
return ["admin"]
4949

50-
def page_handlers(self):
50+
def page_handlers(self) -> PageHandlers:
5151
return {
5252
"sidebar_ajax_speedometer": self._ajax_speedometer,
5353
}
5454

55-
def _ajax_speedometer(self):
55+
def _ajax_speedometer(self) -> None:
5656
response.set_content_type("application/json")
5757
try:
5858
# Try to get values from last call in order to compute

0 commit comments

Comments
 (0)