Robot Framework Integrated Development Environment (RIDE)
dotdict.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 collections import OrderedDict
17 
18 from .robottypes import is_dict_like
19 
20 
21 class DotDict(OrderedDict):
22 
23  def __init__(self, *args, **kwds):
24  args = [self._convert_nested_initial_dicts_convert_nested_initial_dicts(a) for a in args]
25  kwds = self._convert_nested_initial_dicts_convert_nested_initial_dicts(kwds)
26  OrderedDict.__init__(self, *args, **kwds)
27 
29  items = value.items() if is_dict_like(value) else value
30  return OrderedDict((key, self._convert_nested_dicts_convert_nested_dicts(value))
31  for key, value in items)
32 
33  def _convert_nested_dicts(self, value):
34  if isinstance(value, DotDict):
35  return value
36  if is_dict_like(value):
37  return DotDict(value)
38  if isinstance(value, list):
39  value[:] = [self._convert_nested_dicts_convert_nested_dicts(item) for item in value]
40  return value
41 
42  def __getattr__(self, key):
43  try:
44  return self[key]
45  except KeyError:
46  raise AttributeError(key)
47 
48  def __setattr__(self, key, value):
49  if not key.startswith('_OrderedDict__'):
50  self[key] = value
51  else:
52  OrderedDict.__setattr__(self, key, value)
53 
54  def __delattr__(self, key):
55  try:
56  self.pop(key)
57  except KeyError:
58  OrderedDict.__delattr__(self, key)
59 
60  def __eq__(self, other):
61  return dict.__eq__(self, other)
62 
63  def __ne__(self, other):
64  return not self == other
65 
66  def __str__(self):
67  return '{%s}' % ', '.join('%r: %r' % (key, self[key]) for key in self)
68 
69  # Must use original dict.__repr__ to allow customising PrettyPrinter.
70  __repr__ = dict.__repr__
def _convert_nested_initial_dicts(self, value)
Definition: dotdict.py:28
def __setattr__(self, key, value)
Definition: dotdict.py:48
def __init__(self, *args, **kwds)
Definition: dotdict.py:23
def _convert_nested_dicts(self, value)
Definition: dotdict.py:33