Skip to content

Commit

Permalink
fix Action.from_dict and test_action_from_dict to support <cmd>-t
Browse files Browse the repository at this point in the history
  • Loading branch information
abrichr committed Oct 9, 2024
1 parent 8808b15 commit 28a1ea3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 44 deletions.
52 changes: 26 additions & 26 deletions openadapt/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,40 +439,37 @@ def from_dict(
(ActionEvent) The ActionEvent.
"""
sep = config.ACTION_TEXT_SEP
name_prefix = config.ACTION_TEXT_NAME_PREFIX
name_suffix = config.ACTION_TEXT_NAME_SUFFIX
children = []
release_events = []
if "text" in action_dict:
# Splitting actions based on whether they are special keys or characters
if action_dict["text"].startswith(name_prefix) and action_dict[
"text"
].endswith(name_suffix):
# handle multiple key separators
# (each key separator must start and end with a prefix and suffix)
children = []
name_prefix = config.ACTION_TEXT_NAME_PREFIX
name_suffix = config.ACTION_TEXT_NAME_SUFFIX
text = action_dict["text"]

# Check if the text contains named keys (starting with the name prefix)
contains_named_keys = text.startswith(name_prefix) and text.endswith(name_suffix)

if contains_named_keys:
# Handle named keys, potentially with separator variations
release_events = []
default_sep = "".join([name_suffix, sep, name_prefix])
variation_seps = ["".join([name_suffix, name_prefix])]
key_seps = [default_sep]
if handle_separator_variations:
variation_seps = ["".join([name_suffix, name_prefix])]
key_seps += variation_seps

prefix_len = len(name_prefix)
suffix_len = len(name_suffix)

key_names = utils.split_by_separators(
action_dict.get("text", "")[prefix_len:-suffix_len],
text[prefix_len:-suffix_len],
key_seps,
)
canonical_key_names = utils.split_by_separators(
action_dict.get("canonical_text", "")[prefix_len:-suffix_len],
key_seps,
)
logger.info(f"{key_names=}")
logger.info(f"{canonical_key_names=}")

# Process each key name and canonical key name found
children = []
release_events = []
for key_name, canonical_key_name in zip_longest(
key_names,
canonical_key_names,
Expand All @@ -481,19 +478,22 @@ def from_dict(
key_name, canonical_key_name
)
children.append(press)
release_events.append(
release
) # Collect release events to append in reverse order later

release_events.append(release)
children += release_events[::-1]
else:
# Handling regular character sequences
sep_len = len(sep)
for key_char in action_dict["text"][:: sep_len + 1]:
# Press and release each character one after another
press, release = cls._create_key_events(key_char=key_char)
# Handle mixed sequences of named keys and regular characters
split_text = text.split(sep)
for part in split_text:
if part.startswith(name_prefix) and part.endswith(name_suffix):
# It's a named key
key_name = part[len(name_prefix):-len(name_suffix)]
press, release = cls._create_key_events(key_name=key_name)
else:
# It's a character
press, release = cls._create_key_events(key_char=part)
children.append(press)
children.append(release)
children += release_events[::-1]

rval = ActionEvent(**action_dict, children=children)
return rval

Expand Down
58 changes: 40 additions & 18 deletions tests/openadapt/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,44 @@

def test_action_from_dict() -> None:
"""Test ActionEvent.from_dict()."""
texts = [
# standard
"<backspace>-<backspace>-<backspace>",
# mal-formed
"<backspace><backspace><backspace>",
# mixed
"<backspace>-<backspace><backspace>",
"<backspace><backspace>-<backspace>",
]
input_variations_by_expected_output = {
## all named keys
"<backspace>-<backspace>-<backspace>": [
# standard
"<backspace>-<backspace>-<backspace>",
# mal-formed
"<backspace><backspace><backspace>",
# mixed
"<backspace>-<backspace><backspace>",
"<backspace><backspace>-<backspace>",
],
# TODO: support malformed configurations below
## all char keys
"a-b-c": [
# standard
"a-b-c",
# malformed
#"abc",
# mixed
#"a-bc",
#"ab-c",
],
## mixed named and char
"<cmd>-t": [
# standard
"<cmd>-t",
# malformed
#"<cmd>t",
],
}

for text in texts:
action_dict = {
"name": "type",
"text": text,
"canonical_text": text,
}
print(f"{text=}")
action_event = models.ActionEvent.from_dict(action_dict)
assert action_event.text == "<backspace>-<backspace>-<backspace>", action_event
for expected_output, input_variations in input_variations_by_expected_output.items():
for input_variation in input_variations:
action_dict = {
"name": "type",
"text": input_variation,
"canonical_text": input_variation,
}
print(f"{input_variation=}")
action_event = models.ActionEvent.from_dict(action_dict)
assert action_event.text == expected_output, action_event

0 comments on commit 28a1ea3

Please sign in to comment.