Skip to content

Commit

Permalink
chore: Format code with ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
chookity-pokk committed Oct 2, 2024
1 parent adef634 commit 76dc74d
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 98 deletions.
12 changes: 6 additions & 6 deletions pantalaimon/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,6 @@ async def handle_key_request_message(self, message):
for share in self.get_active_key_requests(
message.user_id, message.device_id
):

continued = True

if not self.continue_key_share(share):
Expand Down Expand Up @@ -811,8 +810,9 @@ def pan_decrypt_event(self, event_dict, room_id=None, ignore_failures=True):

if not isinstance(event, MegolmEvent):
logger.warn(
"Encrypted event is not a megolm event:"
"\n{}".format(pformat(event_dict))
"Encrypted event is not a megolm event:" "\n{}".format(
pformat(event_dict)
)
)
return False

Expand All @@ -836,9 +836,9 @@ def pan_decrypt_event(self, event_dict, room_id=None, ignore_failures=True):
decrypted_event.source["content"]["url"] = decrypted_event.url

if decrypted_event.thumbnail_url:
decrypted_event.source["content"]["info"][
"thumbnail_url"
] = decrypted_event.thumbnail_url
decrypted_event.source["content"]["info"]["thumbnail_url"] = (
decrypted_event.thumbnail_url
)

event_dict.update(decrypted_event.source)
event_dict["decrypted"] = True
Expand Down
1 change: 0 additions & 1 deletion pantalaimon/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ def read(self):

try:
for section_name, section in config.items():

if section_name == "Default":
continue

Expand Down
14 changes: 9 additions & 5 deletions pantalaimon/daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,8 @@ async def _verify_device(self, message_id, client, device):

if ret:
msg = (
f"Device {device.id} of user " f"{device.user_id} successfully verified."
f"Device {device.id} of user "
f"{device.user_id} successfully verified."
)
await client.send_update_device(device)
else:
Expand Down Expand Up @@ -309,7 +310,6 @@ async def receive_message(self, message):
DeviceUnblacklistMessage,
),
):

device = client.device_store[message.user_id].get(message.device_id, None)

if not device:
Expand Down Expand Up @@ -616,7 +616,9 @@ async def start_pan_client(
await pan_client.close()
return

logger.info(f"Successfully started new background sync client for " f"{user_id}")
logger.info(
f"Successfully started new background sync client for " f"{user_id}"
)

await self.send_ui_message(
UpdateUsersMessage(self.name, user_id, pan_client.device_id)
Expand Down Expand Up @@ -733,7 +735,7 @@ async def decrypt_loop(client, body):
return decryption_method(body, ignore_failures=False)
except EncryptionError:
logger.info("Error decrypting sync, waiting for next pan " "sync")
await client.synced.wait(),
(await client.synced.wait(),)
logger.info("Pan synced, retrying decryption.")

try:
Expand Down Expand Up @@ -1273,7 +1275,9 @@ async def _load_decrypted_file(self, server_name, media_id, file_name):
client = next(iter(self.pan_clients.values()))

try:
response = await client.download(server_name=server_name, media_id=media_id, filename=file_name)
response = await client.download(
server_name=server_name, media_id=media_id, filename=file_name
)
except ClientConnectionError as e:
raise e

Expand Down
1 change: 0 additions & 1 deletion pantalaimon/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,6 @@ def load_events(
)

for message in query:

event = message.event

event_dict = {
Expand Down
1 change: 0 additions & 1 deletion pantalaimon/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,6 @@ def load_all_devices(self):
device_store = defaultdict(dict)

for d in account.device_keys:

if d.deleted:
continue

Expand Down
13 changes: 7 additions & 6 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
url="https://github.com/matrix-org/pantalaimon",
author="The Matrix.org Team",
author_email="poljar@termina.org.uk",
description=("A Matrix proxy daemon that adds E2E encryption "
"capabilities."),
description=("A Matrix proxy daemon that adds E2E encryption " "capabilities."),
long_description=long_description,
long_description_content_type="text/markdown",
license="Apache License, Version 2.0",
Expand All @@ -29,7 +28,7 @@
"cachetools >= 3.0.0",
"prompt_toolkit > 2, < 4",
"typing;python_version<'3.5'",
"matrix-nio[e2e] >= 0.20, < 0.21"
"matrix-nio[e2e] >= 0.20, < 0.21",
],
extras_require={
"ui": [
Expand All @@ -40,8 +39,10 @@
]
},
entry_points={
"console_scripts": ["pantalaimon=pantalaimon.main:main",
"panctl=pantalaimon.panctl:main"],
"console_scripts": [
"pantalaimon=pantalaimon.main:main",
"panctl=pantalaimon.panctl:main",
],
},
zip_safe=False
zip_safe=False,
)
47 changes: 20 additions & 27 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ def access_token(self):
def client(self):
return ClientInfo(faker.mx_id(), faker.access_token())


def avatar_url(self):
return "mxc://{}/{}#auto".format(
faker.hostname(),
"".join(choices(ascii_letters) for i in range(24))
faker.hostname(), "".join(choices(ascii_letters) for i in range(24))
)

def olm_key_pair(self):
Expand All @@ -56,7 +54,6 @@ def olm_device(self):
)



