Skip to content

Commit

Permalink
add Action and Sequence base models
Browse files Browse the repository at this point in the history
  • Loading branch information
quillcraftsman committed Feb 28, 2024
1 parent f70334a commit 6581c80
Show file tree
Hide file tree
Showing 20 changed files with 241 additions and 37 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[run]
omit = ./testing/*
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,7 @@ cython_debug/
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
.idea/
.idea/

# sequence files
*.sequence
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ See more in [Full Documentation](https://replaywizard.craftsman.lol/install.html
## Quickstart

```python
from replay_wizard import info

print(info())
print('Empty yet')
```

### More examples in [Full Documentation][documentation_path]
Expand Down Expand Up @@ -139,12 +137,13 @@ This features will be built during 4 weeks.

- python (library was tested with **3.10**, **3.11** versions)
- pynput (this package use [LGPL-3.0 license](https://github.com/moses-palmer/pynput/blob/master/COPYING.LGPL) and used in this project as a third party library without modifications)
- pydantic

See more in [Full Documentation](https://replaywizard.craftsman.lol/about.html#requirements)

## Development Status

- Package available soon on [PyPi](https://pypi.org/project/replay-wizard/)
- Package available on [PyPi](https://pypi.org/project/replay-wizard/)

See more in [Full Documentation](https://replaywizard.craftsman.lol/about.html#development-status)

Expand Down
3 changes: 3 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# from replay_wizard import capture
#
# capture('one')
2 changes: 2 additions & 0 deletions pylintrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
[MAIN]
ignore-paths=./replay_wizard/package.py
disable=
W0621, # Redefining name outer scope (pytest fixtures)
4 changes: 2 additions & 2 deletions quickstart/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ def main():
ReplayWizard simple usage
:return:
"""
from replay_wizard import info # pylint: disable=import-outside-toplevel
from replay_wizard.models import Action # pylint: disable=import-outside-toplevel

print(info())
print(Action)
4 changes: 2 additions & 2 deletions replay_wizard/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
PyGenesis package
ReplayWizard package
"""
from .main import info
#from .capturing import capture
41 changes: 41 additions & 0 deletions replay_wizard/capturing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# """
# Main module
# """
# from pynput import keyboard
#
#
# def on_press(key, f):
# try:
# print('alphanumeric key {0} pressed'.format(
# key.char))
# f.write(f'{key.char} pressed\n')
# except AttributeError:
# print('special key {0} pressed'.format(
# key))
#
#
# def on_release(key, f):
# if key == keyboard.Key.esc:
# # Stop listener
# return False
# print('{0} released'.format(
# key))
# f.write(f'{key.char} released\n')
#
#
# def capture(name):
# file_name = f'{name}.sequence'
# with open(file_name, 'w', encoding='utf-8') as f:
# on_press_handler = lambda key: on_press(key, f)
# on_release_handler = lambda key: on_release(key, f)
#
# with keyboard.Listener(
# on_press=on_press_handler,
# on_release=on_release_handler) as listener:
# listener.join()
#
# # ...or, in a non-blocking fashion:
# # listener = keyboard.Listener(
# # on_press=on_press,
# # on_release=on_release)
# # listener.start()
13 changes: 0 additions & 13 deletions replay_wizard/main.py
Original file line number Diff line number Diff line change
@@ -1,13 +0,0 @@
"""
Main module
"""


def info():
"""
ReplayWizard info
:return: ReplayWizard usage info
"""
result = ('This is the mock of ReplayWizard package: '
'https://github.com/quillcraftsman/replay-wizard')
return result
5 changes: 5 additions & 0 deletions replay_wizard/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""
Models package
"""
from .action import Action, Subtypes
from .sequence import Sequence
24 changes: 24 additions & 0 deletions replay_wizard/models/action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
Action module
"""
from enum import Enum
from pydantic import BaseModel, ConfigDict


class Subtypes(str, Enum):
"""
Action subtype
"""
KEYBOARD = 'keyboard'
MOUSE = 'mouse'


class Action(BaseModel):
"""
Action model
"""
model_config = ConfigDict(frozen=True)

subtype: Subtypes
value: str
timedelta: float
26 changes: 26 additions & 0 deletions replay_wizard/models/sequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
"""
Sequence module
"""
from typing import List
from pydantic import BaseModel
from .action import Action


class Sequence(BaseModel):
"""
Action sequence
"""
name: str
actions: List[Action]

def __len__(self):
return len(self.actions)

def add(self, new_action: Action):
"""
Add action to sequence
:param new_action: Action to add
:return: None
"""
self.actions.append(new_action)
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pynput==1.7.6
pynput==1.7.6
pydantic==2.6.3
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def read(filename):
],
install_requires=[
'pynput==1.7.6',
'pydantic==2.6.3',
],
python_requires=">=3",
classifiers=[
Expand Down
14 changes: 0 additions & 14 deletions testing/test_pygenesis/test_main.py

This file was deleted.

File renamed without changes.
Empty file.
29 changes: 29 additions & 0 deletions testing/test_replay_wizard/test_models/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"""
Pytest fixtures
"""
from pytest import fixture
from replay_wizard.models import Action, Subtypes


@fixture
def put_a_action():
"""
Simple action fixture
"""
return Action(
subtype=Subtypes.KEYBOARD,
value='a',
timedelta=0.1,
)


@fixture
def put_a_action_dict():
"""
Action as dict fixture
"""
return {
'value': 'a',
'subtype': 'keyboard',
'timedelta': 0.1,
}
30 changes: 30 additions & 0 deletions testing/test_replay_wizard/test_models/test_actions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Test Action model module
"""
import pytest
from pydantic import ValidationError
from replay_wizard.models import Subtypes


def test_action(put_a_action):
"""
Test simple action
"""
assert put_a_action.value == 'a'
assert put_a_action.subtype == Subtypes.KEYBOARD
assert put_a_action.timedelta == 0.1


def test_action_is_frozen(put_a_action):
"""
Test that action is frozen (immutable)
"""
with pytest.raises(ValidationError):
put_a_action.value = 'b'


def test_to_dict(put_a_action, put_a_action_dict):
"""
Test action to dict
"""
assert put_a_action_dict == put_a_action.model_dump()
65 changes: 65 additions & 0 deletions testing/test_replay_wizard/test_models/test_sequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""
Test sequence module
"""
import pytest
from replay_wizard.models import Action, Sequence, Subtypes


@pytest.fixture
def empty_sequence():
"""
Empty sequence fixture
"""
return Sequence(
name='open youtube',
actions=[]
)


def test_full_sequence(put_a_action):
"""
Test full sequence data
"""
put_b = Action(
subtype=Subtypes.KEYBOARD,
value='b',
timedelta=0.1,
)
Sequence(
name='input a then b',
actions=[put_a_action, put_b]
)


def test_len(empty_sequence):
"""
Test __len__ method
"""
assert len(empty_sequence) == 0


def test_add(empty_sequence, put_a_action):
"""
Test add method
"""
assert len(empty_sequence) == 0
empty_sequence.add(put_a_action)
assert len(empty_sequence) == 1


def test_to_dict(empty_sequence, put_a_action, put_a_action_dict):
"""
Test sequence to dict
"""
result = {
'name': 'open youtube',
'actions': [],
}
assert result == empty_sequence.model_dump()
empty_sequence.add(put_a_action)

result['actions'] = [
put_a_action_dict
]

assert result == empty_sequence.model_dump()

0 comments on commit 6581c80

Please sign in to comment.