Skip to content

Commit

Permalink
Fix up typing and remove unused imports
Browse files Browse the repository at this point in the history
  • Loading branch information
C0rn3j committed Dec 28, 2024
1 parent 9ec21bf commit 72a7bbd
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 90 deletions.
9 changes: 3 additions & 6 deletions pypresence/baseclient.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import asyncio
import inspect
import json
import os
import struct
import sys
import tempfile
from typing import Union, Optional

# TODO: Get rid of this import * lol
from .exceptions import *
Expand All @@ -30,8 +27,8 @@ def __init__(self, client_id: str, **kwargs):
else:
self.update_event_loop(get_event_loop())

self.sock_reader: Optional[asyncio.StreamReader] = None
self.sock_writer: Optional[asyncio.StreamWriter] = None
self.sock_reader: asyncio.StreamReader | None = None
self.sock_writer: asyncio.StreamWriter | None = None

self.client_id = client_id

Expand Down Expand Up @@ -88,7 +85,7 @@ async def read_output(self):
raise ServerError(payload["data"]["message"])
return payload

def send_data(self, op: int, payload: Union[dict, Payload]):
def send_data(self, op: int, payload: dict | Payload):
if isinstance(payload, Payload):
payload = payload.data
payload = json.dumps(payload)
Expand Down
71 changes: 36 additions & 35 deletions pypresence/client.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
import asyncio
import inspect
import struct
Expand Down Expand Up @@ -53,7 +54,7 @@ def on_event(self, data):
self.sock_reader._transport = None
else:
self.sock_reader._paused = True

end = 0
while end < len(data):
# While chunks are available in data
Expand Down Expand Up @@ -99,9 +100,9 @@ def get_channels(self, guild_id: str):
self.send_data(1, payload)
return self.loop.run_until_complete(self.read_output())

def set_user_voice_settings(self, user_id: str, pan_left: float = None,
pan_right: float = None, volume: int = None,
mute: bool = None):
def set_user_voice_settings(self, user_id: str, pan_left: float | None = None,
pan_right: float | None = None, volume: int | None = None,
mute: bool | None = None):
payload = Payload.set_user_voice_settings(user_id, pan_left, pan_right, volume, mute)
self.send_data(1, payload)
return self.loop.run_until_complete(self.read_output())
Expand All @@ -122,21 +123,21 @@ def select_text_channel(self, channel_id: str):
return self.loop.run_until_complete(self.read_output())

def set_activity(self, pid: int = os.getpid(),
activity_type: ActivityType = None,
state: str = None, details: str = None,
start: int = None, end: int = None,
large_image: str = None, large_text: str = None,
small_image: str = None, small_text: str = None,
party_id: str = None, party_size: list = None,
join: str = None, spectate: str = None,
match: str = None, buttons: list = None,
activity_type: ActivityType | None = None,
state: str | None = None, details: str | None = None,
start: int | None = None, end: int | None = None,
large_image: str | None = None, large_text: str | None = None,
small_image: str | None = None, small_text: str | None = None,
party_id: str | None = None, party_size: list | None = None,
join: str | None = None, spectate: str | None = None,
match: str | None = None, buttons: list | None = None,
instance: bool = True):
payload = Payload.set_activity(pid=pid, activity_type=activity_type, state=state, details=details, start=start,
end=end, large_image=large_image, large_text=large_text, small_image=small_image,
small_text=small_text, party_id=party_id, party_size=party_size, join=join,
spectate=spectate, match=match, buttons=buttons, instance=instance,
activity=True)

self.send_data(1, payload)
return self.loop.run_until_complete(self.read_output())

Expand Down Expand Up @@ -164,11 +165,11 @@ def get_voice_settings(self):
self.send_data(1, payload)
return self.loop.run_until_complete(self.read_output())

def set_voice_settings(self, _input: dict = None, output: dict = None,
mode: dict = None, automatic_gain_control: bool = None,
echo_cancellation: bool = None, noise_suppression: bool = None,
qos: bool = None, silence_warning: bool = None,
deaf: bool = None, mute: bool = None):
def set_voice_settings(self, _input: dict | None = None, output: dict | None = None,
mode: dict | None = None, automatic_gain_control: bool | None = None,
echo_cancellation: bool | None = None, noise_suppression: bool | None = None,
qos: bool | None = None, silence_warning: bool | None = None,
deaf: bool | None = None, mute: bool | None = None):
payload = Payload.set_voice_settings(_input, output, mode, automatic_gain_control, echo_cancellation,
noise_suppression, qos, silence_warning, deaf, mute)
self.send_data(1, payload)
Expand Down Expand Up @@ -284,9 +285,9 @@ async def get_channels(self, guild_id: str):
self.send_data(1, payload)
return await self.read_output()

