summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorWilliam Holland <william.holland@codethink.co.uk>2015-05-30 18:35:21 +0100
committerWilliam Holland <william.holland@codethink.co.uk>2015-05-30 18:35:21 +0100
commit3d45e42de2b4a8523dba29cb50b50feaa2941e0f (patch)
treeaa575b011de66c9073ded04504ee0dc2baef3f02
parentbb2a7f1bb7b689d68bd55a8c359b4a1f9f3de2ff (diff)
downloadkeyboard-python-3d45e42de2b4a8523dba29cb50b50feaa2941e0f.tar.bz2
Clean parse_key by using pop
Rather than lots of list indexing
-rw-r--r--keyboardpython/key.py58
1 files changed, 28 insertions, 30 deletions
diff --git a/keyboardpython/key.py b/keyboardpython/key.py
index 43ab98c..0b6c36e 100644
--- a/keyboardpython/key.py
+++ b/keyboardpython/key.py
@@ -164,59 +164,57 @@ def parse_code(code):
to_return = KeyCombination()
_code = list(code)
while _code:
- if _code[0] == ESC:
- if len(_code) == 1:
+ char = _code.pop(0)
+ if char == ESC:
+ if not _code:
key = _special_chars[ESC]
to_return.add_key(key)
- _code.pop(0)
- elif _code[1] == '[':
+ elif _code[0] == '[':
import re
- __code = ''.join(_code[2:])
+ _code.pop(0)
+ code_str = ''.join(_code)
for c in _escape_brace_codes.keys():
- match = re.search('^%s' % c,__code)
+ match = re.search('^%s' % c,code_str)
if match:
key = _escape_brace_codes[match.string]
to_return.add_key(key)
len_match = len(match.string)
- _code = _code[len_match+2:]
+ _code = _code[len_match:]
break
+ #TODO HACK - remove
else:
- ___code = re.sub(';3~','~',__code)
- match = re.search('^%s' % c,___code)
+ alt_code_str = re.sub(';3~','~',code_str)
+ match = re.search('^%s' % c,alt_code_str)
if match:
key = _escape_brace_codes[match.string]
to_return.add_key(key)
to_return.add_key(ALT)
len_match = len(match.string)
- _code = _code[len_match+4:]
+ _code = _code[len_match+2:]
break
-
- elif _code[1] == 'O':
- if _code[2] == '1':
- # alt F1 - alt F4
- key = _escape_O_codes[_code[5]]
+ elif _code[0] == 'O':
+ _code.pop(0)
+ char = _code.pop(0)
+ # TODO HACK - remove
+ if len(_code) > 1 and char == '1':
+ # alt F1 - alt F4, alt Del and alt Insert
+ key = _escape_O_codes[_code[3]]
to_return.add_key(key)
to_return.add_key(ALT)
- _code = _code[6:]
+ _code = _code[4:]
else:
- key = _escape_O_codes[_code[2]]
+ key = _escape_O_codes[char]
to_return.add_key(key)
- _code = _code[3:]
else:
to_return.add_key(ALT)
- _code.pop(0)
- elif _code[0].isalnum():
- to_return.add_key(Key(_code[0]))
- _code.pop(0)
- elif _is_ctrl_alpha(_code[0]):
- key_comb = _get_ctrl_alpha(_code[0])
+ elif char.isalnum():
+ to_return.add_key(Key(char))
+ elif _is_ctrl_alpha(char):
+ key_comb = _get_ctrl_alpha(char)
to_return.add_keyCombination(key_comb)
- _code.pop(0)
- elif _code[0] in _special_chars.keys():
- key = _special_chars[_code[0]]
+ elif char in _special_chars.keys():
+ key = _special_chars[char]
to_return.add_key(key)
- _code.pop(0)
else:
- to_return.add_key(Key(_code[0]))
- _code.pop(0)
+ to_return.add_key(Key(char))
return to_return