这有效:
import mailbox
x = mailbox.mbox('filename.mbox') # works
但是如果我只有文件的打开句柄而不是文件名怎么办?
fp = open('filename.mbox', mode='rb') # for example; there are many ways to get an open file-like object
x = mailbox.mbox(fp) # doesn't work
问题:从字节流中打开Mbox的最佳方式(最干净,最快)是什么=打开的二进制句柄,而无需先将字节复制到命名文件中?
答案 0 :(得分:2)
mailbox.mbox()
必须在某个时候调用内置函数open()
。因此,一个有问题的解决方案将是拦截该调用并返回预先存在的文件对象。解决方案草案如下:
import builtins
# FLO stands for file-like object
class MboxFromFLO:
def __init__(self, flo):
original_open = builtins.open
fake_path = '/tmp/MboxFromFLO'
self.fake_path = fake_path
def open_proxy(*args):
print('open_proxy{} was called:'.format(args))
if args[0] == fake_path:
print('Call to open() was intercepted')
return flo
else:
print('Call to open() was let through')
return original_open(*args)
self.original_open = original_open
builtins.open = open_proxy
print('Instrumenting open()')
def __enter__(self):
return mailbox.mbox(self.fake_path)
def __exit__(self, exc_type, exc_value, traceback):
print('Restoring open()')
builtins.open = self.original_open
# Demonstration
import mailbox
# Create an mbox file so that we can use it later
b = mailbox.mbox('test.mbox')
key = b.add('This is a MboxFromFLO test message')
f = open('test.mbox', 'rb')
with MboxFromFLO(f) as b:
print('Msg#{}:'.format(key), b.get(key))
答案 1 :(得分:1)
您可以继承mailbox.mbox。 The source code for the standard library can be found on github.
该逻辑似乎主要在超类_singlefileMailbox
中实现。
class _singlefileMailbox(Mailbox):
"""A single-file mailbox."""
def __init__(self, path, factory=None, create=True):
"""Initialize a single-file mailbox."""
Mailbox.__init__(self, path, factory, create)
try:
f = open(self._path, 'rb+')
except OSError as e:
if e.errno == errno.ENOENT:
if create:
f = open(self._path, 'wb+')
else:
raise NoSuchMailboxError(self._path)
elif e.errno in (errno.EACCES, errno.EROFS):
f = open(self._path, 'rb')
else:
raise
self._file = f
self._toc = None
self._next_key = 0
self._pending = False # No changes require rewriting the file.
self._pending_sync = False # No need to sync the file
self._locked = False
self._file_length = None # Used to record mailbox size
因此,我们可以尝试摆脱open()逻辑,并替换mbox和其他超类中的初始化代码。
class CustomMbox(mailbox.mbox):
"""A custom mbox mailbox from a file like object."""
def __init__(self, fp, factory=None, create=True):
"""Initialize mbox mailbox from a file-like object."""
# from `mailbox.mbox`
self._message_factory = mailbox.mboxMessage
# from `mailbox._singlefileMailbox`
self._file = fp
self._toc = None
self._next_key = 0
self._pending = False # No changes require rewriting the file.
self._pending_sync = False # No need to sync the file
self._locked = False
self._file_length = None # Used to record mailbox size
# from `mailbox.Mailbox`
self._factory = factory
@property
def _path(self):
# If we try to use some functionality that relies on knowing
# the original path, raise an error.
raise NotImplementedError('This class does not have a file path')
def flush(self):
"""Write any pending changes to disk."""
# _singlefileMailbox has quite complicated flush method.
# Hopefully this will work fine.
self._file.flush()
这可能是一个开始。但是您可能必须定义其他方法才能获得其他邮箱类的全部功能。