Skip to content

Commit

Permalink
Ruff fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
howroyd committed Feb 13, 2024
1 parent 1da6481 commit c412cfd
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 51 deletions.
9 changes: 7 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"editor.defaultFormatter": "charliermarsh.ruff",
"restructuredtext.preview.docutils.disabled": true
"[python]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.codeActionsOnSave": {
"source.organizeImports": "explicit"
}
}
}
44 changes: 24 additions & 20 deletions new_stuff/twitchplaysnew/main.py
Original file line number Diff line number Diff line change
@@ -1,61 +1,65 @@
#!./.venv/bin/python3
import dataclasses
import actions
import pprint

import actions


@dataclasses.dataclass(frozen=True, slots=True)
class PhasmophobiaPresetActions:
'''Preset actions'''
keybinds: dict[str, str] = dataclasses.field(default_factory=lambda: {
"walk_forward": "w",
"walk_backward": "s",
"walk_left": "a",
"walk_right": "d",
"crouch": "c",
"journal": "j",
"place": "f",
"grab": "e",
})
"""Preset actions"""

keybinds: dict[str, str] = dataclasses.field(
default_factory=lambda: {
"walk_forward": "w",
"walk_backward": "s",
"walk_left": "a",
"walk_right": "d",
"crouch": "c",
"journal": "j",
"place": "f",
"grab": "e",
}
)
toggle_duration: float = 0.1

def WalkForward(self, *, key: str | None = None, duration: float | None = None) -> actions.ActionSequence:
'''Walk forward'''
"""Walk forward"""
key = key or self.keybinds.get("walk_forward", "w")
return actions.PressReleaseKey(key, duration or 1.0)

def WalkBackward(self, *, key: str | None = None, duration: float | None = None) -> actions.ActionSequence:
'''Walk backward'''
"""Walk backward"""
key = key or self.keybinds.get("walk_backward", "s")
return actions.PressReleaseKey(key, duration or 1.0)

def WalkLeft(self, *, key: str | None = None, duration: float | None = None) -> actions.ActionSequence:
'''Walk left'''
"""Walk left"""
key = key or self.keybinds.get("walk_left", "a")
return actions.PressReleaseKey(key, duration or 1.0)

def WalkRight(self, *, key: str | None = None, duration: float | None = None) -> actions.ActionSequence:
'''Walk right'''
"""Walk right"""
key = key or self.keybinds.get("walk_right", "d")
return actions.PressReleaseKey(key, duration or 1.0)

def CrouchToggle(self, *, key: str | None = None) -> actions.ActionSequence:
'''Toggle crouch'''
"""Toggle crouch"""
key = key or self.keybinds.get("crouch", "c")
return actions.PressReleaseKey(key, self.toggle_duration)

def JournalToggle(self, *, key: str | None = None) -> actions.ActionSequence:
'''Toggle journal'''
"""Toggle journal"""
key = key or self.keybinds.get("journal", "j")
return actions.PressReleaseKey(key, self.toggle_duration)

def Place(self, *, key: str | None = None) -> actions.ActionSequence:
'''Place an item'''
"""Place an item"""
key = key or self.keybinds.get("place", "f")
return actions.PressReleaseKey(key, self.toggle_duration)

def Grab(self, *, key: str | None = None) -> actions.ActionSequence:
'''Pickup and item'''
"""Pickup and item"""
key = key or self.keybinds.get("grab", "e")
return actions.PressReleaseKey(key, self.toggle_duration)

Expand Down
7 changes: 3 additions & 4 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@ exclude = [
"node_modules",
"site-packages",
"venv",
"lla_droneiddetect",
"lla_turbofec",
"pynput_local",
]

# Same as Black.
line-length = 160
indent-width = 4

# Assume Python 3.8
target-version = "py38"
# Assume Python 3.11
target-version = "py311"

[lint]
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
Expand Down
9 changes: 5 additions & 4 deletions src/simonsays_drgreengiant/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import functools
import hashlib
import shutil
from typing import Callable, NoReturn, Self
from collections.abc import Callable
from typing import NoReturn, Self

import tomlkit

Expand All @@ -24,7 +25,7 @@
import urllib.request as urllib_request

with urllib_request.urlopen("https://github.com/howroyd/simonsays/releases/latest/download/blocklist") as _blocklist:
BLOCKLIST = set((line.strip() for line in _blocklist.readlines()))
BLOCKLIST = set(line.strip() for line in _blocklist.readlines())

del urllib_request
del _blocklist
Expand All @@ -33,7 +34,7 @@
def check_blocklist(channel: str | set[str], *, abort: bool = True, silent: bool = False) -> set[str] | NoReturn:
"""Return any channels/users in the blocklist"""
channels = channel if isinstance(channel, set) else set(channel)
blockedchannels = set((channel for channel in channels if hashlib.sha256(channel.strip().lower().encode("utf-8")).hexdigest() in BLOCKLIST))
blockedchannels = set(channel for channel in channels if hashlib.sha256(channel.strip().lower().encode("utf-8")).hexdigest() in BLOCKLIST)

if blockedchannels:
if not silent:
Expand Down Expand Up @@ -170,7 +171,7 @@ def load(cls, version: str, filename: str = None) -> Self:
filename = filename or DEFAULT_FILENAME

