Skip to content

Commit

Permalink
Switching to using ruff for linting (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
mindflayer authored May 12, 2024
1 parent c434799 commit 98a1ed9
Show file tree
Hide file tree
Showing 22 changed files with 217 additions and 230 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ jobs:
run: |
make develop
make services-up
- name: Setup hostname
run: |
export CONTAINER_ID=$(docker compose ps -q proxy)
export CONTAINER_IP=$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $CONTAINER_ID)
echo "$CONTAINER_IP httpbin.local" | sudo tee -a /etc/hosts
- name: Test
run: |
make test
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ Documentation/
.tox/
.ropeproject/
.idea/
*.sh
doc/build
.buildinfo
.coverage
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ repos:
exclude: helm/
args: [ --unsafe ]
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.4.3"
rev: "v0.4.4"
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ install-dev-requirements:
install-test-requirements:
uv pip install --editable .[test]

services-up:
prepare-hosts: _services-up
@bash scripts/patch_hosts.sh

_services-up:
docker compose up -d

services-up: _services-up prepare-hosts

services-down:
docker compose down --remove-orphans

Expand Down Expand Up @@ -39,5 +44,5 @@ clean:
rm -rf *.egg-info dist/ requirements.txt Pipfile.lock
find . -type d -name __pycache__ -exec rm -rf {} \;

.PHONY: clean publish safetest test setup develop lint-python test-python
.PHONY: services-up services-down install-test-requirements install-dev-requirements
.PHONY: clean publish safetest test setup develop lint-python test-python _services-up
.PHONY: prepare-hosts services-up services-down install-test-requirements install-dev-requirements
2 changes: 1 addition & 1 deletion mocket/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import codecs
import os
import shlex
from typing import Any, Final
from typing import Final

ENCODING: Final[str] = os.getenv("MOCKET_ENCODING", "utf-8")

Expand Down
94 changes: 43 additions & 51 deletions mocket/mocket.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import collections
import collections.abc as collections_abc
import contextlib
import errno
import hashlib
import io
import itertools
import json
import os
Expand All @@ -21,6 +21,7 @@
except ImportError:
urllib3_wrap_socket = None


from .compat import basestring, byte_type, decode_from_bytes, encode_to_bytes, text_type
from .utils import (
SSL_PROTOCOL,
Expand All @@ -35,10 +36,8 @@
try:
from xxhash import xxh32
except ImportError: # pragma: no cover
try:
with contextlib.suppress(ImportError):
from xxhash_cffi import xxh32
except ImportError:
pass
hasher = xxh32 or hashlib.md5

try: # pragma: no cover
Expand Down Expand Up @@ -192,9 +191,7 @@ def __init__(
self.kwargs = kwargs

def __str__(self):
return "({})(family={} type={} protocol={})".format(
self.__class__.__name__, self.family, self.type, self.proto
)
return f"({self.__class__.__name__})(family={self.family} type={self.type} protocol={self.proto})"

def __enter__(self):
return self
Expand Down Expand Up @@ -250,14 +247,14 @@ def getpeercert(self, *args, **kwargs):
return {
"notAfter": shift.strftime("%b %d %H:%M:%S GMT"),
"subjectAltName": (
("DNS", "*.%s" % self._host),
("DNS", f"*.{self._host}"),
("DNS", self._host),
("DNS", "*"),
),
"subject": (
(("organizationName", "*.%s" % self._host),),
(("organizationName", f"*.{self._host}"),),
(("organizationalUnitName", "Domain Control Validated"),),
(("commonName", "*.%s" % self._host),),
(("commonName", f"*.{self._host}"),),
),
}

Expand Down Expand Up @@ -292,10 +289,7 @@ def sendall(self, data, entry=None, *args, **kwargs):

if entry:
consume_response = entry.collect(data)
if consume_response is not False:
response = entry.get_response()
else:
response = None
response = entry.get_response() if consume_response is not False else None
else:
response = self.true_sendall(data, *args, **kwargs)

Expand Down Expand Up @@ -353,7 +347,7 @@ def true_sendall(self, data, *args, **kwargs):
)
# check if there's already a recorded session dumped to a JSON file
try:
with io.open(path) as f:
with open(path) as f:
responses = json.load(f)
# if not, create a new dictionary
except (FileNotFoundError, JSONDecodeError):
Expand Down Expand Up @@ -390,11 +384,9 @@ def true_sendall(self, data, *args, **kwargs):
**self.kwargs,
)

