talonvoice / talon

Issue Tracker for the main Talon app
85 stars 0 forks source link

cheatsheat code no longer works due to keyerror involving insertion of '_5fs' into key string for commands dictionary #498

Closed tararoys closed 2 years ago

tararoys commented 2 years ago

The following bug made the linked previously working cheat-sheet generation code stop working:

>>> registry.contexts['user.knausj_talon.modes.modes.talon'].commands
{'___ladictation__mode_ra__': CommandImpl(Rule("dictation mode")), '___lacommand__mode_ra__': CommandImpl(Rule("command mode")), '___lamixed__mode_ra__': CommandImpl(Rule("mixed mode"))}

>>> registry.contexts['user.knausj_talon.modes.modes.talon'].commands['___ladictation__mode_ra__']
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "talon\scripting\context.py", line 100, in __getitem__
KeyError: '___5f_5f_5fladictation_5f_5fmode_5fra_5f_5f__'

The workaround for this error is to change line 97 on the cheatsheet code from

                commands= value.commands

to

                commands= dict(value.commands.items())

https://gist.github.com/tararoys/345deec8188b4613e24222e7b0616ac3#file-cheatsheet-py-L97

Which seems like an awkward fix.

Slack discussion here:

https://talonvoice.slack.com/archives/C9MHQ4AGP/p1647818929183129

lunixbochs commented 2 years ago

this is fixed in the next (unreleased) beta

one thing I would say, is you should use the following pattern, which also doesn't have this problem:

for key, value in dict.items():
    ...

rather than:

for key in dict:
    value = dict[key]

so write_context_commands would look like this:

def write_context_commands(file, commands): 
    # write out each command and it's implementation
    for key, command in commands.items():
        try:
            rule = command.rule.rule
            implementation = command.target.code.replace("\n","\n\t\t")
        except Exception:
            continue
        file.write("\n - **" + rule + "**  `" + implementation + "`\n")

as you're not even using the key, you can also write this:

def write_context_commands(file, commands): 
    # write out each command and it's implementation
    for command in commands.values():
        try:
            rule = command.rule.rule
            implementation = command.target.code.replace("\n","\n\t\t")
        except Exception:
            continue
        file.write("\n - **" + rule + "**  `" + implementation + "`\n")