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

Fix up CI, improve typing, remove dead imports, and other minor changes #248

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
4 changes: 2 additions & 2 deletions .github/workflows/lint_python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ jobs:
lint_python:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- run: pip install --upgrade pip wheel setuptools
- run: pip install bandit black codespell flake8 isort mypy pytest pyupgrade safety
- run: bandit --recursive --skip B101,B311 .
Expand Down
32 changes: 16 additions & 16 deletions .github/workflows/publish-to-pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install setuptools wheel twine
- name: Build and publish
env:
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
run: |
python setup.py sdist bdist_wheel
twine upload dist/*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![GitHub stars](https://img.shields.io/github/stars/qwertyquerty/pypresence.svg?style=for-the-badge&label=Stars)](https://github.com/qwertyquerty/pypresence) [![license](https://img.shields.io/github/license/qwertyquerty/pypresence.svg?style=for-the-badge)](https://github.com/qwertyquerty/pypresence/blob/master/LICENSE) ![GitHub last commit](https://img.shields.io/github/last-commit/qwertyquerty/pypresence.svg?style=for-the-badge) ![GitHub top language](https://img.shields.io/github/languages/top/qwertyquerty/pypresence.svg?style=for-the-badge) ![PyPI](https://img.shields.io/pypi/v/pypresence.svg?style=for-the-badge)

## NOTE: Only Python versions 3.8 and above are supported.
## NOTE: Only Python versions 3.9 and above are supported.

### [Documentation](https://qwertyquerty.github.io/pypresence/html/index.html), [Discord Server](https://discord.gg/JF3kg77), [Patreon](https://www.patreon.com/qwertyquerty)

Expand Down
2 changes: 1 addition & 1 deletion pypresence/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
__author__ = 'qwertyquerty'
__copyright__ = 'Copyright 2018 - Current qwertyquerty'
__license__ = 'MIT'
__version__ = '4.3.0'
__version__ = '4.4.0'
13 changes: 6 additions & 7 deletions pypresence/baseclient.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from __future__ import annotations

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 +29,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 +87,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 All @@ -110,7 +109,7 @@ async def handshake(self):
try:
if sys.platform == 'linux' or sys.platform == 'darwin':
self.sock_reader, self.sock_writer = await asyncio.wait_for(asyncio.open_unix_connection(ipc_path), self.connection_timeout)
elif sys.platform == 'win32' or sys.platform == 'win64':
elif sys.platform == 'win32':
self.sock_reader = asyncio.StreamReader(loop=self.loop)
reader_protocol = asyncio.StreamReaderProtocol(self.sock_reader, loop=self.loop)
self.sock_writer, _ = await asyncio.wait_for(self.loop.create_pipe_connection(lambda: reader_protocol, ipc_path), self.connection_timeout)
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
Loading
Loading