Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/sujqvwi/AnonR/anonr.TX.../opt/imh-pyth.../lib/python3....
File: mailbox.py
"""Read/write support for Maildir, mbox, MH, Babyl, and MMDF mailboxes."""
[0] Fix | Delete
[1] Fix | Delete
# Notes for authors of new mailbox subclasses:
[2] Fix | Delete
#
[3] Fix | Delete
# Remember to fsync() changes to disk before closing a modified file
[4] Fix | Delete
# or returning from a flush() method. See functions _sync_flush() and
[5] Fix | Delete
# _sync_close().
[6] Fix | Delete
[7] Fix | Delete
import os
[8] Fix | Delete
import time
[9] Fix | Delete
import calendar
[10] Fix | Delete
import socket
[11] Fix | Delete
import errno
[12] Fix | Delete
import copy
[13] Fix | Delete
import warnings
[14] Fix | Delete
import email
[15] Fix | Delete
import email.message
[16] Fix | Delete
import email.generator
[17] Fix | Delete
import io
[18] Fix | Delete
import contextlib
[19] Fix | Delete
from types import GenericAlias
[20] Fix | Delete
try:
[21] Fix | Delete
import fcntl
[22] Fix | Delete
except ImportError:
[23] Fix | Delete
fcntl = None
[24] Fix | Delete
[25] Fix | Delete
__all__ = ['Mailbox', 'Maildir', 'mbox', 'MH', 'Babyl', 'MMDF',
[26] Fix | Delete
'Message', 'MaildirMessage', 'mboxMessage', 'MHMessage',
[27] Fix | Delete
'BabylMessage', 'MMDFMessage', 'Error', 'NoSuchMailboxError',
[28] Fix | Delete
'NotEmptyError', 'ExternalClashError', 'FormatError']
[29] Fix | Delete
[30] Fix | Delete
linesep = os.linesep.encode('ascii')
[31] Fix | Delete
[32] Fix | Delete
class Mailbox:
[33] Fix | Delete
"""A group of messages in a particular place."""
[34] Fix | Delete
[35] Fix | Delete
def __init__(self, path, factory=None, create=True):
[36] Fix | Delete
"""Initialize a Mailbox instance."""
[37] Fix | Delete
self._path = os.path.abspath(os.path.expanduser(path))
[38] Fix | Delete
self._factory = factory
[39] Fix | Delete
[40] Fix | Delete
def add(self, message):
[41] Fix | Delete
"""Add message and return assigned key."""
[42] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[43] Fix | Delete
[44] Fix | Delete
def remove(self, key):
[45] Fix | Delete
"""Remove the keyed message; raise KeyError if it doesn't exist."""
[46] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[47] Fix | Delete
[48] Fix | Delete
def __delitem__(self, key):
[49] Fix | Delete
self.remove(key)
[50] Fix | Delete
[51] Fix | Delete
def discard(self, key):
[52] Fix | Delete
"""If the keyed message exists, remove it."""
[53] Fix | Delete
try:
[54] Fix | Delete
self.remove(key)
[55] Fix | Delete
except KeyError:
[56] Fix | Delete
pass
[57] Fix | Delete
[58] Fix | Delete
def __setitem__(self, key, message):
[59] Fix | Delete
"""Replace the keyed message; raise KeyError if it doesn't exist."""
[60] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[61] Fix | Delete
[62] Fix | Delete
def get(self, key, default=None):
[63] Fix | Delete
"""Return the keyed message, or default if it doesn't exist."""
[64] Fix | Delete
try:
[65] Fix | Delete
return self.__getitem__(key)
[66] Fix | Delete
except KeyError:
[67] Fix | Delete
return default
[68] Fix | Delete
[69] Fix | Delete
def __getitem__(self, key):
[70] Fix | Delete
"""Return the keyed message; raise KeyError if it doesn't exist."""
[71] Fix | Delete
if not self._factory:
[72] Fix | Delete
return self.get_message(key)
[73] Fix | Delete
else:
[74] Fix | Delete
with contextlib.closing(self.get_file(key)) as file:
[75] Fix | Delete
return self._factory(file)
[76] Fix | Delete
[77] Fix | Delete
def get_message(self, key):
[78] Fix | Delete
"""Return a Message representation or raise a KeyError."""
[79] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[80] Fix | Delete
[81] Fix | Delete
def get_string(self, key):
[82] Fix | Delete
"""Return a string representation or raise a KeyError.
[83] Fix | Delete
[84] Fix | Delete
Uses email.message.Message to create a 7bit clean string
[85] Fix | Delete
representation of the message."""
[86] Fix | Delete
return email.message_from_bytes(self.get_bytes(key)).as_string()
[87] Fix | Delete
[88] Fix | Delete
def get_bytes(self, key):
[89] Fix | Delete
"""Return a byte string representation or raise a KeyError."""
[90] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[91] Fix | Delete
[92] Fix | Delete
def get_file(self, key):
[93] Fix | Delete
"""Return a file-like representation or raise a KeyError."""
[94] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[95] Fix | Delete
[96] Fix | Delete
def iterkeys(self):
[97] Fix | Delete
"""Return an iterator over keys."""
[98] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[99] Fix | Delete
[100] Fix | Delete
def keys(self):
[101] Fix | Delete
"""Return a list of keys."""
[102] Fix | Delete
return list(self.iterkeys())
[103] Fix | Delete
[104] Fix | Delete
def itervalues(self):
[105] Fix | Delete
"""Return an iterator over all messages."""
[106] Fix | Delete
for key in self.iterkeys():
[107] Fix | Delete
try:
[108] Fix | Delete
value = self[key]
[109] Fix | Delete
except KeyError:
[110] Fix | Delete
continue
[111] Fix | Delete
yield value
[112] Fix | Delete
[113] Fix | Delete
def __iter__(self):
[114] Fix | Delete
return self.itervalues()
[115] Fix | Delete
[116] Fix | Delete
def values(self):
[117] Fix | Delete
"""Return a list of messages. Memory intensive."""
[118] Fix | Delete
return list(self.itervalues())
[119] Fix | Delete
[120] Fix | Delete
def iteritems(self):
[121] Fix | Delete
"""Return an iterator over (key, message) tuples."""
[122] Fix | Delete
for key in self.iterkeys():
[123] Fix | Delete
try:
[124] Fix | Delete
value = self[key]
[125] Fix | Delete
except KeyError:
[126] Fix | Delete
continue
[127] Fix | Delete
yield (key, value)
[128] Fix | Delete
[129] Fix | Delete
def items(self):
[130] Fix | Delete
"""Return a list of (key, message) tuples. Memory intensive."""
[131] Fix | Delete
return list(self.iteritems())
[132] Fix | Delete
[133] Fix | Delete
def __contains__(self, key):
[134] Fix | Delete
"""Return True if the keyed message exists, False otherwise."""
[135] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[136] Fix | Delete
[137] Fix | Delete
def __len__(self):
[138] Fix | Delete
"""Return a count of messages in the mailbox."""
[139] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[140] Fix | Delete
[141] Fix | Delete
def clear(self):
[142] Fix | Delete
"""Delete all messages."""
[143] Fix | Delete
for key in self.keys():
[144] Fix | Delete
self.discard(key)
[145] Fix | Delete
[146] Fix | Delete
def pop(self, key, default=None):
[147] Fix | Delete
"""Delete the keyed message and return it, or default."""
[148] Fix | Delete
try:
[149] Fix | Delete
result = self[key]
[150] Fix | Delete
except KeyError:
[151] Fix | Delete
return default
[152] Fix | Delete
self.discard(key)
[153] Fix | Delete
return result
[154] Fix | Delete
[155] Fix | Delete
def popitem(self):
[156] Fix | Delete
"""Delete an arbitrary (key, message) pair and return it."""
[157] Fix | Delete
for key in self.iterkeys():
[158] Fix | Delete
return (key, self.pop(key)) # This is only run once.
[159] Fix | Delete
else:
[160] Fix | Delete
raise KeyError('No messages in mailbox')
[161] Fix | Delete
[162] Fix | Delete
def update(self, arg=None):
[163] Fix | Delete
"""Change the messages that correspond to certain keys."""
[164] Fix | Delete
if hasattr(arg, 'iteritems'):
[165] Fix | Delete
source = arg.iteritems()
[166] Fix | Delete
elif hasattr(arg, 'items'):
[167] Fix | Delete
source = arg.items()
[168] Fix | Delete
else:
[169] Fix | Delete
source = arg
[170] Fix | Delete
bad_key = False
[171] Fix | Delete
for key, message in source:
[172] Fix | Delete
try:
[173] Fix | Delete
self[key] = message
[174] Fix | Delete
except KeyError:
[175] Fix | Delete
bad_key = True
[176] Fix | Delete
if bad_key:
[177] Fix | Delete
raise KeyError('No message with key(s)')
[178] Fix | Delete
[179] Fix | Delete
def flush(self):
[180] Fix | Delete
"""Write any pending changes to the disk."""
[181] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[182] Fix | Delete
[183] Fix | Delete
def lock(self):
[184] Fix | Delete
"""Lock the mailbox."""
[185] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[186] Fix | Delete
[187] Fix | Delete
def unlock(self):
[188] Fix | Delete
"""Unlock the mailbox if it is locked."""
[189] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[190] Fix | Delete
[191] Fix | Delete
def close(self):
[192] Fix | Delete
"""Flush and close the mailbox."""
[193] Fix | Delete
raise NotImplementedError('Method must be implemented by subclass')
[194] Fix | Delete
[195] Fix | Delete
def _string_to_bytes(self, message):
[196] Fix | Delete
# If a message is not 7bit clean, we refuse to handle it since it
[197] Fix | Delete
# likely came from reading invalid messages in text mode, and that way
[198] Fix | Delete
# lies mojibake.
[199] Fix | Delete
try:
[200] Fix | Delete
return message.encode('ascii')
[201] Fix | Delete
except UnicodeError:
[202] Fix | Delete
raise ValueError("String input must be ASCII-only; "
[203] Fix | Delete
"use bytes or a Message instead")
[204] Fix | Delete
[205] Fix | Delete
# Whether each message must end in a newline
[206] Fix | Delete
_append_newline = False
[207] Fix | Delete
[208] Fix | Delete
def _dump_message(self, message, target, mangle_from_=False):
[209] Fix | Delete
# This assumes the target file is open in binary mode.
[210] Fix | Delete
"""Dump message contents to target file."""
[211] Fix | Delete
if isinstance(message, email.message.Message):
[212] Fix | Delete
buffer = io.BytesIO()
[213] Fix | Delete
gen = email.generator.BytesGenerator(buffer, mangle_from_, 0)
[214] Fix | Delete
gen.flatten(message)
[215] Fix | Delete
buffer.seek(0)
[216] Fix | Delete
data = buffer.read()
[217] Fix | Delete
data = data.replace(b'\n', linesep)
[218] Fix | Delete
target.write(data)
[219] Fix | Delete
if self._append_newline and not data.endswith(linesep):
[220] Fix | Delete
# Make sure the message ends with a newline
[221] Fix | Delete
target.write(linesep)
[222] Fix | Delete
elif isinstance(message, (str, bytes, io.StringIO)):
[223] Fix | Delete
if isinstance(message, io.StringIO):
[224] Fix | Delete
warnings.warn("Use of StringIO input is deprecated, "
[225] Fix | Delete
"use BytesIO instead", DeprecationWarning, 3)
[226] Fix | Delete
message = message.getvalue()
[227] Fix | Delete
if isinstance(message, str):
[228] Fix | Delete
message = self._string_to_bytes(message)
[229] Fix | Delete
if mangle_from_:
[230] Fix | Delete
message = message.replace(b'\nFrom ', b'\n>From ')
[231] Fix | Delete
message = message.replace(b'\n', linesep)
[232] Fix | Delete
target.write(message)
[233] Fix | Delete
if self._append_newline and not message.endswith(linesep):
[234] Fix | Delete
# Make sure the message ends with a newline
[235] Fix | Delete
target.write(linesep)
[236] Fix | Delete
elif hasattr(message, 'read'):
[237] Fix | Delete
if hasattr(message, 'buffer'):
[238] Fix | Delete
warnings.warn("Use of text mode files is deprecated, "
[239] Fix | Delete
"use a binary mode file instead", DeprecationWarning, 3)
[240] Fix | Delete
message = message.buffer
[241] Fix | Delete
lastline = None
[242] Fix | Delete
while True:
[243] Fix | Delete
line = message.readline()
[244] Fix | Delete
# Universal newline support.
[245] Fix | Delete
if line.endswith(b'\r\n'):
[246] Fix | Delete
line = line[:-2] + b'\n'
[247] Fix | Delete
elif line.endswith(b'\r'):
[248] Fix | Delete
line = line[:-1] + b'\n'
[249] Fix | Delete
if not line:
[250] Fix | Delete
break
[251] Fix | Delete
if mangle_from_ and line.startswith(b'From '):
[252] Fix | Delete
line = b'>From ' + line[5:]
[253] Fix | Delete
line = line.replace(b'\n', linesep)
[254] Fix | Delete
target.write(line)
[255] Fix | Delete
lastline = line
[256] Fix | Delete
if self._append_newline and lastline and not lastline.endswith(linesep):
[257] Fix | Delete
# Make sure the message ends with a newline
[258] Fix | Delete
target.write(linesep)
[259] Fix | Delete
else:
[260] Fix | Delete
raise TypeError('Invalid message type: %s' % type(message))
[261] Fix | Delete
[262] Fix | Delete
__class_getitem__ = classmethod(GenericAlias)
[263] Fix | Delete
[264] Fix | Delete
[265] Fix | Delete
class Maildir(Mailbox):
[266] Fix | Delete
"""A qmail-style Maildir mailbox."""
[267] Fix | Delete
[268] Fix | Delete
colon = ':'
[269] Fix | Delete
[270] Fix | Delete
def __init__(self, dirname, factory=None, create=True):
[271] Fix | Delete
"""Initialize a Maildir instance."""
[272] Fix | Delete
Mailbox.__init__(self, dirname, factory, create)
[273] Fix | Delete
self._paths = {
[274] Fix | Delete
'tmp': os.path.join(self._path, 'tmp'),
[275] Fix | Delete
'new': os.path.join(self._path, 'new'),
[276] Fix | Delete
'cur': os.path.join(self._path, 'cur'),
[277] Fix | Delete
}
[278] Fix | Delete
if not os.path.exists(self._path):
[279] Fix | Delete
if create:
[280] Fix | Delete
os.mkdir(self._path, 0o700)
[281] Fix | Delete
for path in self._paths.values():
[282] Fix | Delete
os.mkdir(path, 0o700)
[283] Fix | Delete
else:
[284] Fix | Delete
raise NoSuchMailboxError(self._path)
[285] Fix | Delete
self._toc = {}
[286] Fix | Delete
self._toc_mtimes = {'cur': 0, 'new': 0}
[287] Fix | Delete
self._last_read = 0 # Records last time we read cur/new
[288] Fix | Delete
self._skewfactor = 0.1 # Adjust if os/fs clocks are skewing
[289] Fix | Delete
[290] Fix | Delete
def add(self, message):
[291] Fix | Delete
"""Add message and return assigned key."""
[292] Fix | Delete
tmp_file = self._create_tmp()
[293] Fix | Delete
try:
[294] Fix | Delete
self._dump_message(message, tmp_file)
[295] Fix | Delete
except BaseException:
[296] Fix | Delete
tmp_file.close()
[297] Fix | Delete
os.remove(tmp_file.name)
[298] Fix | Delete
raise
[299] Fix | Delete
_sync_close(tmp_file)
[300] Fix | Delete
if isinstance(message, MaildirMessage):
[301] Fix | Delete
subdir = message.get_subdir()
[302] Fix | Delete
suffix = self.colon + message.get_info()
[303] Fix | Delete
if suffix == self.colon:
[304] Fix | Delete
suffix = ''
[305] Fix | Delete
else:
[306] Fix | Delete
subdir = 'new'
[307] Fix | Delete
suffix = ''
[308] Fix | Delete
uniq = os.path.basename(tmp_file.name).split(self.colon)[0]
[309] Fix | Delete
dest = os.path.join(self._path, subdir, uniq + suffix)
[310] Fix | Delete
if isinstance(message, MaildirMessage):
[311] Fix | Delete
os.utime(tmp_file.name,
[312] Fix | Delete
(os.path.getatime(tmp_file.name), message.get_date()))
[313] Fix | Delete
# No file modification should be done after the file is moved to its
[314] Fix | Delete
# final position in order to prevent race conditions with changes
[315] Fix | Delete
# from other programs
[316] Fix | Delete
try:
[317] Fix | Delete
try:
[318] Fix | Delete
os.link(tmp_file.name, dest)
[319] Fix | Delete
except (AttributeError, PermissionError):
[320] Fix | Delete
os.rename(tmp_file.name, dest)
[321] Fix | Delete
else:
[322] Fix | Delete
os.remove(tmp_file.name)
[323] Fix | Delete
except OSError as e:
[324] Fix | Delete
os.remove(tmp_file.name)
[325] Fix | Delete
if e.errno == errno.EEXIST:
[326] Fix | Delete
raise ExternalClashError('Name clash with existing message: %s'
[327] Fix | Delete
% dest)
[328] Fix | Delete
else:
[329] Fix | Delete
raise
[330] Fix | Delete
return uniq
[331] Fix | Delete
[332] Fix | Delete
def remove(self, key):
[333] Fix | Delete
"""Remove the keyed message; raise KeyError if it doesn't exist."""
[334] Fix | Delete
os.remove(os.path.join(self._path, self._lookup(key)))
[335] Fix | Delete
[336] Fix | Delete
def discard(self, key):
[337] Fix | Delete
"""If the keyed message exists, remove it."""
[338] Fix | Delete
# This overrides an inapplicable implementation in the superclass.
[339] Fix | Delete
try:
[340] Fix | Delete
self.remove(key)
[341] Fix | Delete
except (KeyError, FileNotFoundError):
[342] Fix | Delete
pass
[343] Fix | Delete
[344] Fix | Delete
def __setitem__(self, key, message):
[345] Fix | Delete
"""Replace the keyed message; raise KeyError if it doesn't exist."""
[346] Fix | Delete
old_subpath = self._lookup(key)
[347] Fix | Delete
temp_key = self.add(message)
[348] Fix | Delete
temp_subpath = self._lookup(temp_key)
[349] Fix | Delete
if isinstance(message, MaildirMessage):
[350] Fix | Delete
# temp's subdir and suffix were specified by message.
[351] Fix | Delete
dominant_subpath = temp_subpath
[352] Fix | Delete
else:
[353] Fix | Delete
# temp's subdir and suffix were defaults from add().
[354] Fix | Delete
dominant_subpath = old_subpath
[355] Fix | Delete
subdir = os.path.dirname(dominant_subpath)
[356] Fix | Delete
if self.colon in dominant_subpath:
[357] Fix | Delete
suffix = self.colon + dominant_subpath.split(self.colon)[-1]
[358] Fix | Delete
else:
[359] Fix | Delete
suffix = ''
[360] Fix | Delete
self.discard(key)
[361] Fix | Delete
tmp_path = os.path.join(self._path, temp_subpath)
[362] Fix | Delete
new_path = os.path.join(self._path, subdir, key + suffix)
[363] Fix | Delete
if isinstance(message, MaildirMessage):
[364] Fix | Delete
os.utime(tmp_path,
[365] Fix | Delete
(os.path.getatime(tmp_path), message.get_date()))
[366] Fix | Delete
# No file modification should be done after the file is moved to its
[367] Fix | Delete
# final position in order to prevent race conditions with changes
[368] Fix | Delete
# from other programs
[369] Fix | Delete
os.rename(tmp_path, new_path)
[370] Fix | Delete
[371] Fix | Delete
def get_message(self, key):
[372] Fix | Delete
"""Return a Message representation or raise a KeyError."""
[373] Fix | Delete
subpath = self._lookup(key)
[374] Fix | Delete
with open(os.path.join(self._path, subpath), 'rb') as f:
[375] Fix | Delete
if self._factory:
[376] Fix | Delete
msg = self._factory(f)
[377] Fix | Delete
else:
[378] Fix | Delete
msg = MaildirMessage(f)
[379] Fix | Delete
subdir, name = os.path.split(subpath)
[380] Fix | Delete
msg.set_subdir(subdir)
[381] Fix | Delete
if self.colon in name:
[382] Fix | Delete
msg.set_info(name.split(self.colon)[-1])
[383] Fix | Delete
msg.set_date(os.path.getmtime(os.path.join(self._path, subpath)))
[384] Fix | Delete
return msg
[385] Fix | Delete
[386] Fix | Delete
def get_bytes(self, key):
[387] Fix | Delete
"""Return a bytes representation or raise a KeyError."""
[388] Fix | Delete
with open(os.path.join(self._path, self._lookup(key)), 'rb') as f:
[389] Fix | Delete
return f.read().replace(linesep, b'\n')
[390] Fix | Delete
[391] Fix | Delete
def get_file(self, key):
[392] Fix | Delete
"""Return a file-like representation or raise a KeyError."""
[393] Fix | Delete
f = open(os.path.join(self._path, self._lookup(key)), 'rb')
[394] Fix | Delete
return _ProxyFile(f)
[395] Fix | Delete
[396] Fix | Delete
def iterkeys(self):
[397] Fix | Delete
"""Return an iterator over keys."""
[398] Fix | Delete
self._refresh()
[399] Fix | Delete
for key in self._toc:
[400] Fix | Delete
try:
[401] Fix | Delete
self._lookup(key)
[402] Fix | Delete
except KeyError:
[403] Fix | Delete
continue
[404] Fix | Delete
yield key
[405] Fix | Delete
[406] Fix | Delete
def __contains__(self, key):
[407] Fix | Delete
"""Return True if the keyed message exists, False otherwise."""
[408] Fix | Delete
self._refresh()
[409] Fix | Delete
return key in self._toc
[410] Fix | Delete
[411] Fix | Delete
def __len__(self):
[412] Fix | Delete
"""Return a count of messages in the mailbox."""
[413] Fix | Delete
self._refresh()
[414] Fix | Delete
return len(self._toc)
[415] Fix | Delete
[416] Fix | Delete
def flush(self):
[417] Fix | Delete
"""Write any pending changes to disk."""
[418] Fix | Delete
# Maildir changes are always written immediately, so there's nothing
[419] Fix | Delete
# to do.
[420] Fix | Delete
pass
[421] Fix | Delete
[422] Fix | Delete
def lock(self):
[423] Fix | Delete
"""Lock the mailbox."""
[424] Fix | Delete
return
[425] Fix | Delete
[426] Fix | Delete
def unlock(self):
[427] Fix | Delete
"""Unlock the mailbox if it is locked."""
[428] Fix | Delete
return
[429] Fix | Delete
[430] Fix | Delete
def close(self):
[431] Fix | Delete
"""Flush and close the mailbox."""
[432] Fix | Delete
return
[433] Fix | Delete
[434] Fix | Delete
def list_folders(self):
[435] Fix | Delete
"""Return a list of folder names."""
[436] Fix | Delete
result = []
[437] Fix | Delete
for entry in os.listdir(self._path):
[438] Fix | Delete
if len(entry) > 1 and entry[0] == '.' and \
[439] Fix | Delete
os.path.isdir(os.path.join(self._path, entry)):
[440] Fix | Delete
result.append(entry[1:])
[441] Fix | Delete
return result
[442] Fix | Delete
[443] Fix | Delete
def get_folder(self, folder):
[444] Fix | Delete
"""Return a Maildir instance for the named folder."""
[445] Fix | Delete
return Maildir(os.path.join(self._path, '.' + folder),
[446] Fix | Delete
factory=self._factory,
[447] Fix | Delete
create=False)
[448] Fix | Delete
[449] Fix | Delete
def add_folder(self, folder):
[450] Fix | Delete
"""Create a folder and return a Maildir instance representing it."""
[451] Fix | Delete
path = os.path.join(self._path, '.' + folder)
[452] Fix | Delete
result = Maildir(path, factory=self._factory)
[453] Fix | Delete
maildirfolder_path = os.path.join(path, 'maildirfolder')
[454] Fix | Delete
if not os.path.exists(maildirfolder_path):
[455] Fix | Delete
os.close(os.open(maildirfolder_path, os.O_CREAT | os.O_WRONLY,
[456] Fix | Delete
0o666))
[457] Fix | Delete
return result
[458] Fix | Delete
[459] Fix | Delete
def remove_folder(self, folder):
[460] Fix | Delete
"""Delete the named folder, which must be empty."""
[461] Fix | Delete
path = os.path.join(self._path, '.' + folder)
[462] Fix | Delete
for entry in os.listdir(os.path.join(path, 'new')) + \
[463] Fix | Delete
os.listdir(os.path.join(path, 'cur')):
[464] Fix | Delete
if len(entry) < 1 or entry[0] != '.':
[465] Fix | Delete
raise NotEmptyError('Folder contains message(s): %s' % folder)
[466] Fix | Delete
for entry in os.listdir(path):
[467] Fix | Delete
if entry != 'new' and entry != 'cur' and entry != 'tmp' and \
[468] Fix | Delete
os.path.isdir(os.path.join(path, entry)):
[469] Fix | Delete
raise NotEmptyError("Folder contains subdirectory '%s': %s" %
[470] Fix | Delete
(folder, entry))
[471] Fix | Delete
for root, dirs, files in os.walk(path, topdown=False):
[472] Fix | Delete
for entry in files:
[473] Fix | Delete
os.remove(os.path.join(root, entry))
[474] Fix | Delete
for entry in dirs:
[475] Fix | Delete
os.rmdir(os.path.join(root, entry))
[476] Fix | Delete
os.rmdir(path)
[477] Fix | Delete
[478] Fix | Delete
def clean(self):
[479] Fix | Delete
"""Delete old files in "tmp"."""
[480] Fix | Delete
now = time.time()
[481] Fix | Delete
for entry in os.listdir(os.path.join(self._path, 'tmp')):
[482] Fix | Delete
path = os.path.join(self._path, 'tmp', entry)
[483] Fix | Delete
if now - os.path.getatime(path) > 129600: # 60 * 60 * 36
[484] Fix | Delete
os.remove(path)
[485] Fix | Delete
[486] Fix | Delete
_count = 1 # This is used to generate unique file names.
[487] Fix | Delete
[488] Fix | Delete
def _create_tmp(self):
[489] Fix | Delete
"""Create a file in the tmp subdirectory and open and return it."""
[490] Fix | Delete
now = time.time()
[491] Fix | Delete
hostname = socket.gethostname()
[492] Fix | Delete
if '/' in hostname:
[493] Fix | Delete
hostname = hostname.replace('/', r'\057')
[494] Fix | Delete
if ':' in hostname:
[495] Fix | Delete
hostname = hostname.replace(':', r'\072')
[496] Fix | Delete
uniq = "%s.M%sP%sQ%s.%s" % (int(now), int(now % 1 * 1e6), os.getpid(),
[497] Fix | Delete
Maildir._count, hostname)
[498] Fix | Delete
path = os.path.join(self._path, 'tmp', uniq)
[499] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function