From 8fe674922f7529837bda8886ca08aaff46469717 Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Tue, 20 Sep 2011 09:26:45 +0200 Subject: [PATCH] Rename AttrDef to wsattr, and introduce wsproperty --- wsme/tests/test_types.py | 23 +++++++++++++++++++++++ wsme/types.py | 23 ++++++++++++----------- 2 files changed, 35 insertions(+), 11 deletions(-) create mode 100644 wsme/tests/test_types.py diff --git a/wsme/tests/test_types.py b/wsme/tests/test_types.py new file mode 100644 index 0000000..9a46fd7 --- /dev/null +++ b/wsme/tests/test_types.py @@ -0,0 +1,23 @@ +import unittest +from wsme import types + +class TestTypes(unittest.TestCase): + def test_flat_type(self): + class Flat(object): + aint = int + astr = str + auni = unicode + afloat = float + + types.register_type(Flat) + + assert len(Flat._wsme_attributes) == 4 + + def test_private_attr(self): + class WithPrivateAttrs(object): + _private = 12 + + types.register_type(WithPrivateAttrs) + + assert len(WithPrivateAttrs._wsme_attributes) == 0 + diff --git a/wsme/types.py b/wsme/types.py index c356b8f..7da1ab7 100644 --- a/wsme/types.py +++ b/wsme/types.py @@ -12,18 +12,19 @@ native_types = pod_types + dt_types + extra_types structured_types = [] -def mandatory(datatype): - if isinstance(datatype, AttrDef): - datatype.mandatory = True - return datatype - return AttrDef(datatype, True) + +class wsproperty(property): + def __init__(self, datatype, fget, fset=None, + mandatory=False, doc=None): + property.__init__(self, fget, fset, doc) + self.mandatory = mandatory -class AttrDef(object): - def __init__(self, datatype, mandatory=False, default=None): +class wsattr(object): + def __init__(self, datatype, mandatory=False): self.datatype = datatype self.mandatory = mandatory - self.default = None + def inspect_class(class_): attributes = [] @@ -36,12 +37,12 @@ def inspect_class(class_): continue if inspect.ismethod(attr): continue - if not isinstance(attr, AttrDef): - attrdef = AttrDef(attr) + if not isinstance(attr, wsattr): + attrdef = wsattr(attr) else: attrdef = attr - attributes.append((name, AttrDef)) + attributes.append((name, wsattr)) return attributes def register_type(class_):