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