added property_adder mixin. moved all "property" attributes of all classes to a Meta class. moved attribute setting code to LastfmBase.
parent
03c5cb0046
commit
b30839a26f
@ -0,0 +1,43 @@ |
||||
#!/usr/bin/env python |
||||
|
||||
__author__ = "Abhinav Sarkar <abhinav@abhinavsarkar.net>" |
||||
__version__ = "0.2" |
||||
__license__ = "GNU Lesser General Public License" |
||||
__package__ = "lastfm.mixin" |
||||
|
||||
def property_adder(cls): |
||||
for p in cls.Meta.properties: |
||||
if not hasattr(cls, p): |
||||
def wrapper(): |
||||
q = p |
||||
@property |
||||
def get(self): |
||||
try: |
||||
return getattr(self, "_{0}".format(q)) |
||||
except AttributeError: |
||||
return None |
||||
return get |
||||
setattr(cls, p, wrapper()) |
||||
|
||||
if hasattr(cls.Meta, 'fillable_properties'): |
||||
for p in cls.Meta.fillable_properties: |
||||
if not hasattr(cls, p): |
||||
def wrapper(): |
||||
q = p |
||||
@property |
||||
def get(self): |
||||
fill = False |
||||
try: |
||||
attrval = getattr(self, "_{0}".format(q)) |
||||
if attrval is None: |
||||
fill = True |
||||
else: |
||||
return attrval |
||||
except AttributeError: |
||||
fill = True |
||||
if fill: |
||||
self._fill_info() |
||||
return getattr(self, "_{0}".format(q)) |
||||
return get |
||||
setattr(cls, p, wrapper()) |
||||
return cls |