new typing style

This commit is contained in:
Maxim Devaev
2022-09-04 18:08:40 +03:00
parent 4b75221e94
commit ee3e224e39
129 changed files with 593 additions and 941 deletions

View File

@@ -26,9 +26,6 @@ import dataclasses
import itertools
import argparse
from typing import List
from typing import Optional
from ...logging import get_logger
from ...yamlconf import Section
@@ -66,25 +63,25 @@ class _Netcfg: # pylint: disable=too-many-instance-attributes
class _Service: # pylint: disable=too-many-instance-attributes
def __init__(self, config: Section) -> None:
self.__iface_net: str = config.otgnet.iface.net
self.__ip_cmd: List[str] = config.otgnet.iface.ip_cmd
self.__ip_cmd: list[str] = config.otgnet.iface.ip_cmd
self.__allow_icmp: bool = config.otgnet.firewall.allow_icmp
self.__allow_tcp: List[int] = sorted(set(config.otgnet.firewall.allow_tcp))
self.__allow_udp: List[int] = sorted(set(config.otgnet.firewall.allow_udp))
self.__allow_tcp: list[int] = sorted(set(config.otgnet.firewall.allow_tcp))
self.__allow_udp: list[int] = sorted(set(config.otgnet.firewall.allow_udp))
self.__forward_iface: str = config.otgnet.firewall.forward_iface
self.__iptables_cmd: List[str] = config.otgnet.firewall.iptables_cmd
self.__iptables_cmd: list[str] = config.otgnet.firewall.iptables_cmd
def build_cmd(key: str) -> List[str]:
def build_cmd(key: str) -> list[str]:
return tools.build_cmd(
getattr(config.otgnet.commands, key),
getattr(config.otgnet.commands, f"{key}_remove"),
getattr(config.otgnet.commands, f"{key}_append"),
)
self.__pre_start_cmd: List[str] = build_cmd("pre_start_cmd")
self.__post_start_cmd: List[str] = build_cmd("post_start_cmd")
self.__pre_stop_cmd: List[str] = build_cmd("pre_stop_cmd")
self.__post_stop_cmd: List[str] = build_cmd("post_stop_cmd")
self.__pre_start_cmd: list[str] = build_cmd("pre_start_cmd")
self.__post_start_cmd: list[str] = build_cmd("post_start_cmd")
self.__pre_stop_cmd: list[str] = build_cmd("pre_stop_cmd")
self.__post_stop_cmd: list[str] = build_cmd("post_stop_cmd")
self.__gadget: str = config.otg.gadget
self.__driver: str = config.otg.devices.ethernet.driver
@@ -101,7 +98,7 @@ class _Service: # pylint: disable=too-many-instance-attributes
key: str(value)
for (key, value) in dataclasses.asdict(netcfg).items()
}
ctls: List[BaseCtl] = [
ctls: list[BaseCtl] = [
CustomCtl(self.__pre_start_cmd, self.__post_stop_cmd, placeholders),
IfaceUpCtl(self.__ip_cmd, netcfg.iface),
*([IptablesAllowIcmpCtl(self.__iptables_cmd, netcfg.iface)] if self.__allow_icmp else []),
@@ -185,7 +182,7 @@ class _Service: # pylint: disable=too-many-instance-attributes
# =====
def main(argv: Optional[List[str]]=None) -> None:
def main(argv: (list[str] | None)=None) -> None:
(parent_parser, argv, config) = init(
add_help=False,
argv=argv,

View File

@@ -20,50 +20,46 @@
# ========================================================================== #
from typing import List
from typing import Dict
# =====
class BaseCtl:
def get_command(self, direct: bool) -> List[str]:
def get_command(self, direct: bool) -> list[str]:
raise NotImplementedError
class IfaceUpCtl(BaseCtl):
def __init__(self, base_cmd: List[str], iface: str) -> None:
def __init__(self, base_cmd: list[str], iface: str) -> None:
self.__base_cmd = base_cmd
self.__iface = iface
def get_command(self, direct: bool) -> List[str]:
def get_command(self, direct: bool) -> list[str]:
return [*self.__base_cmd, "link", "set", self.__iface, ("up" if direct else "down")]
class IfaceAddIpCtl(BaseCtl):
def __init__(self, base_cmd: List[str], iface: str, cidr: str) -> None:
def __init__(self, base_cmd: list[str], iface: str, cidr: str) -> None:
self.__base_cmd = base_cmd
self.__iface = iface
self.__cidr = cidr
def get_command(self, direct: bool) -> List[str]:
def get_command(self, direct: bool) -> list[str]:
return [*self.__base_cmd, "address", ("add" if direct else "del"), self.__cidr, "dev", self.__iface]
class IptablesDropAllCtl(BaseCtl):
def __init__(self, base_cmd: List[str], iface: str) -> None:
def __init__(self, base_cmd: list[str], iface: str) -> None:
self.__base_cmd = base_cmd
self.__iface = iface
def get_command(self, direct: bool) -> List[str]:
def get_command(self, direct: bool) -> list[str]:
return [*self.__base_cmd, ("-A" if direct else "-D"), "INPUT", "-i", self.__iface, "-j", "DROP"]
class IptablesAllowIcmpCtl(BaseCtl):
def __init__(self, base_cmd: List[str], iface: str) -> None:
def __init__(self, base_cmd: list[str], iface: str) -> None:
self.__base_cmd = base_cmd
self.__iface = iface
def get_command(self, direct: bool) -> List[str]:
def get_command(self, direct: bool) -> list[str]:
return [
*self.__base_cmd,
("-A" if direct else "-D"), "INPUT", "-i", self.__iface, "-p", "icmp", "-j", "ACCEPT",
@@ -71,13 +67,13 @@ class IptablesAllowIcmpCtl(BaseCtl):
class IptablesAllowPortCtl(BaseCtl):
def __init__(self, base_cmd: List[str], iface: str, port: int, tcp: bool) -> None:
def __init__(self, base_cmd: list[str], iface: str, port: int, tcp: bool) -> None:
self.__base_cmd = base_cmd
self.__iface = iface
self.__port = port
self.__proto = ("tcp" if tcp else "udp")
def get_command(self, direct: bool) -> List[str]:
def get_command(self, direct: bool) -> list[str]:
return [
*self.__base_cmd,
("-A" if direct else "-D"), "INPUT", "-i", self.__iface, "-p", self.__proto,
@@ -86,11 +82,11 @@ class IptablesAllowPortCtl(BaseCtl):
class IptablesForwardOut(BaseCtl):
def __init__(self, base_cmd: List[str], iface: str) -> None:
def __init__(self, base_cmd: list[str], iface: str) -> None:
self.__base_cmd = base_cmd
self.__iface = iface
def get_command(self, direct: bool) -> List[str]:
def get_command(self, direct: bool) -> list[str]:
return [
*self.__base_cmd,
"--table", "nat",
@@ -100,11 +96,11 @@ class IptablesForwardOut(BaseCtl):
class IptablesForwardIn(BaseCtl):
def __init__(self, base_cmd: List[str], iface: str) -> None:
def __init__(self, base_cmd: list[str], iface: str) -> None:
self.__base_cmd = base_cmd
self.__iface = iface
def get_command(self, direct: bool) -> List[str]:
def get_command(self, direct: bool) -> list[str]:
return [
*self.__base_cmd,
("-A" if direct else "-D"), "FORWARD",
@@ -115,16 +111,16 @@ class IptablesForwardIn(BaseCtl):
class CustomCtl(BaseCtl):
def __init__(
self,
direct_cmd: List[str],
reverse_cmd: List[str],
placeholders: Dict[str, str],
direct_cmd: list[str],
reverse_cmd: list[str],
placeholders: dict[str, str],
) -> None:
self.__direct_cmd = direct_cmd
self.__reverse_cmd = reverse_cmd
self.__placeholders = placeholders
def get_command(self, direct: bool) -> List[str]:
def get_command(self, direct: bool) -> list[str]:
return [
part.format(**self.__placeholders)
for part in (self.__direct_cmd if direct else self.__reverse_cmd)