Skip to content

Commit 833deb7

Browse files
committed
fixes, WindowTitle, update ip only if changed
1 parent d87ef91 commit 833deb7

File tree

5 files changed

+103
-55
lines changed

5 files changed

+103
-55
lines changed

programershell/bar.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,36 @@
55
from indicators.CPUIndicator import CPUIndicator
66
from indicators.NetworkIndicator import NetworkIndicator
77
from indicators.CurrentWindow import CurrentWindowIndicator
8-
9-
10-
def create_indicator(indicator_type, config):
11-
if indicator_type == "memory" and config["enabled"]:
12-
return MemoryIndicator(config["update_interval"])
13-
elif indicator_type == "cpu" and config["enabled"]:
14-
return CPUIndicator(config["update_interval"])
15-
elif indicator_type == "clock" and config["enabled"]:
16-
return ClockIndicator(config["update_interval"])
17-
elif indicator_type == "battery" and config["enabled"]:
18-
return BatteryIndicator(config["update_interval"])
19-
elif indicator_type == "network" and config["enabled"]:
20-
return NetworkIndicator(config["update_interval"])
8+
from abstracts.StatusIndicator import StatusIndicator
9+
10+
11+
def create_indicator(indicator_type, config, monitor_id) -> StatusIndicator | None:
12+
lres = None
13+
if indicator_type == "memory":
14+
lres = MemoryIndicator(config["update_interval"])
15+
elif indicator_type == "cpu":
16+
lres = CPUIndicator(config["update_interval"])
17+
elif indicator_type == "clock":
18+
lres = ClockIndicator(config["update_interval"])
19+
elif indicator_type == "battery":
20+
lres = BatteryIndicator(config["update_interval"])
21+
elif indicator_type == "network":
22+
lres = NetworkIndicator(config["update_interval"])
2123
elif indicator_type == "current_window":
22-
return CurrentWindowIndicator(config["update_interval"])
24+
lres = CurrentWindowIndicator(config["update_interval"])
25+
else:
26+
return None
27+
if lres is not None:
28+
lres.monitor_id = monitor_id
29+
return lres
2330
else:
2431
return None
2532

2633

2734
class ShellWindow:
2835
def __init__(self, screen, indicators_config, position) -> None:
2936
self.indicators_config = indicators_config
30-
37+
self.screen = screen
3138
self.window = Gtk.Window()
3239
self.window.set_default_size(600, 30)
3340

@@ -120,13 +127,8 @@ def setupWindow(self):
120127
self.stack.set_transition_type(Gtk.StackTransitionType.SLIDE_LEFT_RIGHT)
121128
self.stack.set_transition_duration(1000)
122129

123-
# stack_switcher = Gtk.StackSwitcher()
124-
# stack_switcher.set_stack(self.stack)
125-
126-
# Index label
127130
self.index_label = Gtk.Label()
128131
hbox.pack_start(self.index_label, False, False, 0)
129-
# hbox.pack_start(stack_switcher, False, False, 0)
130132

131133
def on_stack_notify(stack, param):
132134
visible_child_name = stack.get_visible_child_name()
@@ -153,9 +155,9 @@ def on_stack_notify(stack, param):
153155
box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=10)
154156
for indicator_type, position in indicators:
155157
indicator = create_indicator(
156-
indicator_type, self.indicators_config[indicator_type]
158+
indicator_type, self.indicators_config[indicator_type], self.screen
157159
)
158-
if indicator:
160+
if indicator is not None:
159161
if position == "start":
160162
box.pack_start(indicator, False, False, 0)
161163
elif position == "center":

programershell/indicators/CurrentWindow.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
class CurrentWindowIndicator(StatusIndicator):
88
def __init__(self, update_interval):
99
super().__init__("utilities-system-monitor-symbolic", "Mem: ", update_interval)
10+
self.label.set_text("".ljust(45, " "))
1011
register_function("hypr", self.update_with_data)
1112

1213
def update_with_data(self, data=None):
1314
if data is not None:
1415
if data["type"] == "activewindow":
1516
GLib.idle_add(
16-
self._safe_update_label, "\n".join(str(data["data"]).split(",", 1))
17+
self._safe_update_label,
18+
(str(data["data"]).split(",", 1)[1][:40].ljust(45, " ")),
1719
)
1820
# self.label.set_text(data["type"])
1921
return True # Return True to keep the timeout active

programershell/indicators/NetworkIndicator.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ def __init__(self, update_interval):
1313
register_function("updateip", self.update)
1414

1515
def update(self):
16-
self.label.set_text(" ".join(self.network.ip4s))
16+
self.label.set_text(
17+
" ".join([", ".join(i.ip4.ip) for i in self.network.Network.Interfaces])
18+
)
1719
return True

programershell/service/Network.py

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
NetworkDeviceWireless,
55
IPv4Config,
66
)
7-
from share.decoratos import call_registered_functions
87
from sdbus import sd_bus_open_system
98
from threading import Thread
10-
from time import sleep
9+
from time import sleep, time
1110
from enum import Enum
11+
from share.decoratos import call_registered_functions
1212

1313

1414
class InterfaceType(Enum):
15-
WIFI = 1
16-
ETHERNET = 2
15+
ETHERNET = 1
16+
WIFI = 2
1717

1818

1919
class InterfaceIP4Config:
@@ -22,7 +22,7 @@ def __init__(self) -> None:
2222
self.gateway: list[str] = []
2323

2424
def __repr__(self) -> str:
25-
return "".join(self.ip).join(self.gateway)
25+
return f"IP: {', '.join(self.ip)}, Gateway: {', '.join(self.gateway)}"
2626

2727

