Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: split modules #263

Merged
merged 6 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion mocket/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from mocket.async_mocket import async_mocketize
from mocket.mocket import FakeSSLContext, Mocket, MocketEntry, Mocketizer, mocketize
from mocket.entry import MocketEntry
from mocket.mocket import Mocket
from mocket.mocketizer import Mocketizer, mocketize
from mocket.ssl import FakeSSLContext

__all__ = (
"async_mocketize",
Expand Down
2 changes: 1 addition & 1 deletion mocket/async_mocket.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from mocket.mocket import Mocketizer
from mocket.mocketizer import Mocketizer
from mocket.utils import get_mocketize


Expand Down
59 changes: 59 additions & 0 deletions mocket/entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import collections.abc

from mocket.compat import encode_to_bytes


class MocketEntry:
class Response(bytes):
@property
def data(self):
return self

response_index = 0
request_cls = bytes
response_cls = Response
responses = None
_served = None

def __init__(self, location, responses):
self._served = False
self.location = location

if not isinstance(responses, collections.abc.Iterable):
responses = [responses]

if not responses:
self.responses = [self.response_cls(encode_to_bytes(""))]
else:
self.responses = []
for r in responses:
if not isinstance(r, BaseException) and not getattr(r, "data", False):
if isinstance(r, str):
r = encode_to_bytes(r)
r = self.response_cls(r)
self.responses.append(r)

def __repr__(self):
return f"{self.__class__.__name__}(location={self.location})"

@staticmethod
def can_handle(data):
return True

def collect(self, data):
from mocket import Mocket

req = self.request_cls(data)
Mocket.collect(req)

def get_response(self):
response = self.responses[self.response_index]
if self.response_index < len(self.responses) - 1:
self.response_index += 1

self._served = True

if isinstance(response, BaseException):
raise response

return response.data
17 changes: 17 additions & 0 deletions mocket/io.py
Original file line number Diff line number Diff line change
@@ -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)
Loading