Mercurial > treepkg
changeset 39:17756cdce844
Make defining a status class easier with a meta class
author | Bernhard Herzog <bh@intevation.de> |
---|---|
date | Thu, 15 Mar 2007 13:00:01 +0100 |
parents | dd88230dd762 |
children | c544903eeced |
files | treepkg/status.py |
diffstat | 1 files changed, 19 insertions(+), 8 deletions(-) [+] |
line wrap: on
line diff
--- a/treepkg/status.py Thu Mar 15 12:20:12 2007 +0100 +++ b/treepkg/status.py Thu Mar 15 13:00:01 2007 +0100 @@ -51,8 +51,24 @@ return datetime.datetime(*time.strptime(string.strip(), self.date_format)[:6]) +class StatusMetaClass(type): + + def __new__(cls, name, bases, clsdict): + # Generate the _fields class variable from the field descriptors + # in clsdict. + fields = dict() + for key, value in clsdict.items(): + if isinstance(value, FieldDesc): + fields[key] = value + del clsdict[key] + clsdict["_fields"] = fields + return type.__new__(cls, name, bases, clsdict) + + class Status(object): + __metaclass__ = StatusMetaClass + # Overwrite in derived classes with a different magic string _magic = "Status 0.0\n" @@ -60,10 +76,6 @@ # variables. _attrs = set(["_filename", "_values"]) - # Derived classes should set this to a suitable list of attribute - # descriptors - _fields = () - def __init__(self, filename): assert os.path.isabs(filename) self._filename = filename @@ -118,7 +130,6 @@ _magic = "TreePackagerStatus 0.0\n" - _fields = dict(status=StringFieldDesc(default="unknown"), - start=DateFieldDesc(default=None), - stop=DateFieldDesc(default=None)) - + status = StringFieldDesc(default="unknown") + start = DateFieldDesc(default=None) + stop = DateFieldDesc(default=None)