Rename AttrDef to wsattr, and introduce wsproperty

This commit is contained in:
Christophe de Vienne
2011-09-20 09:26:45 +02:00
parent d66f1fd874
commit 8fe674922f
2 changed files with 35 additions and 11 deletions

23
wsme/tests/test_types.py Normal file
View File

@@ -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

View File

@@ -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_):