Pep8
This commit is contained in:
parent
06a5a758a3
commit
58fa7d1ee6
19 changed files with 567 additions and 377 deletions
|
|
@ -30,7 +30,8 @@ class NetworkService:
|
|||
self.nm = NetworkManager(self.system_bus)
|
||||
self.nm_settings = NetworkManagerSettings(self.system_bus)
|
||||
|
||||
async def get_ip_method(self, device: NetworkDeviceGeneric, ip_type: str) -> str:
|
||||
async def get_ip_method(self, device: NetworkDeviceGeneric,
|
||||
ip_type: str) -> str:
|
||||
"""Получает метод IP для устройства"""
|
||||
try:
|
||||
config = await device.get_applied_connection()
|
||||
|
|
@ -52,14 +53,14 @@ class NetworkService:
|
|||
# добавить потом условия для отключенных интерфейсов
|
||||
return "unknown"
|
||||
|
||||
async def get_ip_config(
|
||||
self, device: NetworkDeviceGeneric, ip_version: str
|
||||
) -> List[IndividualIPConfig]:
|
||||
async def get_ip_config(self, device: NetworkDeviceGeneric,
|
||||
ip_version: str) -> List[IndividualIPConfig]:
|
||||
"""Получает конфигурацию IP для устройства"""
|
||||
configs = []
|
||||
device_ip = await getattr(device, f"ip{ip_version}_config")
|
||||
if device_ip != "/":
|
||||
ip_config = globals()[f"IPv{ip_version}Config"](device_ip, self.system_bus)
|
||||
ip_config = globals()[f"IPv{ip_version}Config"](device_ip,
|
||||
self.system_bus)
|
||||
address_data = await ip_config.address_data
|
||||
for address in address_data:
|
||||
config = IndividualIPConfig(
|
||||
|
|
@ -67,24 +68,27 @@ class NetworkService:
|
|||
routes=[
|
||||
globals()[f"IPv{ip_version}Route"](
|
||||
destination=route.get(
|
||||
"dest", ("s", "0.0.0.0" if ip_version == "4" else "::")
|
||||
)[1],
|
||||
"dest",
|
||||
("s",
|
||||
"0.0.0.0" if ip_version == "4" else "::"))[1],
|
||||
prefix=route.get("prefix", ("u", 0))[1],
|
||||
next_hop=route.get(
|
||||
"next-hop",
|
||||
("s", "0.0.0.0" if ip_version == "4" else "::"),
|
||||
("s",
|
||||
"0.0.0.0" if ip_version == "4" else "::"),
|
||||
)[1],
|
||||
metric=route.get("metric", ("u", 0))[1],
|
||||
)
|
||||
for route in await ip_config.route_data
|
||||
) for route in await ip_config.route_data
|
||||
],
|
||||
method=await self.get_ip_method(device, f"ipv{ip_version}"),
|
||||
method=await self.get_ip_method(device,
|
||||
f"ipv{ip_version}"),
|
||||
gateway=str(await ip_config.gateway),
|
||||
dns=[
|
||||
str(ns["address"][1]) if ip_version == "4" else str(ns)
|
||||
for ns in await getattr(
|
||||
ip_config,
|
||||
"nameserver_data" if ip_version == "4" else "nameservers",
|
||||
"nameserver_data" if ip_version ==
|
||||
"4" else "nameservers",
|
||||
)
|
||||
],
|
||||
)
|
||||
|
|
@ -106,19 +110,18 @@ class NetworkService:
|
|||
}
|
||||
if active_ap != "/":
|
||||
ap = AccessPoint(active_ap, self.system_bus)
|
||||
wifi_info.update(
|
||||
{
|
||||
"ssid": await ap.ssid,
|
||||
"bssid": await ap.hw_address,
|
||||
"strength": await ap.strength,
|
||||
"frequency": await ap.frequency,
|
||||
"mode": await ap.mode,
|
||||
}
|
||||
)
|
||||
wifi_info.update({
|
||||
"ssid": await ap.ssid,
|
||||
"bssid": await ap.hw_address,
|
||||
"strength": await ap.strength,
|
||||
"frequency": await ap.frequency,
|
||||
"mode": await ap.mode,
|
||||
})
|
||||
return wifi_info
|
||||
|
||||
async def get_network_interfaces(
|
||||
self, initial_positions: Dict[str, Tuple[int, int]] = None
|
||||
self,
|
||||
initial_positions: Dict[str, Tuple[int, int]] = None
|
||||
) -> List[Union[NetworkInterface, WiFiDevice]]:
|
||||
"""Получает список сетевых интерфейсов"""
|
||||
devices_paths = await self.nm.get_devices()
|
||||
|
|
@ -138,26 +141,36 @@ class NetworkService:
|
|||
continue
|
||||
|
||||
common_data = {
|
||||
"name": interface_name,
|
||||
"type": device_type,
|
||||
"device_state": device_state,
|
||||
"active_connection": await self.get_connection_name(generic_device),
|
||||
"mac_address": str(await generic_device.hw_address),
|
||||
"device_mtu": str(await generic_device.mtu),
|
||||
"ipv4": IPConfig(
|
||||
configurations=await self.get_ip_config(generic_device, "4")
|
||||
),
|
||||
"ipv6": IPConfig(
|
||||
configurations=await self.get_ip_config(generic_device, "6")
|
||||
),
|
||||
"x": initial_positions.get(interface_name, (100, 100))[0],
|
||||
"y": initial_positions.get(interface_name, (100, 100))[1],
|
||||
"name":
|
||||
interface_name,
|
||||
"type":
|
||||
device_type,
|
||||
"device_state":
|
||||
device_state,
|
||||
"active_connection":
|
||||
await self.get_connection_name(generic_device),
|
||||
"mac_address":
|
||||
str(await generic_device.hw_address),
|
||||
"device_mtu":
|
||||
str(await generic_device.mtu),
|
||||
"ipv4":
|
||||
IPConfig(configurations=await self.get_ip_config(
|
||||
generic_device, "4")),
|
||||
"ipv6":
|
||||
IPConfig(configurations=await self.get_ip_config(
|
||||
generic_device, "6")),
|
||||
"x":
|
||||
initial_positions.get(interface_name, (100, 100))[0],
|
||||
"y":
|
||||
initial_positions.get(interface_name, (100, 100))[1],
|
||||
}
|
||||
|
||||
if device_type == "WIFI":
|
||||
wifi_device = NetworkDeviceWireless(device_path, self.system_bus)
|
||||
wifi_device = NetworkDeviceWireless(device_path,
|
||||
self.system_bus)
|
||||
wifi_info = await self.get_wifi_info(wifi_device)
|
||||
network_interfaces.append(WiFiDevice(**common_data, **wifi_info))
|
||||
network_interfaces.append(
|
||||
WiFiDevice(**common_data, **wifi_info))
|
||||
else:
|
||||
network_interfaces.append(NetworkInterface(**common_data))
|
||||
|
||||
|
|
@ -166,9 +179,10 @@ class NetworkService:
|
|||
async def get_hostname(self) -> Dict[str, str]:
|
||||
"""Возвращает имя хоста из команды hostname в формате JSON"""
|
||||
try:
|
||||
result = subprocess.run(
|
||||
["hostname"], capture_output=True, text=True, check=True
|
||||
)
|
||||
result = subprocess.run(["hostname"],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
check=True)
|
||||
hostname = result.stdout.strip()
|
||||
return {"hostname": hostname}
|
||||
except subprocess.CalledProcessError as e:
|
||||
|
|
@ -177,14 +191,17 @@ class NetworkService:
|
|||
raise RuntimeError(f"Error processing hostname output: {e}")
|
||||
|
||||
async def get_interfaces_by_state(
|
||||
self, state: str, initial_positions: Dict[str, Tuple[int, int]] = None
|
||||
self,
|
||||
state: str,
|
||||
initial_positions: Dict[str, Tuple[int, int]] = None
|
||||
) -> List[NetworkInterface]:
|
||||
"""
|
||||
Получает список интерфейсов по состоянию
|
||||
"""
|
||||
all_interfaces = await self.get_network_interfaces(initial_positions)
|
||||
return [
|
||||
interface for interface in all_interfaces if interface.device_state == state
|
||||
interface for interface in all_interfaces
|
||||
if interface.device_state == state
|
||||
]
|
||||
|
||||
async def get_interfaces_container(
|
||||
|
|
@ -196,12 +213,13 @@ class NetworkService:
|
|||
) -> NetworkInterfacesContainer:
|
||||
"""Получает объект, содержащий список интерфейсов и координаты контейнера"""
|
||||
if state:
|
||||
interfaces = await self.get_interfaces_by_state(state, initial_positions)
|
||||
interfaces = await self.get_interfaces_by_state(
|
||||
state, initial_positions)
|
||||
else:
|
||||
interfaces = await self.get_network_interfaces(initial_positions)
|
||||
return NetworkInterfacesContainer(
|
||||
interfaces=interfaces, container_x=container_x, container_y=container_y
|
||||
)
|
||||
return NetworkInterfacesContainer(interfaces=interfaces,
|
||||
container_x=container_x,
|
||||
container_y=container_y)
|
||||
|
||||
async def get_network_interfaces_container(
|
||||
self,
|
||||
|
|
@ -209,23 +227,26 @@ class NetworkService:
|
|||
) -> NetworkInterfacesContainer:
|
||||
"""Получает контейнер с сетевыми интерфейсами и координатами контейнера"""
|
||||
interfaces = await self.get_network_interfaces(initial_positions)
|
||||
return NetworkInterfacesContainer(
|
||||
interfaces=interfaces, container_x=0, container_y=0
|
||||
)
|
||||
return NetworkInterfacesContainer(interfaces=interfaces,
|
||||
container_x=0,
|
||||
container_y=0)
|
||||
|
||||
async def get_activated_interfaces(
|
||||
self, initial_positions: Dict[str, Tuple[int, int]] = None
|
||||
self,
|
||||
initial_positions: Dict[str, Tuple[int, int]] = None
|
||||
) -> List[NetworkInterface]:
|
||||
"""Получает список активных сетевых интерфейсов"""
|
||||
return await self.get_interfaces_by_state("100", initial_positions)
|
||||
|
||||
async def get_deactivated_interfaces(
|
||||
self, initial_positions: Dict[str, Tuple[int, int]] = None
|
||||
self,
|
||||
initial_positions: Dict[str, Tuple[int, int]] = None
|
||||
) -> List[NetworkInterface]:
|
||||
"""Получает список неактивных сетевых интерфейсов"""
|
||||
all_interfaces = await self.get_network_interfaces(initial_positions)
|
||||
return [
|
||||
interface for interface in all_interfaces if interface.device_state != "100"
|
||||
interface for interface in all_interfaces
|
||||
if interface.device_state != "100"
|
||||
]
|
||||
|
||||
async def get_activated_interfaces_container(
|
||||
|
|
@ -235,9 +256,8 @@ class NetworkService:
|
|||
container_y: int = 0,
|
||||
) -> NetworkInterfacesContainer:
|
||||
"""Получает объект, содержащий список активных интерфейсов и координаты контейнера"""
|
||||
return await self.get_interfaces_container(
|
||||
"100", initial_positions, container_x, container_y
|
||||
)
|
||||
return await self.get_interfaces_container("100", initial_positions,
|
||||
container_x, container_y)
|
||||
|
||||
async def get_deactivated_interfaces_container(
|
||||
self,
|
||||
|
|
@ -247,6 +267,6 @@ class NetworkService:
|
|||
) -> NetworkInterfacesContainer:
|
||||
"""Получает объект, содержащий список неактивных интерфейсов и координаты контейнера"""
|
||||
interfaces = await self.get_deactivated_interfaces(initial_positions)
|
||||
return NetworkInterfacesContainer(
|
||||
interfaces=interfaces, container_x=container_x, container_y=container_y
|
||||
)
|
||||
return NetworkInterfacesContainer(interfaces=interfaces,
|
||||
container_x=container_x,
|
||||
container_y=container_y)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue