MCPcopy Index your code
hub / github.com/python/cpython / add

Method add

Lib/mailbox.py:299–339  ·  view source on GitHub ↗

Add message and return assigned key.

(self, message)

Source from the content-addressed store, hash-verified

297 self._skewfactor = 0.1 # Adjust if os/fs clocks are skewing
298
299 def add(self, message):
300 """Add message and return assigned key."""
301 tmp_file = self._create_tmp()
302 try:
303 self._dump_message(message, tmp_file)
304 except BaseException:
305 tmp_file.close()
306 os.remove(tmp_file.name)
307 raise
308 _sync_close(tmp_file)
309 if isinstance(message, MaildirMessage):
310 subdir = message.get_subdir()
311 suffix = self.colon + message.get_info()
312 if suffix == self.colon:
313 suffix = ''
314 else:
315 subdir = 'new'
316 suffix = ''
317 uniq = os.path.basename(tmp_file.name).split(self.colon)[0]
318 dest = os.path.join(self._path, subdir, uniq + suffix)
319 if isinstance(message, MaildirMessage):
320 os.utime(tmp_file.name,
321 (os.path.getatime(tmp_file.name), message.get_date()))
322 # No file modification should be done after the file is moved to its
323 # final position in order to prevent race conditions with changes
324 # from other programs
325 try:
326 try:
327 os.link(tmp_file.name, dest)
328 except (AttributeError, PermissionError):
329 os.rename(tmp_file.name, dest)
330 else:
331 os.remove(tmp_file.name)
332 except OSError as e:
333 os.remove(tmp_file.name)
334 if e.errno == errno.EEXIST:
335 raise ExternalClashError('Name clash with existing message: %s'
336 % dest)
337 else:
338 raise
339 return uniq
340
341 def remove(self, key):
342 """Remove the keyed message; raise KeyError if it doesn't exist."""

Callers 1

__setitem__Method · 0.95

Calls 14

_create_tmpMethod · 0.95
_sync_closeFunction · 0.85
ExternalClashErrorClass · 0.85
_dump_messageMethod · 0.80
get_subdirMethod · 0.80
utimeMethod · 0.80
get_dateMethod · 0.80
closeMethod · 0.45
removeMethod · 0.45
get_infoMethod · 0.45
splitMethod · 0.45
basenameMethod · 0.45

Tested by

no test coverage detected