Skip to content

Instantly share code, notes, and snippets.

@poppe1219
Created June 29, 2014 14:38
Show Gist options
  • Save poppe1219/8953fa40d20ddfef3f3d to your computer and use it in GitHub Desktop.
Save poppe1219/8953fa40d20ddfef3f3d to your computer and use it in GitHub Desktop.
Example of how custom dragonfly commands could be configured
class Key:
"""Mock class."""
def __init__(self, phrase):
self._phrase = phrase
class Function:
""" Mock class."""
def __init__(self, phrase):
self._phrase = phrase
def snake_case_count():
"""Mock method."""
pass
# ------------------------------------
defaults = {
'up [<n>]': Key("up:%(n)d"),
'down [<n>]': Key("down:%(n)d"),
'enter [<n>]': Key("enter:%(n)d"),
'snake case <n> [words]': Function(snake_case_count),
'parens': Key("lparen, rparen, left/3"),
"tab [<n>]": Key("tab:%(n)d"),
"delete [<n>]": Key("del/3:%(n)d")
}
custom_phrases = {
'up [<n>]': 'blipp [<n>]',
'down [<n>]': 'blorp [<n>]',
'enter [<n>]': 'slap [<n>]',
'snake case <n> [words]': 'underscore <n> [words]',
'parens': None # Disables the command.
}
def apply_custom_phrases(defaults, custom_phrases):
for key, new_key in custom_phrases.items():
if new_key is None:
del defaults[key]
continue
if not key in defaults.keys():
print('Warning, unknown phrase: "{}"'.format(key))
continue
defaults[new_key] = defaults[key]
del defaults[key]
if __name__ == '__main__':
apply_custom_phrases(defaults, custom_phrases)
print('{')
for key, value in defaults.items():
print(' "{}", "{}",'.format(key, str(value)))
print('}')
""" Produces:
{
"underscore <n> [words]", "<__main__.Function object at 0x7f85001e8dd8>",
"blipp [<n>]", "<__main__.Key object at 0x7f8500293668>",
"slap [<n>]", "<__main__.Key object at 0x7f85001e83c8>",
"tab [<n>]", "<__main__.Key object at 0x7f85001e8b70>",
"blorp [<n>]", "<__main__.Key object at 0x7f85001e8a90>",
"delete [<n>]", "<__main__.Key object at 0x7f85001e8e80>",
}
"""
@poppe1219
Copy link
Author

In other words: the keys of the default dictionary has been replaced by the values from custom_phrases. This allows the user to redefine default phrases and disable default commands. The values in the default dictionary remain unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment