From 207778acd16368012eaced3b65eb4324477f5b1b Mon Sep 17 00:00:00 2001 From: betaboon Date: Sun, 17 Nov 2024 16:12:03 +0100 Subject: [PATCH] refactor: move SocketMocketCore from mocket.utils to mocket.io --- mocket/io.py | 17 +++++++++++++++++ mocket/mocket.py | 7 ++----- mocket/utils.py | 20 +++----------------- 3 files changed, 22 insertions(+), 22 deletions(-) create mode 100644 mocket/io.py diff --git a/mocket/io.py b/mocket/io.py new file mode 100644 index 00000000..45bb8272 --- /dev/null +++ b/mocket/io.py @@ -0,0 +1,17 @@ +import io +import os + + +class MocketSocketCore(io.BytesIO): + def __init__(self, address) -> None: + self._address = address + super().__init__() + + def write(self, content): + from mocket import Mocket + + super().write(content) + + _, w_fd = Mocket.get_pair(self._address) + if w_fd: + os.write(w_fd, content) diff --git a/mocket/mocket.py b/mocket/mocket.py index e9bb27e3..fb7ec8a0 100644 --- a/mocket/mocket.py +++ b/mocket/mocket.py @@ -23,12 +23,9 @@ from mocket.compat import decode_from_bytes, encode_to_bytes +from mocket.io import MocketSocketCore from mocket.mode import MocketMode -from mocket.utils import ( - MocketSocketCore, - hexdump, - hexload, -) +from mocket.utils import hexdump, hexload xxh32 = None try: diff --git a/mocket/utils.py b/mocket/utils.py index 5f065bfa..f94b78f7 100644 --- a/mocket/utils.py +++ b/mocket/utils.py @@ -1,34 +1,20 @@ from __future__ import annotations import binascii -import io -import os import ssl from typing import Callable from mocket.compat import decode_from_bytes, encode_to_bytes +# NOTE this is here for backwards-compat to keep old import-paths working +from mocket.io import MocketSocketCore as MocketSocketCore + # NOTE this is here for backwards-compat to keep old import-paths working from mocket.mode import MocketMode as MocketMode SSL_PROTOCOL = ssl.PROTOCOL_TLSv1_2 -class MocketSocketCore(io.BytesIO): - def __init__(self, address) -> None: - self._address = address - super().__init__() - - def write(self, content): - from mocket import Mocket - - super().write(content) - - _, w_fd = Mocket.get_pair(self._address) - if w_fd: - os.write(w_fd, content) - - def hexdump(binary_string: bytes) -> str: r""" >>> hexdump(b"bar foobar foo") == decode_from_bytes(encode_to_bytes("62 61 72 20 66 6F 6F 62 61 72 20 66 6F 6F"))