style: ruff format and the lint rules of the driver standard
Mechanical: ruff format, and ruff check --fix with the rule set every NAPALM driver uses (E, F, I, UP, B at line length 100, Python 3.10). What ruff cannot fix comes in the next commit.
This commit is contained in:
1 parent
692a584fd2
commit
571d9abb5f
15 files changed
+690
-419
No files matched your search
@@ -18,11 +18,12 @@ from __future__ import annotations
|
||||
|
||||
import ipaddress
|
||||
import socket
|
||||
from typing import Any, Dict, Iterable, List, Optional, Tuple
|
||||
from collections.abc import Iterable
|
||||
from typing import Any
|
||||
|
||||
#: Port names OPNsense accepts in a rule. ``socket.getservbyname`` knows them
|
||||
#: too, but only where ``/etc/services`` exists -- a slim container has none.
|
||||
WELL_KNOWN_PORTS: Dict[str, int] = {
|
||||
WELL_KNOWN_PORTS: dict[str, int] = {
|
||||
"ftp": 21,
|
||||
"ssh": 22,
|
||||
"telnet": 23,
|
||||
@@ -49,7 +50,7 @@ WELL_KNOWN_PORTS: Dict[str, int] = {
|
||||
#: Alias types whose entries can be addresses.
|
||||
_ADDRESS_ALIASES = ("host", "network")
|
||||
|
||||
AliasIndex = Dict[str, Tuple[str, List[str]]]
|
||||
AliasIndex = dict[str, tuple[str, list[str]]]
|
||||
|
||||
|
||||
def wan_interfaces(overview: Any) -> set:
|
||||
@@ -61,20 +62,23 @@ def wan_interfaces(overview: Any) -> set:
|
||||
def alias_index(rows: Iterable[dict]) -> AliasIndex:
|
||||
"""``name -> (type, entries)`` from the alias list."""
|
||||
return {
|
||||
r["name"]: (r.get("type", ""), [e.strip() for e in str(r.get("content", "")).splitlines() if e.strip()])
|
||||
r["name"]: (
|
||||
r.get("type", ""),
|
||||
[e.strip() for e in str(r.get("content", "")).splitlines() if e.strip()],
|
||||
)
|
||||
for r in rows
|
||||
if r.get("name")
|
||||
}
|
||||
|
||||
|
||||
def _as_address(value: str) -> Optional[str]:
|
||||
def _as_address(value: str) -> str | None:
|
||||
try:
|
||||
return str(ipaddress.ip_address(value))
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
|
||||
def _addresses(target: str, aliases: AliasIndex) -> List[str]:
|
||||
def _addresses(target: str, aliases: AliasIndex) -> list[str]:
|
||||
"""The addresses a rule sends to: a literal, or a host/network alias's.
|
||||
|
||||
Anything else -- an interface address, a name resolved by DNS -- gives no
|
||||
@@ -89,7 +93,7 @@ def _addresses(target: str, aliases: AliasIndex) -> List[str]:
|
||||
return [a for a in (_as_address(e) for e in entries) if a]
|
||||
|
||||
|
||||
def _port(value: Any, protocol: str, aliases: AliasIndex) -> Optional[int]:
|
||||
def _port(value: Any, protocol: str, aliases: AliasIndex) -> int | None:
|
||||
"""A rule's port as a number: literal, start of a range, alias or name.
|
||||
|
||||
No port at all means every port, which the contract writes as 0.
|
||||
@@ -115,7 +119,7 @@ def _faces_the_wan(rule: dict, wan: set) -> bool:
|
||||
return bool(set(str(rule.get("interface", "")).split(",")) & wan)
|
||||
|
||||
|
||||
def _remote_host(rule: dict) -> Optional[str]:
|
||||
def _remote_host(rule: dict) -> str | None:
|
||||
"""A source restriction; an inverted one ("all but X") restricts nothing."""
|
||||
source = str(rule.get("source.network") or "").strip()
|
||||
if source in ("", "any") or rule.get("source.not") == "1":
|
||||
@@ -123,11 +127,11 @@ def _remote_host(rule: dict) -> Optional[str]:
|
||||
return source
|
||||
|
||||
|
||||
def _forwards_of(rule: dict, aliases: AliasIndex) -> List[dict]:
|
||||
def _forwards_of(rule: dict, aliases: AliasIndex) -> list[dict]:
|
||||
protocols = [p.upper() for p in str(rule.get("protocol") or "any").split("/") if p]
|
||||
target = str(rule.get("target", "")).strip()
|
||||
remote = _remote_host(rule)
|
||||
result: List[dict] = []
|
||||
result: list[dict] = []
|
||||
for protocol in protocols:
|
||||
external = _port(rule.get("destination.port", ""), protocol.lower(), aliases)
|
||||
if external is None:
|
||||
@@ -150,9 +154,9 @@ def _forwards_of(rule: dict, aliases: AliasIndex) -> List[dict]:
|
||||
return result
|
||||
|
||||
|
||||
def port_forwards(rules: Iterable[dict], wan: set, aliases: AliasIndex) -> List[dict]:
|
||||
def port_forwards(rules: Iterable[dict], wan: set, aliases: AliasIndex) -> list[dict]:
|
||||
"""The destination-NAT rules on a WAN interface, as ``PortForwardDict`` entries."""
|
||||
result: List[dict] = []
|
||||
result: list[dict] = []
|
||||
for rule in rules:
|
||||
if _faces_the_wan(rule, wan):
|
||||
result.extend(_forwards_of(rule, aliases))
|
||||
|
||||
Reference in new issue
Block a user