faker.add_provider(Provider)


Expand All @@ -80,13 +77,7 @@ def tempdir():
@pytest.fixture
def panstore(tempdir):
for _ in range(10):
store = SqliteStore(
faker.mx_id(),
faker.device_id(),
tempdir,
"",
"pan.db"
)
store = SqliteStore(faker.mx_id(), faker.device_id(), tempdir, "", "pan.db")
account = OlmAccount()
store.save_account(account)

Expand Down Expand Up @@ -130,21 +121,23 @@ async def pan_proxy_server(tempdir, aiohttp_server):
recv_queue=ui_queue.async_q,
proxy=None,
ssl=False,
client_store_class=SqliteStore
client_store_class=SqliteStore,
)

app.add_routes([
web.post("/_matrix/client/r0/login", proxy.login),
web.get("/_matrix/client/r0/sync", proxy.sync),
web.get("/_matrix/client/r0/rooms/{room_id}/messages", proxy.messages),
web.put(
r"/_matrix/client/r0/rooms/{room_id}/send/{event_type}/{txnid}",
proxy.send_message
),
web.post("/_matrix/client/r0/user/{user_id}/filter", proxy.filter),
web.post("/_matrix/client/r0/search", proxy.search),
web.options("/_matrix/client/r0/search", proxy.search_opts),
])
app.add_routes(
[
web.post("/_matrix/client/r0/login", proxy.login),
web.get("/_matrix/client/r0/sync", proxy.sync),
web.get("/_matrix/client/r0/rooms/{room_id}/messages", proxy.messages),
web.put(
r"/_matrix/client/r0/rooms/{room_id}/send/{event_type}/{txnid}",
proxy.send_message,
),
web.post("/_matrix/client/r0/user/{user_id}/filter", proxy.filter),
web.post("/_matrix/client/r0/search", proxy.search),
web.options("/_matrix/client/r0/search", proxy.search_opts),
]
)

server = await aiohttp_server(app)

Expand All @@ -161,7 +154,7 @@ async def running_proxy(pan_proxy_server, aioresponse, aiohttp_client):
"access_token": "abc123",
"device_id": "GHTYAJCE",
"home_server": "example.org",
"user_id": "@example:example.org"
"user_id": "@example:example.org",
}

aioclient = await aiohttp_client(server)
Expand All @@ -170,7 +163,7 @@ async def running_proxy(pan_proxy_server, aioresponse, aiohttp_client):
"https://example.org/_matrix/client/r0/login",
status=200,
payload=login_response,
repeat=True
repeat=True,
)

await aioclient.post(
Expand All @@ -179,7 +172,7 @@ async def running_proxy(pan_proxy_server, aioresponse, aiohttp_client):
"type": "m.login.password",
"user": "example",
"password": "wordpass",
}
},
)

yield server, aioclient, proxy, queues
Expand Down
8 changes: 6 additions & 2 deletions tests/pan_client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,9 @@ async def test_history_fetching_tasks(self, client, aioresponse, loop):
)

aioresponse.get(
sync_url, status=200, payload=self.initial_sync_response,
sync_url,
status=200,
payload=self.initial_sync_response,
)

