Commit 2baae075 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Reworked paths in client and Anyio backend

This cleans up some of the questionable changes between strings and
Path objects; it also allows the wpa_supplicant.types module to be
removed.
parent a61eb691
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -109,7 +109,7 @@ class InterfaceMethodsTests(unittest.TestCase):
			ifclient = await client.connect_interface("enp1s0")

			self.assertIsInstance(ifclient, interfaces.InterfaceClient)
			connect_mock.assert_called_once_with("/tmp/enp1s0")
			connect_mock.assert_called_once_with(pathlib.Path("/tmp/enp1s0"))

			# Check only INTERFACES was sent
			client.sock.send.assert_called_once_with(b"INTERFACES")
@@ -133,7 +133,7 @@ class InterfaceMethodsTests(unittest.TestCase):
			ifclient = await client.connect_interface("enp1s0")

			self.assertIsInstance(ifclient, interfaces.InterfaceClient)
			connect_mock.assert_called_once_with("/tmp/enp1s0")
			connect_mock.assert_called_once_with(pathlib.Path("/tmp/enp1s0"))

			# Check INTERFACE_ADD sent after INTERFACES
			args = client.sock.send.call_args_list
+13 −13
Original line number Diff line number Diff line
@@ -23,18 +23,18 @@ import os
import socket
import tempfile
from contextlib import suppress
from os import PathLike
from typing import Any
from typing import Callable
from typing import Coroutine
from typing import Dict
from typing import Protocol
from typing import Union
from typing import cast

import sniffio

from .types import PathLike

ConnectorFn = Callable[[PathLike, PathLike], Coroutine[Any, Any, 'DatagramSocket']]
ConnectorFn = Callable[[str, str], Coroutine[Any, Any, 'DatagramSocket']]

connectors: Dict[str, ConnectorFn] = {}

@@ -59,7 +59,7 @@ class ConnectedUNIXMixin:
		os.unlink(path)


async def connect_unix_datagram(path: PathLike) -> DatagramSocket:
async def connect_unix_datagram(path: Union[str, PathLike[str]]) -> DatagramSocket:
	"""
	Return an AnyIO socket connected to a Unix datagram socket

@@ -70,7 +70,7 @@ async def connect_unix_datagram(path: PathLike) -> DatagramSocket:
		with suppress(FileExistsError):
			async_lib = sniffio.current_async_library()
			connector = connectors[async_lib]
			return await connector(fname, path)
			return await connector(fname, os.fspath(path))
	raise FileExistsError(
		errno.EEXIST, "No usable temporary filename found",
	)
@@ -86,13 +86,13 @@ else:
		...

	async def trio_connect_unix_datagram(
		local_path: PathLike,
		remote_path: PathLike,
		local_path: str,
		remote_path: str,
	) -> TrioConnectedUNIXSocket:
		sock = trio.socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
		await sock.bind(os.fspath(local_path))
		await sock.bind(local_path)
		try:
			await sock.connect(os.fspath(remote_path))
			await sock.connect(remote_path)
		except BaseException:  # pragma: no cover
			sock.close()
			raise
@@ -113,17 +113,17 @@ else:
		...

	async def asyncio_connect_unix_datagram(
		local_path: PathLike,
		remote_path: PathLike,
		local_path: str,
		remote_path: str,
	) -> AsyncioConnectedUNIXSocket:
		await asyncio.sleep(0.0)
		loop = asyncio.get_running_loop()
		sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
		sock.setblocking(False)
		sock.bind(os.fspath(local_path))
		sock.bind(local_path)
		while True:
			try:
				sock.connect(os.fspath(remote_path))
				sock.connect(remote_path)
			except BlockingIOError:
				future: asyncio.Future[None] = asyncio.Future()
				loop.add_writer(sock, future.set_result, None)
+2 −7
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ from __future__ import annotations
import enum
import logging
import os
import pathlib
from re import compile as regex
from types import TracebackType as Traceback
from typing import Any
@@ -40,7 +39,6 @@ import anyio
from .. import errors
from .._anyio import DatagramSocket
from .._anyio import connect_unix_datagram
from ..types import PathLike
from . import consts

T = TypeVar('T')
@@ -117,18 +115,15 @@ class BaseClient:
	) -> None:
		await self.disconnect()

	async def connect(self, path: PathLike) -> None:
	async def connect(self, path: os.PathLike[str]) -> None:
		"""
		Connect to a WPA-Supplicant daemon through the given address
		"""
		if self.sock is not None:
			raise RuntimeError("cannot connect to multiple daemons")

		if not isinstance(path, pathlib.Path):
			path = pathlib.Path(os.fspath(path))

		with anyio.fail_after(1.0):
			self.sock = await connect_unix_datagram(path.as_posix())
			self.sock = await connect_unix_datagram(os.fspath(path))
			await self.send_command(consts.COMMAND_PING, expect=consts.RESPONSE_PONG)

	async def disconnect(self) -> None:
+4 −2
Original line number Diff line number Diff line
@@ -16,13 +16,15 @@
Interfaces control client class
"""

from __future__ import annotations

from itertools import count
from os import PathLike
from typing import Any
from typing import AsyncGenerator
from typing import Dict

from .. import config
from ..types import PathLike
from . import consts
from .base import BaseClient

@@ -36,7 +38,7 @@ class InterfaceClient(BaseClient):

	name = None

	async def connect(self, path: PathLike) -> None:
	async def connect(self, path: PathLike[str]) -> None:
		await super().connect(path)
		self.name = await self.send_command(consts.COMMAND_IFNAME, convert=str)

+5 −3
Original line number Diff line number Diff line
@@ -16,10 +16,12 @@
Master control client class
"""

from __future__ import annotations

import pathlib
from os import PathLike
from typing import Set

from ..types import PathLike
from . import consts
from .base import BaseClient
from .interfaces import InterfaceClient
@@ -32,7 +34,7 @@ class MasterClient(BaseClient):

	ctrl_dir = None

	async def connect(self, path: PathLike) -> None:
	async def connect(self, path: PathLike[str]) -> None:
		if not isinstance(path, pathlib.Path):
			path = pathlib.Path(path)
		await super().connect(path)
@@ -82,5 +84,5 @@ class MasterClient(BaseClient):
			"RuntimeError should be raised for sends on unconnected clients; " \
			"or connect() may not have set ctrl_dir"
		client = InterfaceClient(logger=self.logger)
		await client.connect(self.ctrl_dir.joinpath(ifname).as_posix())
		await client.connect(self.ctrl_dir / ifname)
		return client
Loading