addon-sdk/source/python-lib/cuddlefish/bunch.py

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:08ca6df463c2
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5 # Taken from Paver's paver.options module.
6
7 class Bunch(dict):
8 """A dictionary that provides attribute-style access."""
9
10 def __repr__(self):
11 keys = self.keys()
12 keys.sort()
13 args = ', '.join(['%s=%r' % (key, self[key]) for key in keys])
14 return '%s(%s)' % (self.__class__.__name__, args)
15
16 def __getitem__(self, key):
17 item = dict.__getitem__(self, key)
18 if callable(item):
19 return item()
20 return item
21
22 def __getattr__(self, name):
23 try:
24 return self[name]
25 except KeyError:
26 raise AttributeError(name)
27
28 __setattr__ = dict.__setitem__
29
30 def __delattr__(self, name):
31 try:
32 del self[name]
33 except KeyError:
34 raise AttributeError(name)

mercurial