summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Holland <william.holland@codethink.co.uk>2015-05-30 19:19:18 +0100
committerWilliam Holland <william.holland@codethink.co.uk>2015-05-30 19:19:18 +0100
commit08fe514529e73c1a20e870de65bf76096a008be0 (patch)
treedd4e25f6ba6175ae1dd4bba3aa44dc3eb0c2cad0
parentbf1b8101a3060d2d342e2ff2bf7cfa0a58fc1b56 (diff)
downloadkeyboard-python-08fe514529e73c1a20e870de65bf76096a008be0.tar.bz2
Move repeated regex to function
-rw-r--r--keyboardpython/key.py45
1 files changed, 21 insertions, 24 deletions
diff --git a/keyboardpython/key.py b/keyboardpython/key.py
index 09d8007..aebc7d5 100644
--- a/keyboardpython/key.py
+++ b/keyboardpython/key.py
@@ -174,6 +174,17 @@ def _get_ctrl_alpha(code):
alpha_key = Key(letter)
return KeyCombination(alpha_key,CTRL)
+def match_start_in_table(_code,table):
+ import re
+ code_str = ''.join(_code)
+ for c in table.keys():
+ match = re.search('^%s' % c,code_str)
+ if match:
+ key_comb = table[match.string]
+ len_match = len(match.string)
+ _code = _code[len_match:]
+ return _code,key_comb
+
def parse_code(code):
''' get Key object from code '''
_assert_type(code,str)
@@ -185,30 +196,16 @@ def parse_code(code):
if not _code:
key = _special_chars[ESC]
to_return.add_key(key)
- elif _code[0] == '[':
- import re
- _code.pop(0)
- code_str = ''.join(_code)
- for c in _escape_brace_codes.keys():
- match = re.search('^%s' % c,code_str)
- if match:
- key_comb = _escape_brace_codes[match.string]
- to_return.add_keyCombination(key_comb)
- len_match = len(match.string)
- _code = _code[len_match:]
- break
- elif _code[0] == 'O':
- import re
- _code.pop(0)
- code_str = ''.join(_code)
- for c in _escape_O_codes.keys():
- match = re.search('^%s' % c,code_str)
- if match:
- key_comb = _escape_O_codes[match.string]
- to_return.add_keyCombination(key_comb)
- len_match = len(match.string)
- _code = _code[len_match:]
- break
+ break
+ char = _code.pop(0)
+ if char == '[':
+ _code, key_comb = match_start_in_table(_code,
+ _escape_brace_codes)
+ to_return.add_keyCombination(key_comb)
+ elif char == 'O':
+ _code, key_comb = match_start_in_table(_code,
+ _escape_O_codes)
+ to_return.add_keyCombination(key_comb)
else:
to_return.add_key(ALT)
elif char.isalnum():