Forgot to add join.js and join_view.html to the git
[unfold.git] / manifold / util / frozendict.py
1 import copy
2
3 class frozendict(dict):
4     def _blocked_attribute(obj):
5         raise AttributeError, "A frozendict cannot be modified."
6     _blocked_attribute = property(_blocked_attribute)
7
8     __delitem__ = __setitem__ = clear = _blocked_attribute
9     pop = popitem = setdefault = update = _blocked_attribute
10
11     def __new__(cls, *args, **kw):
12         new = dict.__new__(cls)
13
14         args_ = []
15         for arg in args:
16             if isinstance(arg, dict):
17                 arg = copy.copy(arg)
18                 for k, v in arg.items():
19                     if isinstance(v, dict):
20                         arg[k] = frozendict(v)
21                     elif isinstance(v, list):
22                         v_ = list()
23                         for elm in v:
24                             if isinstance(elm, dict):
25                                 v_.append( frozendict(elm) )
26                             else:
27                                 v_.append( elm )
28                         arg[k] = tuple(v_)
29                 args_.append( arg )
30             else:
31                 args_.append( arg )
32
33         dict.__init__(new, *args_, **kw)
34         return new
35
36     def __init__(self, *args, **kw):
37         pass
38
39     def __hash__(self):
40         try:
41             return self._cached_hash
42         except AttributeError:
43             h = self._cached_hash = hash(tuple(sorted(self.items())))
44             return h
45
46     def __repr__(self):
47         return "frozendict(%s)" % dict.__repr__(self)