try:
self.true_socket.connect((host, port))
except (OSError, socket.error, ValueError):
with contextlib.suppress(OSError, ValueError):
# already connected
pass
self.true_socket.connect((host, port))
self.true_socket.sendall(data, *args, **kwargs)
encoded_response = b""
# https://github.com/kennethreitz/requests/blob/master/tests/testserver/server.py#L13
Expand All @@ -416,7 +408,7 @@ def true_sendall(self, data, *args, **kwargs):
response_dict["request"] = req
response_dict["response"] = hexdump(encoded_response)

with io.open(path, mode="w") as f:
with open(path, mode="w") as f:
f.write(
decode_from_bytes(
json.dumps(responses, indent=4, sort_keys=True)
Expand All @@ -428,8 +420,9 @@ def true_sendall(self, data, *args, **kwargs):

def send(self, data, *args, **kwargs): # pragma: no cover
entry = self.get_entry(data)
kwargs["entry"] = entry
if not entry or (entry and self._entry != entry):
self.sendall(data, entry=entry, *args, **kwargs)
self.sendall(data, *args, **kwargs)
else:
req = Mocket.last_request()
if hasattr(req, "add_data"):
Expand Down Expand Up @@ -512,41 +505,40 @@ def enable(namespace=None, truesocket_recording_dir=None):
Mocket._namespace = namespace
Mocket._truesocket_recording_dir = truesocket_recording_dir

if truesocket_recording_dir:
if truesocket_recording_dir and not os.path.isdir(truesocket_recording_dir):
# JSON dumps will be saved here
if not os.path.isdir(truesocket_recording_dir):
raise AssertionError
raise AssertionError

socket.socket = socket.__dict__["socket"] = MocketSocket
socket._socketobject = socket.__dict__["_socketobject"] = MocketSocket
socket.SocketType = socket.__dict__["SocketType"] = MocketSocket
socket.create_connection = socket.__dict__[
"create_connection"
] = create_connection
socket.create_connection = socket.__dict__["create_connection"] = (
create_connection
)
socket.gethostname = socket.__dict__["gethostname"] = lambda: "localhost"
socket.gethostbyname = socket.__dict__[
"gethostbyname"
] = lambda host: "127.0.0.1"
socket.getaddrinfo = socket.__dict__[
"getaddrinfo"
] = lambda host, port, family=None, socktype=None, proto=None, flags=None: [
(2, 1, 6, "", (host, port))
]
socket.gethostbyname = socket.__dict__["gethostbyname"] = (
lambda host: "127.0.0.1"
)
socket.getaddrinfo = socket.__dict__["getaddrinfo"] = (
lambda host, port, family=None, socktype=None, proto=None, flags=None: [
(2, 1, 6, "", (host, port))
]
)
socket.socketpair = socket.__dict__["socketpair"] = socketpair
ssl.wrap_socket = ssl.__dict__["wrap_socket"] = FakeSSLContext.wrap_socket
ssl.SSLContext = ssl.__dict__["SSLContext"] = FakeSSLContext
socket.inet_pton = socket.__dict__["inet_pton"] = lambda family, ip: byte_type(
"\x7f\x00\x00\x01", "utf-8"
)
urllib3.util.ssl_.wrap_socket = urllib3.util.ssl_.__dict__[
"wrap_socket"
] = FakeSSLContext.wrap_socket
urllib3.util.ssl_.wrap_socket = urllib3.util.ssl_.__dict__["wrap_socket"] = (
FakeSSLContext.wrap_socket
)
urllib3.util.ssl_.ssl_wrap_socket = urllib3.util.ssl_.__dict__[
"ssl_wrap_socket"
] = FakeSSLContext.wrap_socket
urllib3.util.ssl_wrap_socket = urllib3.util.__dict__[
"ssl_wrap_socket"
] = FakeSSLContext.wrap_socket
urllib3.util.ssl_wrap_socket = urllib3.util.__dict__["ssl_wrap_socket"] = (
FakeSSLContext.wrap_socket
)
urllib3.connection.ssl_wrap_socket = urllib3.connection.__dict__[
"ssl_wrap_socket"
] = FakeSSLContext.wrap_socket
Expand All @@ -564,9 +556,9 @@ def disable():
socket.socket = socket.__dict__["socket"] = true_socket
socket._socketobject = socket.__dict__["_socketobject"] = true_socket
socket.SocketType = socket.__dict__["SocketType"] = true_socket
socket.create_connection = socket.__dict__[
"create_connection"
] = true_create_connection
socket.create_connection = socket.__dict__["create_connection"] = (
true_create_connection
)
socket.gethostname = socket.__dict__["gethostname"] = true_gethostname
socket.gethostbyname = socket.__dict__["gethostbyname"] = true_gethostbyname
socket.getaddrinfo = socket.__dict__["getaddrinfo"] = true_getaddrinfo
Expand All @@ -575,15 +567,15 @@ def disable():
ssl.wrap_socket = ssl.__dict__["wrap_socket"] = true_ssl_wrap_socket
ssl.SSLContext = ssl.__dict__["SSLContext"] = true_ssl_context
socket.inet_pton = socket.__dict__["inet_pton"] = true_inet_pton
urllib3.util.ssl_.wrap_socket = urllib3.util.ssl_.__dict__[
"wrap_socket"
] = true_urllib3_wrap_socket
urllib3.util.ssl_.wrap_socket = urllib3.util.ssl_.__dict__["wrap_socket"] = (
true_urllib3_wrap_socket
)
urllib3.util.ssl_.ssl_wrap_socket = urllib3.util.ssl_.__dict__[
"ssl_wrap_socket"
] = true_urllib3_ssl_wrap_socket
urllib3.util.ssl_wrap_socket = urllib3.util.__dict__[
"ssl_wrap_socket"
] = true_urllib3_ssl_wrap_socket
urllib3.util.ssl_wrap_socket = urllib3.util.__dict__["ssl_wrap_socket"] = (
true_urllib3_ssl_wrap_socket
)
urllib3.connection.ssl_wrap_socket = urllib3.connection.__dict__[
"ssl_wrap_socket"
] = true_urllib3_ssl_wrap_socket
Expand Down Expand Up @@ -645,7 +637,7 @@ def __init__(self, location, responses):
self.responses.append(r)

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

@staticmethod
def can_handle(data):
Expand Down
32 changes: 9 additions & 23 deletions mocket/mockhttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def body(self):
return self._protocol.body

def __str__(self):
return "{} - {} - {}".format(self.method, self.path, self.headers)
return f"{self.method} - {self.path} - {self.headers}"


class Response:
Expand Down Expand Up @@ -104,16 +104,14 @@ def __init__(self, body="", status=200, headers=None, lib_magic=magic):
self.data = self.get_protocol_data() + self.body

def get_protocol_data(self, str_format_fun_name="capitalize"):
status_line = "HTTP/1.1 {status_code} {status}".format(
status_code=self.status, status=STATUS[self.status]
)
status_line = f"HTTP/1.1 {self.status} {STATUS[self.status]}"
header_lines = CRLF.join(
(
"{0}: {1}".format(getattr(k, str_format_fun_name)(), v)
f"{getattr(k, str_format_fun_name)()}: {v}"
for k, v in self.headers.items()
)
)
return "{0}\r\n{1}\r\n\r\n".format(status_line, header_lines).encode(ENCODING)
return f"{status_line}\r\n{header_lines}\r\n\r\n".encode(ENCODING)

def set_base_headers(self):
self.headers = {
Expand All @@ -140,7 +138,7 @@ def set_extra_headers(self, headers):
True
"""
for k, v in headers.items():
self.headers["-".join((token.capitalize() for token in k.split("-")))] = v
self.headers["-".join(token.capitalize() for token in k.split("-"))] = v


class Entry(MocketEntry):
Expand All @@ -164,12 +162,9 @@ def __init__(self, uri, method, responses, match_querystring=True):

port = uri.port
if not port:
if uri.scheme == "https":
port = 443
else:
port = 80
port = 443 if uri.scheme == "https" else 80

super(Entry, self).__init__((uri.hostname, port), responses)
super().__init__((uri.hostname, port), responses)
self.schema = uri.scheme
self.path = uri.path
self.query = uri.query
Expand All @@ -178,16 +173,7 @@ def __init__(self, uri, method, responses, match_querystring=True):
self._match_querystring = match_querystring

def __repr__(self):
return (
"{}(method={!r}, schema={!r}, location={!r}, path={!r}, query={!r})".format(
self.__class__.__name__,
self.method,
self.schema,
self.location,
self.path,
self.query,
)
)
return f"{self.__class__.__name__}(method={self.method!r}, schema={self.schema!r}, location={self.location!r}, path={self.path!r}, query={self.query!r})"

def collect(self, data):
consume_response = True
Expand All @@ -200,7 +186,7 @@ def collect(self, data):
else:
self._sent_data = data

super(Entry, self).collect(self._sent_data)
super().collect(self._sent_data)

return consume_response

Expand Down
Loading

0 comments on commit 98a1ed9

Please sign in to comment.