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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/addon-sdk/source/python-lib/cuddlefish/bunch.py	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,34 @@
     1.4 +# This Source Code Form is subject to the terms of the Mozilla Public
     1.5 +# License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 +# file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.7 +
     1.8 +# Taken from Paver's paver.options module.
     1.9 +
    1.10 +class Bunch(dict):
    1.11 +    """A dictionary that provides attribute-style access."""
    1.12 +
    1.13 +    def __repr__(self):
    1.14 +        keys = self.keys()
    1.15 +        keys.sort()
    1.16 +        args = ', '.join(['%s=%r' % (key, self[key]) for key in keys])
    1.17 +        return '%s(%s)' % (self.__class__.__name__, args)
    1.18 +    
    1.19 +    def __getitem__(self, key):
    1.20 +        item = dict.__getitem__(self, key)
    1.21 +        if callable(item):
    1.22 +            return item()
    1.23 +        return item
    1.24 +
    1.25 +    def __getattr__(self, name):
    1.26 +        try:
    1.27 +            return self[name]
    1.28 +        except KeyError:
    1.29 +            raise AttributeError(name)
    1.30 +
    1.31 +    __setattr__ = dict.__setitem__
    1.32 +
    1.33 +    def __delattr__(self, name):
    1.34 +        try:
    1.35 +            del self[name]
    1.36 +        except KeyError:
    1.37 +            raise AttributeError(name)

mercurial