2828
class Interface:
@@ -32,70 +32,108 @@ def __init__(self, type: InterfaceType, name: str) -> None:
3232
self.name = name
3333

3434
def __repr__(self) -> str:
35-
return "".join(str(self.type.value)).join(str(self.ip4))
35+
return f"Type: {self.type.name}, Name: {self.name}, {self.ip4}"
3636

3737

3838
class EthernetInterface(Interface):
39-
pass
39+
def __init__(self, name: str) -> None:
40+
super().__init__(InterfaceType.ETHERNET, name)
4041

4142

4243
class WifiInterface(Interface):
43-
def __init__(self, type: InterfaceType, name: str) -> None:
44-
super().__init__(type, name)
44+
def __init__(self, name: str) -> None:
45+
super().__init__(InterfaceType.WIFI, name)
4546
self.ssid: str = ""
4647

48+
def __repr__(self) -> str:
49+
return f"SSID: {self.ssid}, {super().__repr__()}"
50+
4751

4852
class NetworkObject:
4953
def __init__(self) -> None:
5054
self.Interfaces: list[Interface] = []
5155

5256
def PushObject(self, obj: Interface) -> None:
53-
for int in self.Interfaces:
54-
if str(int) == str(obj):
57+
for intf in self.Interfaces:
58+
if str(intf) == str(obj):
5559
return
5660
self.Interfaces.append(obj)
5761

5862
def pushEthernetObject(self, interfacename: str, ip4s: list[str], gw: list[str]):
59-
pass
63+
eth_int = EthernetInterface(interfacename)
64+
eth_int.ip4.ip = ip4s
65+
eth_int.ip4.gateway = gw
66+
self.PushObject(eth_int)
6067

6168
def pushWifiObject(
6269
self, interfacename: str, ssid: str, ip4s: list[str], gw: list[str]
6370
):
64-
pass
71+
wifi_int = WifiInterface(interfacename)
72+
wifi_int.ssid = ssid
73+
wifi_int.ip4.ip = ip4s
74+
wifi_int.ip4.gateway = gw
75+
self.PushObject(wifi_int)
6576

6677

6778
class NetworkService(Thread):
6879
def __init__(self):
6980
super().__init__()
7081
self.Network: NetworkObject = NetworkObject()
82+
self.last_network_state = None
83+
self.last_call_time = time()
84+
85+
def has_state_changed(self):
86+
current_state = str(self.Network.Interfaces)
87+
if self.last_network_state != current_state:
88+
self.last_network_state = current_state
89+
return True
90+
return False
7191

7292
def run(self) -> None:
7393
system_bus = sd_bus_open_system() # We need system bus
7494
nm = NetworkManager(system_bus)
7595
while True:
7696
try:
7797
devices_paths = nm.get_devices()
78-
new_ips = []
98+
self.Network.Interfaces = [] # Reset the network interfaces list
7999
for device_path in devices_paths:
80100
generic_device = NetworkDeviceGeneric(device_path, system_bus)
81-
# print(generic_device.device_type, generic_device.interface)
82-
if generic_device.device_type == 2:
83-
pass
84-
# print(NetworkDeviceWireless(device_path, system_bus).get_applied_connection()[0]['connection'])
85101
device_ip4_conf_path = generic_device.ip4_config
86102
if device_ip4_conf_path == "/":
87103
continue
88104
else:
89105
ip4_conf = IPv4Config(device_ip4_conf_path, system_bus)
90-
for address_data in ip4_conf.address_data:
91-
# print(' Ip Adress:', address_data['address'][1])
92-
new_ips.append(address_data["address"][1])
93-
self.ip4s = new_ips
94-
call_registered_functions("updateip", None)
106+
ip_addresses = [
107+
address_data["address"][1]
108+
for address_data in ip4_conf.address_data
109+
]
110+
gw = str(ip4_conf.gateway) if ip4_conf.gateway else "0.0.0.0"
111+
gateways: list[str] = [gw]
112+
if generic_device.device_type == InterfaceType.ETHERNET.value:
113+
self.Network.pushEthernetObject(
114+
generic_device.interface, ip_addresses, gateways
115+
)
116+
elif generic_device.device_type == InterfaceType.WIFI.value:
117+
wifi_device = NetworkDeviceWireless(device_path, system_bus)
118+
self.Network.pushWifiObject(
119+
generic_device.interface,
120+
"",
121+
ip_addresses,
122+
gateways,
123+
)
124+
125+
current_time = time()
126+
if (
127+
self.has_state_changed()
128+
or (current_time - self.last_call_time) >= 10
129+
):
130+
call_registered_functions("updateip", None)
131+
self.last_call_time = current_time
95132

96133
sleep(1)
97134
except Exception as e:
98135
print(e)
136+
sleep(1)
99137

100138

101139
ns: NetworkService | None = None

programershell/share/decoratos.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import inspect
2+
import datetime
23

34
registered_functions = []
45

@@ -10,11 +11,14 @@ def register_function(cat, method):
1011

1112
def call_registered_functions(icat, data=None):
1213
for cat, method in registered_functions:
13-
if icat == cat:
14-
print("[{:<10}] calling update".format(cat))
15-
if data is None:
16-
method()
17-
else:
18-
# print("with args, data:", data)
19-
print(inspect.signature(method))
20-
method(data)
14+
if callable(method):
15+
if icat == cat:
16+
print(
17+
f"{datetime.datetime.now()} ", "[{:<10}] calling update".format(cat)
18+
)
19+
if data is None:
20+
method()
21+
else:
22+
# print("with args, data:", data)
23+
# print(inspect.signature(method))
24+
method(data)

0 commit comments

Comments
 (0)