Robot Framework Integrated Development Environment (RIDE)
shortcut.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from functools import total_ordering
17 
18 import wx
19 
20 from ..context import IS_MAC
21 
22 CMD_CHAR = u'\u2318'
23 SHIFT_CHAR = u'\u21E7'
24 OPTION_CHAR = u'\u2325'
25 CTRL_CHAR = u'\u2303'
26 SPACE_CHAR = u'\u2423'
27 LEFT_CHAR = u'\u2190'
28 RIGHT_CHAR = u'\u2192'
29 DEL_CHAR = u'\u2326'
30 ENTER_CHAR = u'\u2324'
31 RETURN_CHAR = u'\u21A9'
32 ESC_CHAR = u'\u238B'
33 UP_CHAR = u'\u2191'
34 DOWN_CHAR = u'\u2193'
35 
36 
39 _REPLACE = {
40  'Cmd': CMD_CHAR,
41  'Shift': SHIFT_CHAR,
42  'Alt': OPTION_CHAR,
43  'Ctrl': CTRL_CHAR,
44  'Space': SPACE_CHAR,
45  'Left': LEFT_CHAR,
46  'Right': RIGHT_CHAR,
47  'Delete': DEL_CHAR,
48  'Enter': ENTER_CHAR,
49  'Return': RETURN_CHAR,
50  'Escape': ESC_CHAR,
51  '-': '',
52  'Up': UP_CHAR,
53  'Down': DOWN_CHAR
54 }
55 
56 
57 def localize_shortcuts(string):
58  if IS_MAC:
59  string = string.replace('CtrlCmd', 'Cmd')
60  else:
61  string = string.replace('CtrlCmd', 'Ctrl')
62  return _replace_mac_chars(string)
63 
64 
65 def _replace_mac_chars(string):
66  if not IS_MAC or not string:
67  return string
68  for key, value in _REPLACE.items():
69  string = string.replace(key, value)
70  return string
71 
72 
73 @total_ordering
74 class Shortcut():
75 
76  def __init__(self, shortcut):
77  self.valuevalue = self._normalize_normalize(shortcut)
78  self.printableprintable = self._get_printable_get_printable(self.valuevalue)
79 
80  def _get_printable(self, value):
81  return self._replace_chars_in_mac_replace_chars_in_mac(value)
82 
83  def _replace_chars_in_mac(self, shortcut):
84  return _replace_mac_chars(shortcut)
85 
86  def __nonzero__(self):
87  return bool(self.valuevalue)
88 
89  def _normalize(self, shortcut):
90  if not shortcut:
91  return None
92  order = ['Shift', 'Ctrl', 'Cmd', 'Alt']
93  keys = [self._normalize_key_normalize_key(key) for key in self._split_split(shortcut)]
94  try: # DEBUG only in python3 ??
95  keys.index('')
96  keys.pop()
97  return None
98  except ValueError:
99  pass
100  keys.sort(key=lambda t: t in order and order.index(t) or 42)
101  return '-'.join(keys)
102 
103  def _split(self, shortcut):
104  try:
105  m_str=shortcut.replace('+', '-').split('-')
106  # print("DEBUG: Shortcut %s" % m_str)
107  return m_str
108  except AttributeError: # DEBUG On python 3 there are NoneType
109  pass
110  return ''
111 
112  def _normalize_key(self, key):
113  key = key.title()
114  key = self._handle_ctrlcmd_handle_ctrlcmd(key)
115  return {'Del': 'Delete', 'Ins': 'Insert',
116  'Enter': 'Return', 'Esc':'Escape'}.get(key, key)
117 
118  def _handle_ctrlcmd(self, key):
119  if key != 'Ctrlcmd':
120  return key
121  if IS_MAC:
122  return 'Cmd'
123  return 'Ctrl'
124 
125  def parse(self):
126  keys = self._split_split(self.valuevalue)
127  if keys:
128  # print("DEBUG: parser %s" % keys)
129  if len(keys) == 1:
130  flags = wx.ACCEL_NORMAL
131  else:
132  flags = sum(self._get_wx_key_constant_get_wx_key_constant('ACCEL', key)
133  for key in keys[:-1])
134  return flags, self._get_key_get_key(keys[-1])
135 
136  def _get_wx_key_constant(self, prefix, name):
137  attr = '%s_%s' % (prefix, name.upper().replace(' ', ''))
138  try:
139  return getattr(wx, attr)
140  except AttributeError:
141  raise ValueError('Invalid shortcut key: %s' % name)
142 
143  def _get_key(self, key):
144  if len(key) == 1:
145  return ord(key.upper())
146  return self._get_wx_key_constant_get_wx_key_constant('WXK', self._normalize_key_normalize_key(key))
147 
148  def __eq__(self, other):
149  return self.name.lower() == other.name.lower()
150 
151  def __hash__(self):
152  return hash(repr(self))
153 
154  def __lt__(self, other):
155  return self.name.lower() < other.name.lower()
156 
157  def __repr__(self):
158  return self.printableprintable
def _replace_chars_in_mac(self, shortcut)
Definition: shortcut.py:83
def _normalize(self, shortcut)
Definition: shortcut.py:89
def _get_wx_key_constant(self, prefix, name)
Definition: shortcut.py:136
def _split(self, shortcut)
Definition: shortcut.py:103
def _get_printable(self, value)
Definition: shortcut.py:80
def __init__(self, shortcut)
Definition: shortcut.py:76
def localize_shortcuts(string)
Definition: shortcut.py:57
def _replace_mac_chars(string)
Definition: shortcut.py:65