Skip to content

Commit

Permalink
refactor: move convert_to_key_code to utils/
Browse files Browse the repository at this point in the history
  • Loading branch information
winstxnhdw committed Jul 25, 2023
1 parent bcc3f4e commit 7ce25af
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 36 deletions.
37 changes: 1 addition & 36 deletions keywin/keyboard/__init__.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,7 @@
from itertools import groupby

from keywin.keyboard.codes import Typables
from keywin.keyboard.exceptions import UnknownTypableException
from keywin.keyboard.utils import convert_to_key_code
from keywin.send_input import press_keyboard


def convert_to_key_code(string: str) -> list[list[int]]:
"""
Summary
-------
convert a string to key code(s)
Parameters
----------
string (str) : string to convert
Returns
-------
key_codes (Iterable[int]) : converted key code(s)
"""
try:
key_codes: list[list[int]] = []

for key_with_modifier, group in groupby((Typables.table[character] for character in string), lambda x: isinstance(x, list)):
group_list = list(group)

if key_with_modifier:
key_codes.extend(group_list) # type: ignore

else:
key_codes.append(group_list) # type: ignore

return key_codes

except KeyError as error:
raise UnknownTypableException from error


def press(*keys: int):
"""
Summary
Expand Down
2 changes: 2 additions & 0 deletions keywin/keyboard/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from keywin.keyboard.utils.convert_to_key_code import \
convert_to_key_code as convert_to_key_code
36 changes: 36 additions & 0 deletions keywin/keyboard/utils/convert_to_key_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from itertools import groupby

from keywin.keyboard.codes import Typables
from keywin.keyboard.exceptions import UnknownTypableException


def convert_to_key_code(string: str) -> list[list[int]]:
"""
Summary
-------
convert a string to key code(s)
Parameters
----------
string (str) : string to convert
Returns
-------
key_codes (list[list[int]]) : converted key code(s)
"""
try:
key_codes: list[list[int]] = []

for key_with_modifier, group in groupby((Typables.table[character] for character in string), lambda x: isinstance(x, list)):
group_list = list(group)

if key_with_modifier:
key_codes.extend(group_list)

else:
key_codes.append(group_list)

return key_codes

except KeyError as error:
raise UnknownTypableException from error

0 comments on commit 7ce25af

Please sign in to comment.