Skip to content

Commit

Permalink
black formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
thewh1teagle committed Aug 4, 2024
1 parent 0c7386c commit 47e9cd8
Show file tree
Hide file tree
Showing 26 changed files with 244 additions and 231 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
"python.testing.pytestEnabled": true,
"editor.formatOnSave": true
}
3 changes: 2 additions & 1 deletion dev_requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pytest
pytest
black
8 changes: 4 additions & 4 deletions examples/cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
with Keyboard() as k:
k.press([KeyCodes.MOD_LEFT_GUI], KeyCodes.KEY_R)
sleep(2)
k.type('cmd')
k.type("cmd")
sleep(2)
k.press([], KeyCodes.KEY_ENTER)
sleep(2)
for i in range(1, 10):
k.type(f'color {i}')
k.type(f"color {i}")
k.press([], KeyCodes.KEY_ENTER)
k.type('cls & pause & exit')
k.press([], KeyCodes.KEY_ENTER)
k.type("cls & pause & exit")
k.press([], KeyCodes.KEY_ENTER)
2 changes: 1 addition & 1 deletion examples/drag_and_drop.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
sleep(2)
m.move(100, 100)
sleep(2)
m.release()
m.release()
4 changes: 2 additions & 2 deletions examples/github_star.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
with Keyboard() as k, Mouse() as m:
k.press([KeyCodes.MOD_LEFT_GUI], KeyCodes.KEY_R)
sleep(3)
k.type('https://github.com/thewh1teagle/zero-hid')
k.type("https://github.com/thewh1teagle/zero-hid")
sleep(2)
k.press([], KeyCodes.KEY_ENTER)
sleep(3)
for i in range(20):
m.move(127, -127)
for i in range(2):
m.move(-100, 0)
m.move(-100, 0)
m.move(0, 120)
sleep(3)
m.left_click()
2 changes: 1 addition & 1 deletion examples/hot_keys.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from zero_hid import Keyboard, KeyCodes

with Keyboard() as k:
k.press([KeyCodes.MOD_LEFT_GUI], KeyCodes.KEY_R)
k.press([KeyCodes.MOD_LEFT_GUI], KeyCodes.KEY_R)
2 changes: 1 addition & 1 deletion examples/keyboard.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from zero_hid import Keyboard

with Keyboard() as k:
k.type('Hello world!')
k.type("Hello world!")
4 changes: 2 additions & 2 deletions examples/keyboard_leds.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from zero_hid import Keyboard

with Keyboard() as k:
print('Press CapsLock / NumLock / ScrLock')
print("Press CapsLock / NumLock / ScrLock")
while True:
leds = k.blocking_read_led_state()
print(leds)
print(leds)
2 changes: 1 addition & 1 deletion examples/mouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

with Mouse() as m:
for i in range(5):
m.move(5,5)
m.move(5, 5)
2 changes: 1 addition & 1 deletion examples/mouse_absolute.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
with Mouse(absolute=True) as m:
m.move(1000, 1000)
sleep(0.2)
m.move(5000, 5000)
m.move(5000, 5000)
6 changes: 3 additions & 3 deletions examples/mouse_absolute_and_relative.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

# Absolute mouse used for positioning. relative mouse used for clicking.
with Mouse(absolute=False) as rel_mouse, Mouse(absolute=True) as abs_mouse:
abs_mouse.move(5000,5000)
abs_mouse.move(5000, 5000)
sleep(1)
rel_mouse.right_click()
sleep(1)
abs_mouse.move(3000,3000)
abs_mouse.move(3000, 3000)
sleep(1)
rel_mouse.left_click()
rel_mouse.left_click()
2 changes: 1 addition & 1 deletion examples/mouse_middle.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
m.middle_click(release=False)
m.move(0, 300)
sleep(3)
m.release()
m.release()
1 change: 0 additions & 1 deletion examples/scroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,3 @@
m.scroll_y(50)
sleep(1)
m.scroll_x(50)

13 changes: 8 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from setuptools import setup, find_packages

with open('README.md', 'r') as f:
with open("README.md", "r") as f:
long_description = f.read()
# with open('requirements.txt', 'r') as f:
# requirements = f.read().splitlines()
setup(
name="zero-hid", # How you named your package folder
packages=['zero_hid', 'zero_hid.hid'], # Chose the same as "name"
packages=["zero_hid", "zero_hid.hid"], # Chose the same as "name"
include_package_data=True,
version="v1.0.0", # Start with a small number and increase it with every change you make
license="LGPL", # Chose a license from here: https://help.github.com/articles/licensing-a-repository
Expand All @@ -32,8 +32,11 @@
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
],

)
print("********************* NOTE *********************")
print("To make this library work, you MUST enable usb gadget module in your raspberry pi")
print("Please refer to https://github.com/thewh1teagle/zero-hid#setup---tested-on-raspbian-lite-510")
print(
"To make this library work, you MUST enable usb gadget module in your raspberry pi"
)
print(
"Please refer to https://github.com/thewh1teagle/zero-hid#setup---tested-on-raspbian-lite-510"
)
3 changes: 2 additions & 1 deletion tests/common.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import tempfile
import os


def random_file():
# Create a temporary directory
temp_dir = tempfile.mkdtemp()