async def set_user_voice_settings(self, user_id: str, pan_left: float = None,
pan_right: float = None, volume: int = None,
mute: bool = None):
async def set_user_voice_settings(self, user_id: str, pan_left: float | None = None,
pan_right: float | None = None, volume: int | None = None,
mute: bool | None = None):
payload = Payload.set_user_voice_settings(user_id, pan_left, pan_right, volume, mute)
self.send_data(1, payload)
return await self.read_output()
Expand All @@ -307,15 +308,15 @@ async def select_text_channel(self, channel_id: str):
return await self.read_output()

async def set_activity(self, pid: int = os.getpid(),
activity_type: ActivityType = None,
state: str = None, details: str = None,
start: int = None, end: int = None,
large_image: str = None, large_text: str = None,
small_image: str = None, small_text: str = None,
party_id: str = None, party_size: list = None,
join: str = None, spectate: str = None,
buttons: list = None,
match: str = None, instance: bool = True):
activity_type: ActivityType | None = None,
state: str | None = None, details: str | None = None,
start: int | None = None, end: int | None = None,
large_image: str | None = None, large_text: str | None = None,
small_image: str | None = None, small_text: str | None = None,
party_id: str | None = None, party_size: list | None = None,
join: str | None = None, spectate: str | None = None,
buttons: list | None = None,
match: str | None = None, instance: bool = True):
payload = Payload.set_activity(pid, activity_type, state, details, start, end, large_image,
large_text, small_image, small_text, party_id, party_size,
join, spectate, match, buttons, instance, activity=True)
Expand Down Expand Up @@ -346,11 +347,11 @@ async def get_voice_settings(self):
self.send_data(1, payload)
return await self.read_output()

async def set_voice_settings(self, _input: dict = None, output: dict = None,
mode: dict = None, automatic_gain_control: bool = None,
echo_cancellation: bool = None, noise_suppression: bool = None,
qos: bool = None, silence_warning: bool = None,
deaf: bool = None, mute: bool = None):
async def set_voice_settings(self, _input: dict | None = None, output: dict | None = None,
mode: dict | None = None, automatic_gain_control: bool | None = None,
echo_cancellation: bool | None = None, noise_suppression: bool | None = None,
qos: bool | None = None, silence_warning: bool | None = None,
deaf: bool | None = None, mute: bool | None = None):
payload = Payload.set_voice_settings(_input, output, mode, automatic_gain_control, echo_cancellation,
noise_suppression, qos, silence_warning, deaf, mute)
self.send_data(1, payload)
Expand Down
7 changes: 4 additions & 3 deletions pypresence/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations
class PyPresenceException(Exception):
def __init__(self, message: str = None):
def __init__(self, message: str | None = None):
if message is None:
message = 'An error has occurred within PyPresence'
super().__init__(message)
Expand All @@ -16,7 +17,7 @@ def __init__(self):


