diff options
author | William Holland <william.holland@codethink.co.uk> | 2015-05-29 08:38:53 +0100 |
---|---|---|
committer | William Holland <william.holland@codethink.co.uk> | 2015-05-29 08:38:53 +0100 |
commit | 6f84025af8bd84308c2872d6129ab3c1cfb96a2a (patch) | |
tree | 22dc17b4dd77ad0073686099e64a0c51ce422403 | |
parent | 0ba924f2c0b8d0b3b1ddd2bc887adf06145d41df (diff) | |
download | keyboard-python-6f84025af8bd84308c2872d6129ab3c1cfb96a2a.tar.bz2 |
Hide the innards of key.py
-rw-r--r-- | keyboardpython/__init__.py | 2 | ||||
-rw-r--r-- | keyboardpython/key.py | 26 |
2 files changed, 14 insertions, 14 deletions
diff --git a/keyboardpython/__init__.py b/keyboardpython/__init__.py index 216f08d..160a6b8 100644 --- a/keyboardpython/__init__.py +++ b/keyboardpython/__init__.py @@ -1,4 +1,4 @@ -import key +from key import * def set_to_read_key(fd='default'): ''' Set tty so that os.read gets one keypress at a time ''' diff --git a/keyboardpython/key.py b/keyboardpython/key.py index e6592ab..237d92a 100644 --- a/keyboardpython/key.py +++ b/keyboardpython/key.py @@ -1,6 +1,6 @@ ESC = '\x1b' -def assert_type(thing,_type): +def _assert_type(thing,_type): ''' if thing is not of type _type raise an exception ''' if type(thing) is not _type: raise(TypeError, @@ -11,7 +11,7 @@ class Key: ''' a keyboard keypress object ''' def __init__(self,name,*modifiers): - assert_type(name,str) + _assert_type(name,str) for m in modifiers: assert isinstance(m,Key), "Modifiers expected to be instances of Key" self.name = name @@ -49,7 +49,7 @@ class Key: ALT = Key('Alt') CTRL = Key('Control') -table = { +_table = { ESC : Key('Escape'), '\t': Key('Tab'), ' ': Key('Space'), @@ -79,29 +79,29 @@ table = { '%s[C' % ESC: Key('Right'), } -def is_alt(code): +def _is_alt(code): ''' is this the code of a combination modified by Alt? ''' - assert_type(code,str) + _assert_type(code,str) if len(code) == 2 and code[0] == ESC: return True return False -def is_ctrl_alpha(code): +def _is_ctrl_alpha(code): ''' is this the code of a combination modified by Ctrl? ''' - assert_type(code,str) + _assert_type(code,str) if code < '\x20': return True return False -def get_ctrl_alpha(code): +def _get_ctrl_alpha(code): ''' get alphabetic key modified by Ctrl ''' - assert_type(code,str) + _assert_type(code,str) letter = chr(ord(code) + 0x40) return Key(letter,CTRL) def parse_code(code): ''' get Key object from code ''' - assert_type(code,str) - if code in table.keys(): return table[code] - elif is_alt(code): return parse_code(code[1:]).modify(ALT) - elif is_ctrl_alpha(code): return get_ctrl_alpha(code) + _assert_type(code,str) + if code in _table.keys(): return _table[code] + elif _is_alt(code): return parse_code(code[1:]).modify(ALT) + elif _is_ctrl_alpha(code): return _get_ctrl_alpha(code) else: return Key(code) |