本文整理汇总了Python中ZODB.blob.Blob._p_activate方法的典型用法代码示例。如果您正苦于以下问题:Python Blob._p_activate方法的具体用法?Python Blob._p_activate怎么用?Python Blob._p_activate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ZODB.blob.Blob
的用法示例。
在下文中一共展示了Blob._p_activate方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: File
# 需要导入模块: from ZODB.blob import Blob [as 别名]
# 或者: from ZODB.blob.Blob import _p_activate [as 别名]
#.........这里部分代码省略.........
self.mimetype = mimetype or "application/octet-stream"
if stream is not None:
if mimetype is USE_MAGIC:
hint = USE_MAGIC
else:
hint = None
self.upload(stream, mimetype_hint=hint)
def upload(self, stream, mimetype_hint=None):
""" Replace the current contents of this file's blob with the
contents of ``stream``. ``stream`` must be a filelike object (it
must have a ``read`` method that takes a size argument).
``mimetype_hint`` can be any of the following:
- ``None``, meaning don't reset the current mimetype. This is the
default. If you already know the file's mimetype, and you don't
want it divined from a filename or stream content, use ``None`` as
the ``mimetype_hint`` value, and set the ``mimetype`` attribute of
the file object directly before or after calling this method.
- A string containing a filename that has an extension; the mimetype
will be derived from the extension in the filename using the Python
``mimetypes`` module, and the result will be set as the mimetype
attribute of this object.
- The constant :attr:`pyramid.file.USE_MAGIC`, which will derive
the mimetype using the ``python-magic`` library based on the
stream's actual content. The result will be set as the mimetype
attribute of this object.
.. warning::
On non-Linux systems, successful use of
:attr:`substanced.file.USE_MAGIC` requires the installation
of additional dependencies. See :ref:`optional_dependencies`.
"""
if not stream:
stream = io.StringIO()
fp = self.blob.open("w")
first = True
use_magic = False
if mimetype_hint is USE_MAGIC:
use_magic = True
if magic is None: # pragma: no cover
warnings.warn(
"The python-magic library does not have its requisite "
"dependencies installed properly, therefore the "
'"USE_MAGIC" flag passed to this method has been ignored '
'(it has been converted to "None"). The mimetype of '
"substanced.file.File objects created may be incorrect as "
"a result."
)
use_magic = False
mimetype_hint = None
if not use_magic:
if mimetype_hint is not None:
mimetype, _ = mimetypes.guess_type(mimetype_hint, strict=False)
if mimetype is None:
mimetype = "application/octet-stream"
self.mimetype = mimetype
for chunk in chunks(stream):
if use_magic and first:
first = False
m = magic.Magic(mime=True)
mimetype = m.from_buffer(chunk)
self.mimetype = u(mimetype)
fp.write(chunk)
fp.close()
def get_response(self, **kw):
""" Return a WebOb-compatible response object which uses the blob
content as the stream data and the mimetype of the file as the
content type. The ``**kw`` arguments will be passed to the
``pyramid.response.FileResponse`` constructor as its keyword
arguments."""
if not "content_type" in kw:
kw["content_type"] = str(self.mimetype)
path = self.blob.committed()
response = FileResponse(path, **kw)
return response
def get_size(self):
""" Return the size in bytes of the data in the blob associated with
the file"""
return os.stat(self.blob.committed()).st_size
def get_etag(self):
""" Return a token identifying the "version" of the file.
"""
self._p_activate()
mine = self._p_serial
blob = self.blob._p_serial
if blob == z64:
self.blob._p_activate()
blob = self.blob._p_serial
return oid_repr(max(mine, blob))