# Generate a random file name
file_name = tempfile.NamedTemporaryFile(dir=temp_dir, delete=False).name
# Return the path of the random file
return file_name
return file_name
12 changes: 5 additions & 7 deletions tests/keyboard_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,22 @@
from zero_hid import Keyboard
from common import random_file


def test_typing():
with open(random_file(), 'ab+') as f:
with open(random_file(), "ab+") as f:
k = Keyboard(f)
k.type("Hello world!")
f.seek(0)
data = f.read()
expect = b'\x02\x00\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x12\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00,\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x12\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x15\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x1e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
expect = b"\x02\x00\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x12\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00,\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x1a\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x12\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x15\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0f\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x1e\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
assert expect == data




def test_release():
with open(random_file(), 'ab+') as f:
with open(random_file(), "ab+") as f:
k = Keyboard(f)
k.release()
f.seek(0)
data = f.read()

assert b'\x00\x00\x00\x00\x00\x00\x00\x00' == data

assert b"\x00\x00\x00\x00\x00\x00\x00\x00" == data
19 changes: 10 additions & 9 deletions tests/mouse_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@
from zero_hid import Mouse
from common import random_file


def test_left_click():
with open(random_file(), 'ab+') as f:
m = Mouse(f)
m.left_click()
f.seek(0)
data = f.read()
f.close()
assert b'\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00' == data
with open(random_file(), "ab+") as f:
m = Mouse(f)
m.left_click()
f.seek(0)
data = f.read()
f.close()
assert b"\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00" == data


def test_move():
with open(random_file(), 'ab+') as f:
with open(random_file(), "ab+") as f:
m = Mouse(f)
m.move(100, 100)
f.seek(0)
data = f.read()
f.close()
assert b'\x00dd\x00\x00' == data
assert b"\x00dd\x00\x00" == data
14 changes: 8 additions & 6 deletions tests/write_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from zero_hid import hid



def test_process_with_result_child_completed():
def target():
pass
Expand All @@ -16,6 +15,7 @@ def target():
assert result.was_successful()
assert hid.write.ProcessResult(return_value=None, exception=None) == result


def test_process_with_result_child_not_completed():

def target():
Expand All @@ -29,30 +29,32 @@ def target():
# Clean up the running child process.
process.kill()


def test_process_with_result_child_exception():

def target():
raise Exception('Child exception')
raise Exception("Child exception")

# Silence stderr while the child exception is being raised to avoid
# polluting the terminal output.
with mock.patch('sys.stderr', io.StringIO()):
with mock.patch("sys.stderr", io.StringIO()):
process = hid.write.ProcessWithResult(target=target, daemon=True)
process.start()
process.join()
result = process.result()
assert result.was_successful() == False
assert hid.write.ProcessResult(return_value=None, exception=mock.ANY) == result
assert 'Child exception' == str(result.exception)
assert "Child exception" == str(result.exception)


def test_process_with_result_return_value():

def target():
return 'Done!'
return "Done!"

process = hid.write.ProcessWithResult(target=target, daemon=True)
process.start()
process.join()
result = process.result()
assert result.was_successful()
assert hid.write.ProcessResult(return_value='Done!', exception=None) == result
assert hid.write.ProcessResult(return_value="Done!", exception=None) == result
7 changes: 1 addition & 6 deletions zero_hid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,4 @@
from .hid.keycodes import KeyCodes
from . import defaults

__all__ = [
'Mouse',
'Keyboard',
'KeyCodes',
'defaults'
]
__all__ = ["Mouse", "Keyboard", "KeyCodes", "defaults"]
6 changes: 3 additions & 3 deletions zero_hid/defaults.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
KEYBOARD_PATH = '/dev/hidg0'
RELATIVE_MOUSE_PATH = '/dev/hidg1'
ABSOLUTE_MOUSE_PATH = '/dev/hidg2'
KEYBOARD_PATH = "/dev/hidg0"
RELATIVE_MOUSE_PATH = "/dev/hidg1"
ABSOLUTE_MOUSE_PATH = "/dev/hidg2"
9 changes: 6 additions & 3 deletions zero_hid/hid/keyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from . import write as hid_write
import select

def send_keystroke(keyboard_path, control_keys, hid_keycode, release = True):

def send_keystroke(keyboard_path, control_keys, hid_keycode, release=True):
buf = [0] * 8
buf[0] = control_keys
buf[2] = hid_keycode
Expand All @@ -13,9 +14,10 @@ def send_keystroke(keyboard_path, control_keys, hid_keycode, release = True):
if release:
release_keys(keyboard_path)

def read_last_report(keyboard: 'BufferedReader', size: int):

def read_last_report(keyboard: "BufferedReader", size: int):
if isinstance(keyboard, str):
keyboard = open(keyboard, 'rb')
keyboard = open(keyboard, "rb")
has_data = select.select([keyboard], [], [], 0.1)[0] != []
if has_data:
while select.select([keyboard], [], [], 0.1)[0] != []:
Expand All @@ -24,5 +26,6 @@ def read_last_report(keyboard: 'BufferedReader', size: int):
buf = keyboard.read(size)
return buf


def release_keys(keyboard_path):
hid_write.write_to_hid_interface(keyboard_path, [0] * 8)
Loading

0 comments on commit 47e9cd8

Please sign in to comment.