本文整理汇总了Python中twext.python.filepath.CachingFilePath.moveTo方法的典型用法代码示例。如果您正苦于以下问题:Python CachingFilePath.moveTo方法的具体用法?Python CachingFilePath.moveTo怎么用?Python CachingFilePath.moveTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twext.python.filepath.CachingFilePath
的用法示例。
在下文中一共展示了CachingFilePath.moveTo方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: AttachmentStorageTransport
# 需要导入模块: from twext.python.filepath import CachingFilePath [as 别名]
# 或者: from twext.python.filepath.CachingFilePath import moveTo [as 别名]
class AttachmentStorageTransport(StorageTransportBase):
_TEMPORARY_UPLOADS_DIRECTORY = "Temporary"
def __init__(self, attachment, contentType, dispositionName, creating=False, migrating=False):
super(AttachmentStorageTransport, self).__init__(
attachment, contentType, dispositionName)
fileDescriptor, fileName = self._temporaryFile()
# Wrap the file descriptor in a file object we can write to
self._file = os.fdopen(fileDescriptor, "w")
self._path = CachingFilePath(fileName)
self._hash = hashlib.md5()
self._creating = creating
self._migrating = migrating
self._txn.postAbort(self.aborted)
def _temporaryFile(self):
"""
Returns a (file descriptor, absolute path) tuple for a temporary file within
the Attachments/Temporary directory (creating the Temporary subdirectory
if it doesn't exist). It is the caller's responsibility to remove the
file.
"""
attachmentRoot = self._txn._store.attachmentsPath
tempUploadsPath = attachmentRoot.child(self._TEMPORARY_UPLOADS_DIRECTORY)
if not tempUploadsPath.exists():
tempUploadsPath.createDirectory()
return tempfile.mkstemp(dir=tempUploadsPath.path)
@property
def _txn(self):
return self._attachment._txn
def aborted(self):
"""
Transaction aborted - clean up temp files.
"""
if self._path.exists():
self._path.remove()
def write(self, data):
if isinstance(data, buffer):
data = str(data)
self._file.write(data)
self._hash.update(data)
@inlineCallbacks
def loseConnection(self):
"""
Note that when self._migrating is set we only care about the data and don't need to
do any quota checks/adjustments.
"""
# FIXME: this should be synchronously accessible; IAttachment should
# have a method for getting its parent just as CalendarObject/Calendar
# do.
# FIXME: If this method isn't called, the transaction should be
# prevented from committing successfully. It's not valid to have an
# attachment that doesn't point to a real file.
home = (yield self._txn.calendarHomeWithResourceID(self._attachment._ownerHomeID))
oldSize = self._attachment.size()
newSize = self._file.tell()
self._file.close()
# Check max size for attachment
if not self._migrating and newSize > config.MaximumAttachmentSize:
self._path.remove()
if self._creating:
yield self._attachment._internalRemove()
raise AttachmentSizeTooLarge()
# Check overall user quota
if not self._migrating:
allowed = home.quotaAllowedBytes()
if allowed is not None and allowed < ((yield home.quotaUsedBytes())
+ (newSize - oldSize)):
self._path.remove()
if self._creating:
yield self._attachment._internalRemove()
raise QuotaExceeded()
self._path.moveTo(self._attachment._path)
yield self._attachment.changed(
self._contentType,
self._dispositionName,
self._hash.hexdigest(),
newSize
)
#.........这里部分代码省略.........