Robot Framework Integrated Development Environment (RIDE)
normalizing.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 try:
17  from collections import MutableMapping
18 except ImportError:
19  from collections.abc import MutableMapping # Python 3.10
20 
21 from .platform import IRONPYTHON, PY_VERSION, PY3
22 from .robottypes import is_dict_like, is_unicode
23 
24 
25 
30 def normalize(string, ignore=(), caseless=True, spaceless=True):
31  empty = u'' if is_unicode(string) else b''
32  if PY3 and isinstance(ignore, bytes):
33  # Iterating bytes in Python3 yields integers.
34  ignore = [bytes([i]) for i in ignore]
35  if spaceless:
36  string = empty.join(string.split())
37  if caseless:
38  string = lower(string)
39  ignore = [lower(i) for i in ignore]
40  # both if statements below enhance performance a little
41  if ignore:
42  for ign in ignore:
43  if ign in string:
44  string = string.replace(ign, empty)
45  return string
46 
47 
48 # http://ironpython.codeplex.com/workitem/33133
49 if IRONPYTHON and PY_VERSION < (2, 7, 5):
50  def lower(string):
51  return ('A' + string).lower()[1:]
52 else:
53  def lower(string):
54  return string.lower()
55 
56 
57 
58 class NormalizedDict(MutableMapping):
59 
60 
68  def __init__(self, initial=None, ignore=(), caseless=True, spaceless=True):
69  self._data_data = {}
70  self._keys_keys = {}
71  self._normalize_normalize = lambda s: normalize(s, ignore, caseless, spaceless)
72  if initial:
73  self._add_initial_add_initial(initial)
74 
75  def _add_initial(self, initial):
76  items = initial.items() if hasattr(initial, 'items') else initial
77  for key, value in items:
78  self[key] = value
79 
80  def __getitem__(self, key):
81  return self._data_data[self._normalize_normalize(key)]
82 
83  def __setitem__(self, key, value):
84  norm_key = self._normalize_normalize(key)
85  self._data_data[norm_key] = value
86  self._keys_keys.setdefault(norm_key, key)
87 
88  def __delitem__(self, key):
89  norm_key = self._normalize_normalize(key)
90  del self._data_data[norm_key]
91  del self._keys_keys[norm_key]
92 
93  def __iter__(self):
94  return (self._keys_keys[norm_key] for norm_key in sorted(self._keys_keys))
95 
96  def __len__(self):
97  return len(self._data_data)
98 
99  def __str__(self):
100  return '{%s}' % ', '.join('%r: %r' % (key, self[key]) for key in self)
101 
102  def __eq__(self, other):
103  if not is_dict_like(other):
104  return False
105  if not isinstance(other, NormalizedDict):
106  other = NormalizedDict(other)
107  return self._data_data == other._data
108 
109  def __ne__(self, other):
110  return not self == other
111 
112  def copy(self):
113  copy = NormalizedDict()
114  copy._data = self._data_data.copy()
115  copy._keys = self._keys_keys.copy()
116  copy._normalize = self._normalize_normalize
117  return copy
118 
119  # Speed-ups. Following methods are faster than default implementations.
120 
121  def __contains__(self, key):
122  return self._normalize_normalize(key) in self._data_data
123 
124  def clear(self):
125  self._data_data.clear()
126  self._keys_keys.clear()
Custom dictionary implementation automatically normalizing keys.
Definition: normalizing.py:58
def __init__(self, initial=None, ignore=(), caseless=True, spaceless=True)
Initialized with possible initial value and normalizing spec.
Definition: normalizing.py:68
def normalize(string, ignore=(), caseless=True, spaceless=True)
Normalizes given string according to given spec.
Definition: normalizing.py:30