Форматирование всего серверного кода по стандартам (black)
This commit is contained in:
parent
e61ffd793c
commit
06a5a758a3
28 changed files with 611 additions and 305 deletions
|
|
@ -10,34 +10,39 @@ import sdbus
|
|||
from sdbus_async.networkmanager.settings import ConnectionProfile
|
||||
from services.arp_service import NetworkScanner
|
||||
|
||||
|
||||
class IPService:
|
||||
"""
|
||||
Сервис проверки доступности IP-адреса и применения настроек сети
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.arp_scanner = NetworkScanner()
|
||||
self.bus = sdbus.sd_bus_open_system()
|
||||
sdbus.set_default_bus(self.bus)
|
||||
|
||||
def _reset_bus(self):
|
||||
""" Перезапускает соединение с системной шиной D-Bus """
|
||||
"""Перезапускает соединение с системной шиной D-Bus"""
|
||||
new_bus = sdbus.sd_bus_open_system()
|
||||
sdbus.set_default_bus(new_bus)
|
||||
if hasattr(self, 'bus') and self.bus is not None:
|
||||
if hasattr(self, "bus") and self.bus is not None:
|
||||
self.bus = None
|
||||
self.bus = new_bus
|
||||
|
||||
async def check_ip_config(self, args: Dict[str, Any]) -> Dict[str, Any]:
|
||||
""" Проверяет доступность IP-адреса перед его применением """
|
||||
"""Проверяет доступность IP-адреса перед его применением"""
|
||||
self._reset_bus()
|
||||
clients = self.arp_scanner.scan_network()
|
||||
new_ip = args["ipv4_updates"]["ipv4.address-data"][0]["address"]
|
||||
if any(client['ip'] == new_ip for client in clients):
|
||||
if any(client["ip"] == new_ip for client in clients):
|
||||
return {"available": False, "message": f"IP address {new_ip} уже занят."}
|
||||
return {"available": True, "message": "Network profile settings are available for use."}
|
||||
return {
|
||||
"available": True,
|
||||
"message": "Network profile settings are available for use.",
|
||||
}
|
||||
|
||||
async def apply_ip_config(self, args: Dict[str, Any]) -> None:
|
||||
""" Применяет проверенные настройки IP """
|
||||
"""Применяет проверенные настройки IP"""
|
||||
self._reset_bus()
|
||||
nm = NetworkManager()
|
||||
settings = NetworkManagerSettings()
|
||||
|
|
@ -51,7 +56,7 @@ class IPService:
|
|||
settings_dict = profile.to_settings_dict()
|
||||
|
||||
for key_path, value in args["ipv4_updates"].items():
|
||||
section, *props = key_path.split('.')
|
||||
section, *props = key_path.split(".")
|
||||
current = settings_dict[section]
|
||||
for prop in props[:-1]:
|
||||
current = current.setdefault(prop, {})
|
||||
|
|
@ -61,14 +66,16 @@ class IPService:
|
|||
await connection.update(new_profile.to_dbus())
|
||||
print(f"Обновлены настройки для {args['connection_id']}:")
|
||||
|
||||
interface_name = settings_dict['connection']['interface-name']
|
||||
interface_name = settings_dict["connection"]["interface-name"]
|
||||
devices = await nm.get_devices()
|
||||
for device_path in devices:
|
||||
device = NetworkDeviceGeneric(device_path)
|
||||
if await device.interface == interface_name:
|
||||
await device.disconnect()
|
||||
print(f"Отключён от {interface_name}")
|
||||
await nm.activate_connection(connection=connection_paths[0], device=device_path)
|
||||
await nm.activate_connection(
|
||||
connection=connection_paths[0], device=device_path
|
||||
)
|
||||
print(f"Переподключён к {args['connection_id']}")
|
||||
return
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue