-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move
convert_to_key_code
to utils/
- Loading branch information
1 parent
bcc3f4e
commit 7ce25af
Showing
3 changed files
with
39 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |