Mercurial > treepkg
changeset 40:c544903eeced
Add real enums for status information
author | Bernhard Herzog <bh@intevation.de> |
---|---|
date | Thu, 15 Mar 2007 21:03:44 +0100 |
parents | 17756cdce844 |
children | f7ec40638a06 |
files | test/test_status.py treepkg/status.py |
diffstat | 2 files changed, 64 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/test/test_status.py Thu Mar 15 13:00:01 2007 +0100 +++ b/test/test_status.py Thu Mar 15 21:03:44 2007 +0100 @@ -11,7 +11,7 @@ import unittest from datetime import datetime -from treepkg.status import RevisionStatus +from treepkg.status import RevisionStatus, Status, EnumFieldDesc from treepkg.util import ensure_directory, writefile @@ -62,3 +62,18 @@ writefile(self.filename, "Some other magic\nstart: 2007-03-09 17:32:55\n") self.assertRaises(ValueError, RevisionStatus, self.filename) + + def test_status_enum(self): + + class TestStatus(Status): + status = EnumFieldDesc() + status.add("not_running", "Process is not running", + default=True) + status.add("running", "Process is running") + status.add("error", "An error occurred") + + status = TestStatus(self.filename) + status.running() + + otherstatus = TestStatus(self.filename) + self.assertEquals(otherstatus.status.name, "running")
--- a/treepkg/status.py Thu Mar 15 13:00:01 2007 +0100 +++ b/treepkg/status.py Thu Mar 15 21:03:44 2007 +0100 @@ -24,6 +24,9 @@ def has_default(self): return self.default is not nodefault + def set_default(self, value): + self.default = value + def serialize(self, value): raise NotImplementedError @@ -51,16 +54,60 @@ return datetime.datetime(*time.strptime(string.strip(), self.date_format)[:6]) + +class EnumValue(object): + + def __init__(self, name, description, finished=False, error=False): + self.name = name + self.description = description + self.finished = finished + self.error = error + + +class EnumFieldDesc(FieldDesc): + + def __init__(self, *args, **kw): + super(EnumFieldDesc, self).__init__(*args, **kw) + self.values = {} + + def add(self, name, description, default=False, **kw): + enum = EnumValue(name, description, **kw) + self.values[enum.name] = enum + if default: + self.set_default(enum) + + def __iter__(self): + return self.values.itervalues() + + def serialize(self, value): + assert value.name is not None + return value.name + + def deserialize(self, string): + return self.values[string.strip()] + + +def make_setter(fieldname, enum): + def setter(self): + setattr(self, fieldname, enum) + setter.__name__ = enum.name + return setter + + class StatusMetaClass(type): def __new__(cls, name, bases, clsdict): # Generate the _fields class variable from the field descriptors - # in clsdict. + # in clsdict and remove the descriptors themselves. Also, add + # one setter method for each enum. fields = dict() for key, value in clsdict.items(): if isinstance(value, FieldDesc): fields[key] = value del clsdict[key] + if isinstance(value, EnumFieldDesc): + for enum in value: + clsdict[enum.name] = make_setter(key, enum) clsdict["_fields"] = fields return type.__new__(cls, name, bases, clsdict)