summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Holland <william.holland@codethink.co.uk>2015-06-07 13:27:15 +0100
committerWilliam Holland <william.holland@codethink.co.uk>2015-06-07 13:27:15 +0100
commitcc20e02878cf7482dec6a42392dff68616fddae8 (patch)
tree8430f4ec4f375df789e868ed0b2e8d142cbf3e2a
parente2c88ef0809376ecad840d41a7fb948bc1f18d58 (diff)
downloadkeyboard-python-cc20e02878cf7482dec6a42392dff68616fddae8.tar.bz2
Fix key_codes_table loading each time parser called
By loading it when key.py is imported
-rw-r--r--keyboardpython/key.py22
1 files changed, 11 insertions, 11 deletions
diff --git a/keyboardpython/key.py b/keyboardpython/key.py
index 1104945..eb315b5 100644
--- a/keyboardpython/key.py
+++ b/keyboardpython/key.py
@@ -1,5 +1,7 @@
ESC = '\x1b'
+import key_codes as _key_codes
+
def _assert_type(thing,_type):
''' if thing is not of type _type raise an exception '''
if type(thing) is not _type:
@@ -129,6 +131,8 @@ def _format_key_dict(in_dict):
to_return[code] = combination
return to_return
+_key_codes_table = _format_key_dict(_key_codes.key_codes)
+
def _is_alt(code):
''' is this the code of a combination modified by Alt? '''
_assert_type(code,str)
@@ -150,7 +154,7 @@ def _get_ctrl_alpha(code):
alpha_key = Key(letter)
return KeyCombination(alpha_key,CTRL)
-def _match_start_in_table(_code,table):
+def _match_start_in_table(_code):
''' look for match at start of _code in table,
if a match if found remove it from _code
returns _code,key_comb where key_comb is
@@ -159,12 +163,12 @@ def _match_start_in_table(_code,table):
'''
import re
code_str = ''.join(_code)
- for c in table:
+ for c in _key_codes_table:
c = c.replace('[','\[')
c = c.replace('^','\^')
match = re.search('^%s' % c,code_str)
if match:
- key_comb = table.get(match.string)
+ key_comb = _key_codes_table.get(match.string)
len_match = len(match.string)
_code = _code[len_match:]
return _code,key_comb
@@ -173,29 +177,25 @@ def _match_start_in_table(_code,table):
def parse_code(code):
''' get KeyCombination object from code '''
_assert_type(code,str)
- import key_codes
- key_codes_table = _format_key_dict(key_codes.key_codes)
to_return = KeyCombination()
_code = list(code)
while _code:
char = _code.pop(0)
if char == ESC:
if not _code:
- return key_codes_table[ESC]
+ return _key_codes_table[ESC]
char = _code.pop(0)
if char == '[':
- _code, key_comb = _match_start_in_table(['^[','[']+_code,
- key_codes_table)
+ _code, key_comb = _match_start_in_table(['^[','[']+_code)
if not key_comb: key_comb = KeyCombination('[',ALT)
elif char == 'O':
- _code, key_comb = _match_start_in_table(['^[','O']+_code,
- key_codes_table)
+ _code, key_comb = _match_start_in_table(['^[','O']+_code)
if not key_comb: key_comb = KeyCombination('O',ALT)
else: key_comb = KeyCombination(char,ALT)
elif _is_ctrl_alpha(char): key_comb = _get_ctrl_alpha(char)
else:
try:
- key_comb = key_codes_table[char]
+ key_comb = _key_codes_table[char]
except:
key_comb = KeyCombination(char)
to_return.add_keyCombination(key_comb)