def test_plays_nice_with_dict(): d = { from_string('C-f'): 'foo', from_string('M-f'): 'bar', from_string('f'): 'baz' } assert from_string('C-f') in d assert d[from_string('M-f')] == 'bar'
def configure(factory): """Set the right reactor up and get the GUI going.""" from twisted.internet import gtk2reactor gtk2reactor.install() gui = GUI(factory.realm) macros = {from_string("<page up>"): gui.forward_page_up_cb, from_string('<page down>'): gui.forward_page_down_cb, from_string("C-c"): gui.maybe_forward_copy_cb} factory.realm.macros.update(macros) factory.realm.baked_in_macros.update(macros)
def test_rejects_blank_key(): try: res = from_string('') except CantParseThatError: pass else: assert False
def test_rejects_blank_key_with_modifier(): try: res = from_string('M-') except CantParseThatError: pass else: assert False
def test_blows_up_on_unexpected_sequence_of_characters(): try: res = from_string('foo') except CantParseThatError: pass else: assert False, res
def test_modifiers_are_case_sensitive(): try: res = from_string('m-f') except InvalidModifiersError: pass else: assert False
def test_special_keys_go_through_alright(): allowed_specials = ["backspace", "tab", "return", "enter", "dash", "pause", "escape", "page up", "page down", "end", "home", "left", "up", "right", "down", "insert", "delete"] + \ ['f%d' % n for n in range(1, 13)] + \ ['numpad %s' % n for n in range(0, 10) + ['add', 'divide', 'multiply', 'subtract']] for special in allowed_specials: yield single_keychord_key_equal, special, from_string('<%s>' % special)
def test_rejects_unknown_modifier(): try: res = from_string('x-f') except InvalidModifiersError: pass else: assert False
def test_blows_up_on_unclosed_special_key(): try: res = from_string('<escape') except CantParseThatError: pass else: assert False, res
def test_rejects_dash_on_its_own(): try: res = from_string('-') except (InvalidModifiersError, CantParseThatError): pass else: assert False
def test_invalid_special_keys_raises_errors(): invalid_specials = ['foo', 'bar', 'baz'] for special in invalid_specials: try: v = from_string('<%s>') except InvalidSpecialKeyError: pass else: assert False, v
def macros(self): return { from_string('<F1>'): self.key1, from_string('<F2>'): self.key2, from_string('<F3>'): self.key3, from_string('<F4>'): self.key4, from_string('<F5>'): self.key5, from_string('C-<cyrillic_a>'): self.change_attack, from_string('C-<cyrillic_be>'): self.change_auto_agr, }
"""The predefined keybindings for the GUI.""" from mudpyl.gui.keychords import from_string gui_macros = {} def enter_pressed(realm): """Submit the line to the realm.""" realm.factory.gui.command_line.submit_line() gui_macros[from_string('<Enter>')] = enter_pressed gui_macros[from_string('<Return>')] = enter_pressed def escape_pressed(realm): """Escape's been pressed. Store the current line in the history, and then clear the entry area. """ realm.factory.gui.command_line.escape_pressed() gui_macros[from_string('<Escape>')] = escape_pressed def tab_pressed(realm): """Complete the current word in the buffer.""" realm.factory.gui.command_line.tab_complete() gui_macros[from_string('<Tab>')] = tab_pressed def up_pressed(realm): """Go back one command in the history.""" realm.factory.gui.command_line.history_up() gui_macros[from_string('<Up>')] = up_pressed
def test_alt_key_flag_is_default_false(): assert not from_string('f').meta
def test_alt_key_setting_to_true(): assert from_string('M-f').meta
def test_alt_and_control_work_together(): c = from_string('C-M-f') assert c.meta and c.control
def test_normal_key_case_insensitive(): assert from_string('F').key == 'f'
def test_normal_key_setting(): assert from_string('f').key == 'f'
def test_ordering_of_modifiers_doesnt_matter(): c1 = from_string('C-M-q') c2 = from_string('M-C-q') assert c1 == c2
def test_equality(): #XXX: other interesting comparison cases kc1 = from_string('f') kc2 = from_string('f') assert kc1 == kc2, (kc1, kc2)
def test_case_insensitivity_in_special_keys(): vals = ['escape', 'EsCaPe', 'esCApe', 'ESCAPE'] for val in vals: yield single_keychord_key_equal, 'escape', from_string('<%s>' % val)
def test_control_key_flag_is_default_false(): assert not from_string('f').control
def trace_toggle(realm): """Turn tracing on or off, depending on its current state.""" if not realm.tracing: realm.trace_on() else: realm.trace_off() def keypad_move(direction): """A wrapper to stop me having loads of different direction-moving functions. """ def walker(realm): """Actually send the direction.""" realm.send(direction) return walker #pylint: enable-msg= W0613 keypad_directions = {from_string('<numpad 7>'): keypad_move('nw'), from_string('<numpad 8>'): keypad_move('n'), from_string('<numpad 9>'): keypad_move('ne'), from_string('<numpad 4>'): keypad_move('w'), from_string('<numpad 6>'): keypad_move('e'), from_string('<numpad 1>'): keypad_move('sw'), from_string('<numpad 2>'): keypad_move('s'), from_string('<numpad 3>'): keypad_move('se'), from_string('<numpad divide>'): keypad_move('in'), from_string('<numpad multiply>'): keypad_move('out'), from_string('<numpad subtract>'): keypad_move('up'), from_string('<numpad add>'): keypad_move('down')}
def test_inequality(): assert from_string('m') != from_string('r')
def test_KeyChord_unicode_and_ascii_characters_are_the_same(): kc1 = from_string('f') kc2 = from_string(u'f') assert kc1 == kc2
def test_equal_chords_hash_the_same(): assert hash(from_string('f')) == hash(from_string('f'))
def test_sets_Control_key_to_true(): assert from_string('C-f').control
class KeysSystem(BaseModule): keypad_directions = { from_string('<numpad 7>'): keypad_move('up'), from_string('<numpad 8>'): keypad_move('n'), from_string('<numpad 4>'): keypad_move('w'), from_string('<numpad 6>'): keypad_move('e'), from_string('<numpad 1>'): keypad_move('down'), from_string('<numpad 2>'): keypad_move('s'), } keypad_look = { from_string('<numpad 5>'): keypad('look \n огл'), # Побеги и рекол from_string('<numpad add>'): keypad('~\nбежать'), from_string('C-1'): keypad('~'), from_string('C-2'): keypad('~\n зачит возв \n вз возвр сунд1 \n дер офф'), from_string('C-3'): keypad('~\n вз возвр сунд1 \n зачит возв \n вз возвр сунд1 \n дер офф'), # Лутим трупы from_string('<numpad subtract>'): keypad('вз все.труп \n вз все все.труп \n бр все.труп'), from_string('<numpad 0>'): keypad('подн'), # Автосник ы from_string(u'C-<cyrillic_yeru>'): toggle_auto_snik(), } keys = merge_dicts(keypad_directions, keypad_look) def __init__(self, factory): BaseModule.__init__(self, factory) @property def macros(self): return self.keys
def macros(self): return { from_string('C-<cyrillic_ie>'): self.do_trig, }