Documents the File type
This commit is contained in:
parent
f1eedeed19
commit
3ada92fbf1
@ -8,7 +8,7 @@ Changes
|
|||||||
|
|
||||||
* String types handling is clearer.
|
* String types handling is clearer.
|
||||||
|
|
||||||
* New FileType type.
|
* New File type.
|
||||||
|
|
||||||
* Supports cross-referenced types.
|
* Supports cross-referenced types.
|
||||||
|
|
||||||
|
@ -50,11 +50,6 @@ The native types are :
|
|||||||
|
|
||||||
A time (:py:class:`datetime.time`)
|
A time (:py:class:`datetime.time`)
|
||||||
|
|
||||||
- .. wsme:type:: Filetype
|
|
||||||
|
|
||||||
A file (:py:class:`wsme.types.Filetype`). Currently FileType is
|
|
||||||
supported only as input type on protocols that accept form inputs.
|
|
||||||
|
|
||||||
- Arrays -- This is a special case. When stating a list
|
- Arrays -- This is a special case. When stating a list
|
||||||
datatype, always state its content type as the unique element
|
datatype, always state its content type as the unique element
|
||||||
of a list. Example::
|
of a list. Example::
|
||||||
@ -239,3 +234,13 @@ A few things you should know about complex types:
|
|||||||
|
|
||||||
class B(object):
|
class B(object):
|
||||||
a = wsattr(A)
|
a = wsattr(A)
|
||||||
|
|
||||||
|
|
||||||
|
Predefined Types
|
||||||
|
~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
.. default-domain:: wsme
|
||||||
|
|
||||||
|
- .. autotype:: wsme.types.File
|
||||||
|
:members:
|
||||||
|
|
||||||
|
@ -22,6 +22,12 @@ import wsme.types
|
|||||||
field_re = re.compile(r':(?P<field>\w+)(\s+(?P<name>\w+))?:')
|
field_re = re.compile(r':(?P<field>\w+)(\s+(?P<name>\w+))?:')
|
||||||
|
|
||||||
|
|
||||||
|
def datatypename(datatype):
|
||||||
|
if isinstance(datatype, wsme.types.UserType):
|
||||||
|
return datatype.name
|
||||||
|
return datatype.__name__
|
||||||
|
|
||||||
|
|
||||||
def make_sample_object(datatype):
|
def make_sample_object(datatype):
|
||||||
if datatype is wsme.types.bytes:
|
if datatype is wsme.types.bytes:
|
||||||
return 'samplestring'
|
return 'samplestring'
|
||||||
@ -210,7 +216,10 @@ class AttributeDocumenter(autodoc.AttributeDocumenter):
|
|||||||
return success
|
return success
|
||||||
|
|
||||||
def add_content(self, more_content, no_docstring=False):
|
def add_content(self, more_content, no_docstring=False):
|
||||||
self.add_line(u':type: %s' % self.datatype.__name__, '<wsme.sphinxext>')
|
self.add_line(
|
||||||
|
u':type: %s' % datatypename(self.datatype),
|
||||||
|
'<wsme.sphinxext>'
|
||||||
|
)
|
||||||
self.add_line(u'', '<wsme.sphinxext>')
|
self.add_line(u'', '<wsme.sphinxext>')
|
||||||
super(AttributeDocumenter, self).add_content(
|
super(AttributeDocumenter, self).add_content(
|
||||||
more_content, no_docstring)
|
more_content, no_docstring)
|
||||||
@ -341,7 +350,7 @@ class FunctionDocumenter(autodoc.MethodDocumenter):
|
|||||||
for arg in self.wsme_fd.arguments:
|
for arg in self.wsme_fd.arguments:
|
||||||
content = [
|
content = [
|
||||||
u':type %s: :wsme:type:`%s`' % (
|
u':type %s: :wsme:type:`%s`' % (
|
||||||
arg.name, arg.datatype.__name__)
|
arg.name, datatypename(arg.datatype))
|
||||||
]
|
]
|
||||||
if arg.name not in found_params:
|
if arg.name not in found_params:
|
||||||
content.insert(0, u':param %s: ' % (arg.name))
|
content.insert(0, u':param %s: ' % (arg.name))
|
||||||
|
@ -79,6 +79,7 @@ class DictType(object):
|
|||||||
|
|
||||||
class UserType(object):
|
class UserType(object):
|
||||||
basetype = None
|
basetype = None
|
||||||
|
name = None
|
||||||
|
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
return
|
return
|
||||||
@ -99,11 +100,16 @@ class BinaryType(UserType):
|
|||||||
A user type that use base64 strings to carry binary data.
|
A user type that use base64 strings to carry binary data.
|
||||||
"""
|
"""
|
||||||
basetype = bytes
|
basetype = bytes
|
||||||
|
name = 'binary'
|
||||||
|
|
||||||
def tobasetype(self, value):
|
def tobasetype(self, value):
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
return base64.encodestring(value)
|
return base64.encodestring(value)
|
||||||
|
|
||||||
def frombasetype(self, value):
|
def frombasetype(self, value):
|
||||||
|
if value is None:
|
||||||
|
return None
|
||||||
return base64.decodestring(value)
|
return base64.decodestring(value)
|
||||||
|
|
||||||
#: The binary almost-native type
|
#: The binary almost-native type
|
||||||
@ -125,9 +131,13 @@ class Enum(UserType):
|
|||||||
Specie = Enum(str, 'cat', 'dog')
|
Specie = Enum(str, 'cat', 'dog')
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def __init__(self, basetype, *values):
|
def __init__(self, basetype, *values, **kw):
|
||||||
self.basetype = basetype
|
self.basetype = basetype
|
||||||
self.values = set(values)
|
self.values = set(values)
|
||||||
|
name = kw.pop('name')
|
||||||
|
if name is None:
|
||||||
|
name = "Enum(%s)" % ', '.join((str(v) for v in values))
|
||||||
|
self.name = name
|
||||||
|
|
||||||
def validate(self, value):
|
def validate(self, value):
|
||||||
if value not in self.values:
|
if value not in self.values:
|
||||||
@ -506,7 +516,10 @@ class File(Base):
|
|||||||
In the particular case of protocol accepting form encoded data as
|
In the particular case of protocol accepting form encoded data as
|
||||||
input, File can be loaded from a form file field.
|
input, File can be loaded from a form file field.
|
||||||
"""
|
"""
|
||||||
|
#: The file name
|
||||||
filename = wsattr(text)
|
filename = wsattr(text)
|
||||||
|
|
||||||
|
#: Mime type of the content
|
||||||
contenttype = wsattr(text)
|
contenttype = wsattr(text)
|
||||||
|
|
||||||
def _get_content(self):
|
def _get_content(self):
|
||||||
@ -518,6 +531,7 @@ class File(Base):
|
|||||||
self._content = value
|
self._content = value
|
||||||
self._file = None
|
self._file = None
|
||||||
|
|
||||||
|
#: File content
|
||||||
content = wsproperty(binary, _get_content, _set_content)
|
content = wsproperty(binary, _get_content, _set_content)
|
||||||
|
|
||||||
def __init__(self, filename=None, file=None, content=None,
|
def __init__(self, filename=None, file=None, content=None,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user