try:
with open(filename, "r") as f:
with open(filename) as f:
tomldata = tomlkit.loads(f.read())
except FileNotFoundError:
print(f"Config file not found: {filename=}")
Expand Down
2 changes: 1 addition & 1 deletion src/simonsays_drgreengiant/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def update_env() -> None:
newvars = {}

try:
with open(os.path.join(DATAPATH, ".env"), "r") as f:
with open(os.path.join(DATAPATH, ".env")) as f:
# with open(DATAPATH, "r") as f:
for line in f.readlines():
thisline = line.strip()
Expand Down
5 changes: 3 additions & 2 deletions src/simonsays_drgreengiant/errorcodes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!./.venv/bin/python3
import enum
from typing import Any, Iterable
from collections.abc import Iterable
from typing import Any


@enum.unique
Expand All @@ -24,7 +25,7 @@ class ErrorCode(enum.IntEnum):
def flatten(iter: Iterable[Any]) -> Any:
"""Flatten an iterable which may contain nested iterables"""
for x in iter:
if isinstance(x, Iterable) and not isinstance(x, (str, bytes)):
if isinstance(x, Iterable) and not isinstance(x, str | bytes):
yield from flatten(x)
else:
yield x
Expand Down
3 changes: 2 additions & 1 deletion src/simonsays_drgreengiant/gameactions.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!./.venv/bin/python3
import dataclasses
from typing import Any, Callable, Protocol, Self
from collections.abc import Callable
from typing import Any, Protocol, Self

from . import errorcodes, hidactions

Expand Down
6 changes: 3 additions & 3 deletions src/simonsays_drgreengiant/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import os
import pprint as pp
import tkinter as tk
from collections.abc import Iterable
from typing import Any, Callable, NoReturn, Optional
from collections.abc import Callable, Iterable
from typing import Any, NoReturn

import semantic_version

Expand All @@ -15,7 +15,7 @@
KEY_IGNORED_STR = "ignored"


def on_closing(exit_event: Optional[mp.Event] = None) -> NoReturn:
def on_closing(exit_event: mp.Event = None) -> NoReturn:
"""Exit the program when window is closed"""
if exit_event is None:
print("GUI closed, exiting...")
Expand Down
5 changes: 3 additions & 2 deletions src/simonsays_drgreengiant/hidactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import dataclasses
import enum
import platform
from typing import Any, Callable, Protocol, Self
from collections.abc import Callable
from typing import Any, Protocol, Self

from pynput.keyboard import Controller as Keyboard
from pynput.keyboard import Key, KeyCode
from pynput.keyboard import Key
from pynput.keyboard import Listener as KeyboardListener

from . import actions, environment, errorcodes
Expand Down
3 changes: 2 additions & 1 deletion src/simonsays_drgreengiant/phasmoactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
import dataclasses
import enum
import random
from typing import Any, Callable, ClassVar
from collections.abc import Callable
from typing import Any, ClassVar

from . import actions, environment, errorcodes, gameactions, hidactions

Expand Down
5 changes: 3 additions & 2 deletions src/simonsays_drgreengiant/twitchactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import dataclasses
import random
import time
from collections.abc import Iterable
from typing import Any, Callable
from collections.abc import Callable, Iterable
from typing import Any

from . import actions, environment, errorcodes, phasmoactions

Expand Down Expand Up @@ -236,6 +236,7 @@ def get_twitch_config() -> Config:
return global_config.twitchconfig

myactions = {key: TwitchAction(get_twitch_config, key, value) for key, value in phasmoactions.all_actions_dict(get_phasmo_config).items()}
force = False
myactions["look_up"].run(force=force)
myactions["look_down"].run(force=force)
myactions["look_down"].run(force=force)
Expand Down
1 change: 0 additions & 1 deletion tests/test_actions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import dataclasses

import pytest

from simonsays_drgreengiant import actions, errorcodes

WAIT_TIME = 0.01
Expand Down
1 change: 0 additions & 1 deletion tests/test_errorcodes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest

from simonsays_drgreengiant import errorcodes


Expand Down
10 changes: 6 additions & 4 deletions tests/test_gameactions.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import dataclasses

import pytest

from simonsays_drgreengiant import gameactions, hidactions, errorcodes
from simonsays_drgreengiant import errorcodes, gameactions, hidactions


@dataclasses.dataclass(slots=True)
Expand Down Expand Up @@ -62,7 +60,11 @@ def test_genericaction():
"""Test GenericAction"""
testitem = {"test": Config()}
config = gameactions.Config(testitem)
configfn: gameactions.ConfigFn = lambda: config

def get_config() -> gameactions.Config:
return config

configfn: gameactions.ConfigFn = get_config
genericaction = Action(configfn, "test", False)

hidaction = hidactions.KeyboardActionConfig("x")
Expand Down
1 change: 0 additions & 1 deletion tests/test_hidactions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import pytest

from simonsays_drgreengiant import hidactions


Expand Down
1 change: 0 additions & 1 deletion tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from simonsays_drgreengiant import simonsays


def test_test():
Expand Down
2 changes: 1 addition & 1 deletion tools/hashnames.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
if __name__ == "__main__":
names = set(NAMES)

blocked = set((hashlib.sha256(name.strip().lower().encode("utf-8")).hexdigest() for name in names))
blocked = set(hashlib.sha256(name.strip().lower().encode("utf-8")).hexdigest() for name in names)

blocked.update(OTHER)

Expand Down

0 comments on commit c412cfd

Please sign in to comment.