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

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     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/.
     5 # Taken from Paver's paver.options module.
     7 class Bunch(dict):
     8     """A dictionary that provides attribute-style access."""
    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)
    16     def __getitem__(self, key):
    17         item = dict.__getitem__(self, key)
    18         if callable(item):
    19             return item()
    20         return item
    22     def __getattr__(self, name):
    23         try:
    24             return self[name]
    25         except KeyError:
    26             raise AttributeError(name)
    28     __setattr__ = dict.__setitem__
    30     def __delattr__(self, name):
    31         try:
    32             del self[name]
    33         except KeyError:
    34             raise AttributeError(name)

mercurial