Commit 709519d7 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Drop over-complicated enums and rearrange consts

parent 41b21670
Loading
Loading
Loading
Loading
+0 −18
Original line number Diff line number Diff line
@@ -27,24 +27,6 @@ from mocket import Mocket, MocketEntry, mocketize
from wpa_supplicant import util


class EnumTests(unittest.TestCase):
	"""
	Tests for Enum classes in util
	"""

	def test_autoname(self):
		"""
		Test names match values in AutoName enum classes
		"""

		class _Foo(util.AutoName):
			FOO = util.auto()
			BAR = util.auto()

		for enum in _Foo.FOO, _Foo.BAR:
			self.assertEqual(enum.name, enum.value)


class AnyIOTests(unittest.TestCase):
	"""
	Tests for 'anyio' functions in util
+8 −14
Original line number Diff line number Diff line
@@ -26,16 +26,10 @@ from typing import Any, Callable, Optional, Set, Sequence, Tuple, Union

import anyio

from . import consts
from .. import errors, util


Path = Union[str, os.PathLike]

RESPONSE_OK = 'OK'
RESPONSE_FAIL = 'FAIL'
RESPONSE_UNKNOWN_COMMAND = 'UNKNOWN COMMAND'


class EventPriority(enum.IntEnum):
	"""
	Event Message priorities
@@ -89,7 +83,7 @@ class BaseClient:
	async def __aexit__(self, *exc_info):
		await self.disconnect()

	async def connect(self, path: Path):
	async def connect(self, path: consts.Path):
		"""
		Connect to a WPA-Supplicant daemon through the given address
		"""
@@ -101,7 +95,7 @@ class BaseClient:

		async with anyio.fail_after(1):
			self.sock = await util.connect_unix_datagram(path.as_posix())
			await self.send_message("PING", expect="PONG")
			await self.send_message(consts.COMMAND_PING, expect=consts.RESPONSE_PONG)

	async def disconnect(self):
		"""
@@ -114,7 +108,7 @@ class BaseClient:
			message: str,
			*args: str,
			seperator: str = '\t',
			expect: str = RESPONSE_OK,
			expect: str = consts.RESPONSE_OK,
			convert: Optional[Callable] = None,
			) -> Any:
		"""
@@ -153,9 +147,9 @@ class BaseClient:

			resp = await queue.get()

		if resp == RESPONSE_FAIL:
		if resp == consts.RESPONSE_FAIL:
			raise errors.CommandFailed(f"command returned FAIL: {message!r}")
		if resp == RESPONSE_UNKNOWN_COMMAND:
		if resp == consts.RESPONSE_UNKNOWN_COMMAND:
			raise ValueError(f"Unknown command: {message!r}")
		if convert:
			return convert(resp)
@@ -232,7 +226,7 @@ class BaseClient:
			client = self.client
			assert client._eventcount >= 0
			if client._eventcount == 0:
				await client.send_message("ATTACH")
				await client.send_message(consts.COMMAND_ATTACH)
			client._eventcount += 1

		async def __aexit__(self, *exc_info):
@@ -243,7 +237,7 @@ class BaseClient:
				if __debug__: # On it's own for compiler optimisation
					if exc_info[0]:
						client.logger.debug(f"Detaching due to {exc_info[0].__name__}")
				await client.send_message("DETACH")
				await client.send_message(consts.COMMAND_DETACH)


class ReplyManager:
+42 −0
Original line number Diff line number Diff line
#  Copyright 2019  Dom Sekotill <dom.sekotill@kodo.org.uk>
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

"""
Constants any types for the various client modules and classes
"""

import os
from typing import Union


# Types

Path = Union[str, os.PathLike]


# Commands

COMMAND_PING = 'PING'
COMMAND_ATTACH = 'ATTACH'
COMMAND_DETACH = 'DETACH'
COMMAND_INTERFACES = 'INTERFACES'
COMMAND_INTERFACE_ADD = 'INTERFACE_ADD'


# Unvalued and failure responses

RESPONSE_OK = 'OK'
RESPONSE_PONG = 'PONG'
RESPONSE_FAIL = 'FAIL'
RESPONSE_UNKNOWN_COMMAND = 'UNKNOWN COMMAND'
+1 −11
Original line number Diff line number Diff line
@@ -16,20 +16,10 @@
Interfaces control client class
"""

from .. import util
from . import consts
from .base import BaseClient


class InterfaceCommands(util.AutoName):
	"""
	Commands for interface control clients
	"""

	# INTERFACES = ()
	# ADD_INTERFACE = ()



class InterfaceClient(BaseClient):
	"""
	A client for per-interface management
+3 −13
Original line number Diff line number Diff line
@@ -18,23 +18,13 @@ Master control client class

import os
import pathlib
from enum import auto
from typing import Set

from .. import util
from . import consts
from .base import BaseClient
from .interfaces import InterfaceClient


class MasterCommands(util.AutoName):
	"""
	Commands for master control clients
	"""

	INTERFACES = auto()
	INTERFACE_ADD = auto()


class MasterClient(BaseClient):
	"""
	A client for listing, adding and removing interfaces, and getting per-client interfaces
@@ -53,7 +43,7 @@ class MasterClient(BaseClient):
		Return a set of the interfaces currently managed by the daemon
		"""
		return await self.send_message(
			MasterCommands.INTERFACES.value,
			consts.COMMAND_INTERFACES,
			convert=lambda x: x.splitlines(),
		)

@@ -62,7 +52,7 @@ class MasterClient(BaseClient):
		Add a network interface to the daemon's control interfaces
		"""
		await self.send_message(
			MasterCommands.INTERFACE_ADD.value,
			consts.COMMAND_INTERFACE_ADD,
			ifname, '', driver, self.ctrl_dir.as_posix(), driver_param,
		)

Loading