# Copyright (c) 2012, Tycho Andersen. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


class Configurable(object):
    global_defaults = {}

    def __init__(self, **config):
        self._widget_defaults = {}
        self._user_config = config

    def add_defaults(self, defaults):
        """
            Add defaults to this object, overwriting any which already exist.
        """
        self._widget_defaults.update({d[0]: d[1] for d in defaults})

    def __getattr__(self, name):
        defaults = self._widget_defaults.copy()
        defaults.update(self.global_defaults)
        defaults.update(self._user_config)

        if name in defaults.iterkeys():
            setattr(self, name, defaults[name])
            return getattr(self, name)
        else:
            raise AttributeError("no attribute: %s" % name)


class ExtraFallback(object):
    """
        Adds another layer of fallback to attributes - to look up
        a different attribute name
    """

    def __init__(self, name, fallback):
        self.name = name
        self.fallback = fallback

    def __get__(self, instance, owner=None):
        try:
            retval = instance.__dict__[self.name]
        except KeyError:
            retval = Configurable.__getattr__(instance, self.fallback)
            setattr(instance, self.name, retval)

        return retval

@@ -1509,24 +1509,309 @@
 elf, name):%0A
+        found, value = self._find_default(name)%0A        if found:%0A            setattr(self, name, value)%0A            return value%0A        else:%0A            raise AttributeError(%22no attribute: %25s%22 %25 name)%0A%0A    def _find_default(self, name):%0A        %22%22%22Returns a tuple (found, value)%22%22%22%0A
         defa
@@ -1935,17 +1935,16 @@
 config)%0A
-%0A
         
@@ -1966,19 +1966,8 @@
 ults
-.iterkeys()
 :%0A  
@@ -1976,33 +1976,27 @@
         
-setattr(self, nam
+return (Tru
 e, defau
@@ -2018,110 +2018,44 @@
     
-    return getattr(self, name)%0A        else:%0A            raise AttributeError(%22no attribute: %25s%22 %25 nam
+else:%0A            return (False, Non
 e)%0A%0A
@@ -2263,24 +2263,67 @@
 name = name%0A
+        self.hidden_attribute = %22_%22 + name%0A
         self
@@ -2401,11 +2401,86 @@
     
-try
+retval = getattr(instance, self.hidden_attribute, None)%0A%0A        if not retval
 :%0A  
@@ -2480,32 +2480,40 @@
 val:%0A           
+ _found,
  retval = instan
@@ -2510,26 +2510,45 @@
 l = 
-instance.__dict__%5B
+Configurable._find_default(instance, 
 self
@@ -2552,17 +2552,18 @@
 elf.name
-%5D
+)%0A
 %0A       
@@ -2567,23 +2567,21 @@
     
-except KeyError
+if not retval
 :%0A  
@@ -2603,32 +2603,15 @@
 l = 
-Configurable.__
 getattr
-__
 (ins
@@ -2630,17 +2630,24 @@
 fallback
-)
+, None)%0A
 %0A       
@@ -2651,49 +2651,126 @@
     
-    setattr(instance, self.name, retval)%0A
+return retval%0A%0A    def __set__(self, instance, value):%0A        %22%22%22Set own value to a hidden attribute of the object%22%22%22
 %0A   
@@ -2766,30 +2766,64 @@
 ject%22%22%22%0A        
-return retval
+setattr(instance, self.hidden_attribute, value)
 %0A

Fix ExtraFallback regression in configurable