Replace the keyed message; raise KeyError if it doesn't exist.
(self, key, message)
| 351 | pass |
| 352 | |
| 353 | def __setitem__(self, key, message): |
| 354 | """Replace the keyed message; raise KeyError if it doesn't exist.""" |
| 355 | old_subpath = self._lookup(key) |
| 356 | temp_key = self.add(message) |
| 357 | temp_subpath = self._lookup(temp_key) |
| 358 | if isinstance(message, MaildirMessage): |
| 359 | # temp's subdir and suffix were specified by message. |
| 360 | dominant_subpath = temp_subpath |
| 361 | else: |
| 362 | # temp's subdir and suffix were defaults from add(). |
| 363 | dominant_subpath = old_subpath |
| 364 | subdir = os.path.dirname(dominant_subpath) |
| 365 | if self.colon in dominant_subpath: |
| 366 | suffix = self.colon + dominant_subpath.split(self.colon)[-1] |
| 367 | else: |
| 368 | suffix = '' |
| 369 | self.discard(key) |
| 370 | tmp_path = os.path.join(self._path, temp_subpath) |
| 371 | new_path = os.path.join(self._path, subdir, key + suffix) |
| 372 | if isinstance(message, MaildirMessage): |
| 373 | os.utime(tmp_path, |
| 374 | (os.path.getatime(tmp_path), message.get_date())) |
| 375 | # No file modification should be done after the file is moved to its |
| 376 | # final position in order to prevent race conditions with changes |
| 377 | # from other programs |
| 378 | os.rename(tmp_path, new_path) |
| 379 | |
| 380 | def get_message(self, key): |
| 381 | """Return a Message representation or raise a KeyError.""" |