class InvalidArgument(PyPresenceException):
def __init__(self, expected, received, description: str = None):
def __init__(self, expected, received, description: str | None = None):
description = '\n{0}'.format(description) if description else ''
super().__init__('Bad argument passed. Expected {0} but got {1} instead{2}'.format(expected, received,
description)
Expand All @@ -29,7 +30,7 @@ def __init__(self, message: str):


class DiscordError(PyPresenceException):
def __init__(self, code: int, message: str, override=False):
def __init__(self, code: int, message: str, override: bool = False):
self.code = code
self.message = message
super().__init__('Error Code: {0} Message: {1}'.format(code, message) if not override else message)
Expand Down
39 changes: 20 additions & 19 deletions pypresence/payloads.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from __future__ import annotations

import json
import os
import time
from typing import List, Union

from .utils import remove_none
from .types import ActivityType
Expand All @@ -23,15 +24,15 @@ def time():

@classmethod
def set_activity(cls, pid: int = os.getpid(),
activity_type: ActivityType = None,
state: str = None, details: str = None,
start: int = None, end: int = None,
large_image: str = None, large_text: str = None,
small_image: str = None, small_text: str = None,
party_id: str = None, party_size: list = None,
join: str = None, spectate: str = None,
match: str = None, buttons: list = None,
instance: bool = True, activity: Union[bool, None] = True,
activity_type: ActivityType | None = None,
state: str | None = None, details: str | None = None,
start: int | None = None, end: int | None = None,
large_image: str | None = None, large_text: str | None = None,
small_image: str | None = None, small_text: str | None = None,
party_id: str | None = None, party_size: list | None = None,
join: str | None = None, spectate: str | None = None,
match: str | None = None, buttons: list | None = None,
instance: bool = True, activity: bool | None = True,
_rn: bool = True):

# They should already be an int because we give typehints, but some people are fucking stupid and use
Expand Down Expand Up @@ -91,7 +92,7 @@ def set_activity(cls, pid: int = os.getpid(),
return cls(payload, clear)

@classmethod
def authorize(cls, client_id: str, scopes: List[str]):
def authorize(cls, client_id: str, scopes: list[str]):
payload = {
"cmd": "AUTHORIZE",
"args": {
Expand Down Expand Up @@ -162,9 +163,9 @@ def get_channel(cls, channel_id: str):
return cls(payload)

@classmethod
def set_user_voice_settings(cls, user_id: str, pan_left: float = None,
pan_right: float = None, volume: int = None,
mute: bool = None):
def set_user_voice_settings(cls, user_id: str, pan_left: float | None = None,
pan_right: float | None = None, volume: int | None = None,
mute: bool | None = None):
payload = {
"cmd": "SET_USER_VOICE_SETTINGS",
"args": {
Expand Down Expand Up @@ -254,11 +255,11 @@ def get_voice_settings(cls):
return cls(payload)

@classmethod
def set_voice_settings(cls, _input: dict = None, output: dict = None,
mode: dict = None, automatic_gain_control: bool = None,
echo_cancellation: bool = None, noise_suppression: bool = None,
qos: bool = None, silence_warning: bool = None,
deaf: bool = None, mute: bool = None):
def set_voice_settings(cls, _input: dict | None = None, output: dict | None = None,
mode: dict | None = None, automatic_gain_control: bool | None = None,
echo_cancellation: bool | None = None, noise_suppression: bool | None = None,
qos: bool | None = None, silence_warning: bool | None = None,
deaf: bool | None = None, mute: bool | None = None):
payload = {
"cmd": "SET_VOICE_SETTINGS",
"args": {
Expand Down
39 changes: 19 additions & 20 deletions pypresence/presence.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import json
from __future__ import annotations
import os
import time
import sys

from .baseclient import BaseClient
from .payloads import Payload
from .utils import remove_none, get_event_loop
from .utils import get_event_loop
from .types import ActivityType


Expand All @@ -15,15 +14,15 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def update(self, pid: int = os.getpid(),
activity_type: ActivityType = None,
state: str = None, details: str = None,
start: int = None, end: int = None,
large_image: str = None, large_text: str = None,
small_image: str = None, small_text: str = None,
party_id: str = None, party_size: list = None,
join: str = None, spectate: str = None,
match: str = None, buttons: list = None,
instance: bool = True, payload_override: dict = None):
activity_type: ActivityType | None = None,
state: str | None = None, details: str | None = None,
start: int | None = None, end: int | None = None,
large_image: str | None = None, large_text: str | None = None,
small_image: str | None = None, small_text: str | None = None,
party_id: str | None = None, party_size: list | None = None,
join: str | None = None, spectate: str | None = None,
match: str | None = None, buttons: list | None = None,
instance: bool = True, payload_override: dict | None = None):

if payload_override is None:
payload = Payload.set_activity(pid=pid, activity_type=activity_type, state=state, details=details,
Expand Down Expand Up @@ -58,14 +57,14 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs, isasync=True)

async def update(self, pid: int = os.getpid(),
activity_type: ActivityType = None,
state: str = None, details: str = None,
start: int = None, end: int = None,
large_image: str = None, large_text: str = None,
small_image: str = None, small_text: str = None,
party_id: str = None, party_size: list = None,
join: str = None, spectate: str = None,
match: str = None, buttons: list = None,
activity_type: ActivityType | None = None,
state: str | None = None, details: str | None = None,
start: int | None = None, end: int | None = None,
large_image: str | None = None, large_text: str | None = None,
small_image: str | None = None, small_text: str | None = None,
party_id: str | None = None, party_size: list | None = None,
join: str | None = None, spectate: str | None = None,
match: str | None = None, buttons: list | None = None,
instance: bool = True):
payload = Payload.set_activity(pid=pid, activity_type=activity_type, state=state, details=details,
start=start, end=end, large_image=large_image, large_text=large_text,
Expand Down
10 changes: 3 additions & 7 deletions pypresence/utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
"""Util functions that are needed but messy."""
import asyncio
import json
import os
import sys
import tempfile
import time
import socket

from .exceptions import PyPresenceException


def remove_none(d: dict):
for item in d.copy():
Expand All @@ -22,7 +18,7 @@ def remove_none(d: dict):
return d


def test_ipc_path(path):
def test_ipc_path(path) -> bool:
'''Tests an IPC pipe to ensure that it actually works'''
if sys.platform == 'win32' or sys.platform == 'win64':
with open(path):
Expand All @@ -47,7 +43,7 @@ def get_ipc_path(pipe=None):
paths = ['.']
else:
return

for path in paths:
full_path = os.path.abspath(os.path.join(tempdir, path))
if sys.platform == 'win32' or os.path.isdir(full_path):
Expand All @@ -56,7 +52,7 @@ def get_ipc_path(pipe=None):
return entry.path


def get_event_loop(force_fresh=False):
def get_event_loop(force_fresh: bool = False):
if force_fresh:
return asyncio.new_event_loop()
try:
Expand Down

0 comments on commit 72a7bbd

Please sign in to comment.