aioresponse.get(sync_url, status=200, payload=self.empty_sync, repeat=True)
Expand Down Expand Up @@ -454,7 +456,9 @@ async def test_history_fetching_resume(self, client, aioresponse, loop):
)

aioresponse.get(
sync_url, status=200, payload=self.initial_sync_response,
sync_url,
status=200,
payload=self.initial_sync_response,
)

aioresponse.get(sync_url, status=200, payload=self.empty_sync, repeat=True)
Expand Down
34 changes: 14 additions & 20 deletions tests/proxy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def login_response(self):
"access_token": "abc123",
"device_id": "GHTYAJCE",
"home_server": "example.org",
"user_id": "@example:example.org"
"user_id": "@example:example.org",
}

@property
Expand All @@ -36,12 +36,7 @@ def sync_response(self):

@property
def keys_upload_response(self):
return {
"one_time_key_counts": {
"curve25519": 10,
"signed_curve25519": 20
}
}
return {"one_time_key_counts": {"curve25519": 10, "signed_curve25519": 20}}

@property
def example_devices(self):
Expand All @@ -52,10 +47,7 @@ def example_devices(self):
devices[device.user_id][device.id] = device

bob_device = OlmDevice(
BOB_ID,
BOB_DEVICE,
{"ed25519": BOB_ONETIME,
"curve25519": BOB_CURVE}
BOB_ID, BOB_DEVICE, {"ed25519": BOB_ONETIME, "curve25519": BOB_CURVE}
)

devices[BOB_ID][BOB_DEVICE] = bob_device
Expand All @@ -71,7 +63,7 @@ async def test_daemon_start(self, pan_proxy_server, aiohttp_client, aioresponse)
"https://example.org/_matrix/client/r0/login",
status=200,
payload=self.login_response,
repeat=True
repeat=True,
)

assert not daemon.pan_clients
Expand All @@ -82,7 +74,7 @@ async def test_daemon_start(self, pan_proxy_server, aiohttp_client, aioresponse)
"type": "m.login.password",
"user": "example",
"password": "wordpass",
}
},
)

assert resp.status == 200
Expand All @@ -105,11 +97,11 @@ async def test_pan_client_sync(self, pan_proxy_server, aiohttp_client, aiorespon
"https://example.org/_matrix/client/r0/login",
status=200,
payload=self.login_response,
repeat=True
repeat=True,
)

sync_url = re.compile(
r'^https://example\.org/_matrix/client/r0/sync\?access_token=.*'
r"^https://example\.org/_matrix/client/r0/sync\?access_token=.*"
)

aioresponse.get(
Expand All @@ -124,14 +116,16 @@ async def test_pan_client_sync(self, pan_proxy_server, aiohttp_client, aiorespon
"type": "m.login.password",
"user": "example",
"password": "wordpass",
}
},
)

# Check that the pan client started to sync after logging in.
pan_client = list(daemon.pan_clients.values())[0]
assert len(pan_client.rooms) == 1

async def test_pan_client_keys_upload(self, pan_proxy_server, aiohttp_client, aioresponse):
async def test_pan_client_keys_upload(
self, pan_proxy_server, aiohttp_client, aioresponse
):
server, daemon, _ = pan_proxy_server

client = await aiohttp_client(server)
Expand All @@ -140,11 +134,11 @@ async def test_pan_client_keys_upload(self, pan_proxy_server, aiohttp_client, ai
"https://example.org/_matrix/client/r0/login",
status=200,
payload=self.login_response,
repeat=True
repeat=True,
)

sync_url = re.compile(
r'^https://example\.org/_matrix/client/r0/sync\?access_token=.*'
r"^https://example\.org/_matrix/client/r0/sync\?access_token=.*"
)

aioresponse.get(
Expand All @@ -169,7 +163,7 @@ async def test_pan_client_keys_upload(self, pan_proxy_server, aiohttp_client, ai
"type": "m.login.password",
"user": "example",
"password": "wordpass",
}
},
)

pan_client = list(daemon.pan_clients.values())[0]
Expand Down
Loading

0 comments on commit 76dc74d

Please sign in to comment.