Skip to content

Commit

Permalink
Changed Configurable class
Browse files Browse the repository at this point in the history
It makes more sense to assign real attributes once
instead of searching for proper value every time attribute is requested
If given attribute has a setter then it will be used
  • Loading branch information
horsik authored and tych0 committed Mar 8, 2014
1 parent c2acfb9 commit c8dbc66
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions libqtile/configurable.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,19 @@ def add_defaults(self, defaults):
"""
Add defaults to this object, overwriting any which already exist.
"""
for (prop, value, _) in defaults:
self._widget_defaults[prop] = value
self._widget_defaults.update({d[0]: d[1] for d in defaults})

def __getattr__(self, name):
try:
return self._user_config[name]
except KeyError:
try:
return self.global_defaults[name]
except KeyError:
try:
return self._widget_defaults[name]
except KeyError:
raise AttributeError("no attribute: %s" % 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):
"""
Expand All @@ -57,10 +56,9 @@ def __init__(self, name, fallback):

def __get__(self, instance, owner=None):
try:
retval = Configurable.__getattr__(instance, self.name)
except AttributeError:
retval = None

if retval is None:
retval = instance.__dict__[self.name]
except KeyError:
retval = Configurable.__getattr__(instance, self.fallback)
setattr(instance, self.name, retval)

return retval

0 comments on commit c8dbc66

Please sign in to comment.