本文整理汇总了Python中ZPublisher.Iterators.IStreamIterator类的典型用法代码示例。如果您正苦于以下问题:Python IStreamIterator类的具体用法?Python IStreamIterator怎么用?Python IStreamIterator使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了IStreamIterator类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: manage_FTPget
def manage_FTPget(self, REQUEST=None, RESPONSE=None):
"""Return the body of the content item in an FTP or WebDAV response.
This adapts self to IRawReadFile(), which is then returned as an
iterator. The adapter should provide IStreamIterator.
"""
reader = IRawReadFile(self, None)
if reader is None:
return ''
request = REQUEST is not None and REQUEST or self.REQUEST
response = RESPONSE is not None and RESPONSE or request.response
mimeType = reader.mimeType
encoding = reader.encoding
if mimeType is not None:
if encoding is not None:
response.setHeader('Content-Type', '%s; charset="%s"' % (mimeType, encoding,))
else:
response.setHeader('Content-Type', mimeType)
size = reader.size()
if size is not None:
response.setHeader('Content-Length', str(size))
# if the reader is an iterator that the publisher can handle, return
# it as-is. Otherwise, read the full contents
if ((IInterface.providedBy(IStreamIterator) and IStreamIterator.providedBy(reader))
or (not IInterface.providedBy(IStreamIterator) and IStreamIterator.isImplementedBy(reader))
):
return reader
else:
return reader.read()
示例2: setBody
def setBody(self, body, *args, **kw):
if IInterface.providedBy(IStreamIterator): # is this zope 2.12?
stream = IStreamIterator.providedBy(body)
else:
stream = IStreamIterator.isImplementedBy(body)
if stream:
body = ''.join(body) # read from iterator
return original_setBody(self, body, *args, **kw)
示例3: test_my_avatar
def test_my_avatar(self):
self.login(self.profile1.username)
my_avatar = MyAvatar(self.profile1, self.request)
data = my_avatar()
self.assertTrue(IStreamIterator.providedBy(data))
profile_data = my_avatar.avatar_profile()
self.assertTrue(IStreamIterator.providedBy(profile_data))
no_avatar = MyAvatar(self.profile2, self.request)
data = no_avatar()
self.assertIsNone(data)
示例4: test_my_avatar
def test_my_avatar(self):
self.login(self.profile1.username)
my_avatar = MyAvatar(self.profile1, self.request)
data = my_avatar()
self.assertTrue(IStreamIterator.providedBy(data))
profile_data = my_avatar.avatar_profile()
self.assertTrue(IStreamIterator.providedBy(profile_data))
avatar = MyAvatar(self.profile2, self.request)
self.assertEqual(avatar(), self.default_avatar)
示例5: setBody
def setBody(self, body, title='', is_error=False, lock=None):
# allow locking of the body in the same way as the status
if self._locked_body:
return
if isinstance(body, IOBase):
body.seek(0, 2)
length = body.tell()
body.seek(0)
self.setHeader('Content-Length', '%d' % length)
self.body = body
elif IStreamIterator.providedBy(body):
self.body = body
super(WSGIResponse, self).setBody(b'', title, is_error)
elif IUnboundStreamIterator.providedBy(body):
self.body = body
self._streaming = 1
super(WSGIResponse, self).setBody(b'', title, is_error)
else:
super(WSGIResponse, self).setBody(body, title, is_error)
# Have to apply the lock at the end in case the super class setBody
# is called, which will observe the lock and do nothing
if lock:
self._locked_body = 1
示例6: applyTransformOnSuccess
def applyTransformOnSuccess(event):
"""Apply the transform after a successful request
"""
transformed = applyTransform(event.request)
if transformed is not None:
response = event.request.response
# horrid check to deal with Plone 3/Zope 2.10, where this is still an old-style interface
if ((IInterface.providedBy(IStreamIterator) and IStreamIterator.providedBy(transformed))
or (not IInterface.providedBy(IStreamIterator) and IStreamIterator.isImplementedBy(transformed))
):
response.setBody(transformed)
# setBody() can deal with byte and unicode strings (and will encode as necessary)...
elif isinstance(transformed, basestring):
response.setBody(transformed)
# ... but not with iterables
else:
response.setBody(''.join(transformed))
示例7: setBody
def setBody(self, body, title='', is_error=0):
if isinstance(body, file) or IStreamIterator.providedBy(body):
body.seek(0, 2)
length = body.tell()
body.seek(0)
self.setHeader('Content-Length', '%d' % length)
self.body = body
else:
HTTPResponse.setBody(self, body, title, is_error)
示例8: setBody
def setBody(self, body, title='', is_error=0, **kw):
""" Accept either a stream iterator or a string as the body """
if IStreamIterator.isImplementedBy(body):
assert(self.headers.has_key('content-length'))
# wrap the iterator up in a producer that medusa can understand
self._bodyproducer = iterator_producer(body)
HTTPResponse.setBody(self, '', title, is_error, **kw)
return self
else:
HTTPResponse.setBody(self, body, title, is_error, **kw)
示例9: test_avatars_view
def test_avatars_view(self):
self.login(self.profile1.username)
avatars_view = AvatarsView(self.portal, self.request)
avatars_view.publishTraverse(self.request, self.profile1.username)
data = avatars_view()
self.assertTrue(IStreamIterator.providedBy(data))
avatars_view.publishTraverse(self.request, self.profile2.username)
data = avatars_view()
self.assertIsNone(data)
avatars_view.publishTraverse(self.request, "not-a-username")
with self.assertRaises(NotFound):
avatars_view()
示例10: getWSGIResponse
def getWSGIResponse(self):
# This return a tuple (data_sent, data_to_send_to_WSGI)
result = self.body
if result is not None:
# If we have an iterator, we say that data have been sent
# in order not to commit the transaction finish consuming
# the iterator.
if IStreamIterator.providedBy(result):
return (True, StreamIteratorIterator(result))
elif IResult.providedBy(result):
return (True, result)
if not isinstance(result, str):
log_invalid_response_data(result, self.__environ)
return (self.__started, [result,])
return (self.__started, [])
示例11: test_avatars_view
def test_avatars_view(self):
self.login(self.profile1.username)
avatars_view = AvatarsView(self.portal, self.request)
avatars_view.publishTraverse(self.request,
self.profile1.username)
data = avatars_view()
self.assertTrue(IStreamIterator.providedBy(data))
avatars_view.publishTraverse(self.request,
self.profile2.username)
self.assertEqual(avatars_view(), self.default_avatar)
avatars_view.publishTraverse(self.request,
'not-a-username')
self.assertEqual(avatars_view(), self.default_avatar)
示例12: __init__
def __init__(self, data):
self.fname = tempfile.mktemp()
out = open(self.fname, "w+b")
if isinstance(data, str):
out.write(data)
out.flush()
return
from ZPublisher.Iterators import IStreamIterator
if IStreamIterator.isImplementedBy(data):
for block in data:
out.write(block)
block._p_deactivate()
out.flush()
else:
raise ValueError("Don't know how to write data!")
示例13: applyTransformOnSuccess
def applyTransformOnSuccess(event):
"""Apply the transform after a successful request
"""
transformed = applyTransform(event.request)
if transformed is None:
return
response = event.request.response
if IStreamIterator.providedBy(transformed):
response.setBody(transformed)
# setBody() can deal with byte and unicode strings (and will encode as
# necessary)...
elif isinstance(transformed, basestring):
response.setBody(transformed)
# ... but not with iterables
else:
response.setBody(''.join(transformed))
示例14: setBody
def setBody(self, body, title='', is_error=0, **kw):
""" Accept either a stream iterator or a string as the body """
if not IStreamIterator.providedBy(body):
return HTTPResponse.setBody(self, body, title, is_error, **kw)
assert not self._wrote
if isinstance(body, BlobStreamIterator):
body = body.blob # A BlobFile
if hasattr(body, 'seek') and hasattr(body, 'read') and hasattr(body, 'close'):
self.stdout = body
self._wrote = 1
return
try:
while True:
chunk = body.next()
self.write(chunk)
except StopIteration:
pass
示例15: manage_FTPget
def manage_FTPget(self, REQUEST=None, RESPONSE=None):
"""Get the raw content for this object (also used for the WebDAV source)
"""
if REQUEST is None:
REQUEST = self.REQUEST
if RESPONSE is None:
RESPONSE = REQUEST.RESPONSE
if not self.Schema().hasLayer('marshall'):
RESPONSE.setStatus(501) # Not implemented
return RESPONSE
self.dav__init(REQUEST, RESPONSE)
collection_check(self)
marshaller = self.Schema().getLayerImpl('marshall')
ddata = marshaller.marshall(self, REQUEST=REQUEST, RESPONSE=RESPONSE)
if (shasattr(self, 'marshall_hook') and self.marshall_hook):
ddata = self.marshall_hook(ddata)
content_type, length, data = ddata
RESPONSE.setHeader('Content-Type', content_type)
# Only set Content-Length header length is not None. If we want to
# support 'chunked' Transfer-Encoding we shouldn't set
# this. However ExternalEditor *expects* it to be set if we return
# a StreamIterator, so until that gets fixed we must set it.
if length is not None:
RESPONSE.setHeader('Content-Length', length)
if type(data) is type(''):
return data
# We assume 'data' is a 'Pdata chain' as used by OFS.File and
# return a StreamIterator.
assert length is not None, 'Could not figure out length of Pdata chain'
if (issubclass(IStreamIterator, Interface) and IStreamIterator.providedBy(data)
or not issubclass(IStreamIterator, Interface) and IStreamIterator.IsImplementedBy(data)):
return data
return PdataStreamIterator(data, length)