summaryrefslogtreecommitdiff
path: root/build/scripts/common/frozendict.py
blob: 6a921ec9df90e9d0bc4821cbf3d19c03f4f29792 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Implements a frozen dictionary-like object"""

import collections
import copy

import common.memo as memo

class frozendict(collections.Mapping):
  """A frozen dictionary class"""

  def __init__(self, *args, **kwargs):
    self._data = dict(*args, **kwargs)

  def __iter__(self):
    return iter(self._data)

  def __len__(self):
    return len(self._data)

  def __getitem__(self, key):
    return self._data[key]

  @memo.memo_i()
  def __hash__(self):
    return hash(self.itemtuple())

  def __str__(self):
    return str(self._data)

  def __repr__(self):
    return '%s(%s)' % (type(self).__name__, str(self))

  def __eq__(self, other):
    return self._data == other

  def __ne__(self, other):
    return not self == other

  def __deepcopy__(self, _memo):
    return copy.deepcopy(self._data)

  @memo.memo_i()
  def itemtuple(self):
    return tuple(sorted(self.iteritems()))

  def mutableDict(self):
    """
    Returns a mutable dictionary copy, replacing 'frozendict' with 'dict's.

    This function uses the 'copy.deepcopy' method to create a mutable deep copy
    of the dictionary.

    Note that due to the one-size-fits-all behavior of 'deepcopy', the result
    can be anything from heavyhanded to incorrect depending on the contents of
    the dictionary. The caller should make sure they understand the operation
    and its behavior on all of the dictionary's subtypes before using it.

    Returns: (dict) A mutable clone of the dictionary and its members.
    """
    return copy.deepcopy(self)

  def extend(self, **kwargs):
    """Returns a copy of this object with the 'kwargs' fields updated."""
    ndata = self.mutableDict()
    ndata.update(kwargs)
    return type(self)(**ndata)