Back to index

python3.2  3.2.2
Public Member Functions | Private Member Functions | Private Attributes
mailbox._singlefileMailbox Class Reference
Inheritance diagram for mailbox._singlefileMailbox:
Inheritance graph
[legend]
Collaboration diagram for mailbox._singlefileMailbox:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def add
def remove
def __setitem__
def iterkeys
def __contains__
def __len__
def lock
def unlock
def flush
def close
def __delitem__
def discard
def get
def __getitem__
def get_message
def get_string
def get_bytes
def get_file
def keys
def itervalues
def __iter__
def values
def iteritems
def items
def clear
def pop
def popitem
def update

Private Member Functions

def _pre_mailbox_hook
def _pre_message_hook
def _post_message_hook
def _lookup
def _append_message

Private Attributes

 _file
 _toc
 _next_key
 _pending
 _locked
 _file_length

Detailed Description

A single-file mailbox.

Definition at line 571 of file mailbox.py.


Constructor & Destructor Documentation

def mailbox._singlefileMailbox.__init__ (   self,
  path,
  factory = None,
  create = True 
)
Initialize a single-file mailbox.

Reimplemented from mailbox.Mailbox.

Reimplemented in mailbox.Babyl, mailbox.MMDF, and mailbox.mbox.

Definition at line 574 of file mailbox.py.

00574 
00575     def __init__(self, path, factory=None, create=True):
00576         """Initialize a single-file mailbox."""
00577         Mailbox.__init__(self, path, factory, create)
00578         try:
00579             f = open(self._path, 'rb+')
00580         except IOError as e:
00581             if e.errno == errno.ENOENT:
00582                 if create:
00583                     f = open(self._path, 'wb+')
00584                 else:
00585                     raise NoSuchMailboxError(self._path)
00586             elif e.errno in (errno.EACCES, errno.EROFS):
00587                 f = open(self._path, 'rb')
00588             else:
00589                 raise
00590         self._file = f
00591         self._toc = None
00592         self._next_key = 0
00593         self._pending = False   # No changes require rewriting the file.
00594         self._locked = False
00595         self._file_length = None        # Used to record mailbox size

Here is the caller graph for this function:


Member Function Documentation

def mailbox._singlefileMailbox.__contains__ (   self,
  key 
)
Return True if the keyed message exists, False otherwise.

Reimplemented from mailbox.Mailbox.

Definition at line 622 of file mailbox.py.

00622 
00623     def __contains__(self, key):
00624         """Return True if the keyed message exists, False otherwise."""
00625         self._lookup()
00626         return key in self._toc

Here is the call graph for this function:

def mailbox.Mailbox.__delitem__ (   self,
  key 
) [inherited]

Definition at line 54 of file mailbox.py.

00054 
00055     def __delitem__(self, key):
00056         self.remove(key)

Here is the call graph for this function:

def mailbox.Mailbox.__getitem__ (   self,
  key 
) [inherited]
Return the keyed message; raise KeyError if it doesn't exist.

Definition at line 75 of file mailbox.py.

00075 
00076     def __getitem__(self, key):
00077         """Return the keyed message; raise KeyError if it doesn't exist."""
00078         if not self._factory:
00079             return self.get_message(key)
00080         else:
00081             with contextlib.closing(self.get_file(key)) as file:
00082                 return self._factory(file)

Here is the call graph for this function:

Here is the caller graph for this function:

def mailbox.Mailbox.__iter__ (   self) [inherited]

Definition at line 119 of file mailbox.py.

00119 
00120     def __iter__(self):
00121         return self.itervalues()

Here is the call graph for this function:

Return a count of messages in the mailbox.

Reimplemented from mailbox.Mailbox.

Definition at line 627 of file mailbox.py.

00627 
00628     def __len__(self):
00629         """Return a count of messages in the mailbox."""
00630         self._lookup()
00631         return len(self._toc)

Here is the call graph for this function:

def mailbox._singlefileMailbox.__setitem__ (   self,
  key,
  message 
)
Replace the keyed message; raise KeyError if it doesn't exist.

Reimplemented from mailbox.Mailbox.

Reimplemented in mailbox.Babyl.

Definition at line 610 of file mailbox.py.

00610 
00611     def __setitem__(self, key, message):
00612         """Replace the keyed message; raise KeyError if it doesn't exist."""
00613         self._lookup(key)
00614         self._toc[key] = self._append_message(message)
00615         self._pending = True

Here is the call graph for this function:

def mailbox._singlefileMailbox._append_message (   self,
  message 
) [private]
Append message to mailbox and return (start, stop) offsets.

Definition at line 731 of file mailbox.py.

00731 
00732     def _append_message(self, message):
00733         """Append message to mailbox and return (start, stop) offsets."""
00734         self._file.seek(0, 2)
00735         before = self._file.tell()
00736         try:
00737             self._pre_message_hook(self._file)
00738             offsets = self._install_message(message)
00739             self._post_message_hook(self._file)
00740         except BaseException:
00741             self._file.truncate(before)
00742             raise
00743         self._file.flush()
00744         self._file_length = self._file.tell()  # Record current length of mailbox
00745         return offsets
00746 
00747 

Here is the call graph for this function:

Here is the caller graph for this function:

def mailbox._singlefileMailbox._lookup (   self,
  key = None 
) [private]
Return (start, stop) or raise KeyError.

Definition at line 721 of file mailbox.py.

00721 
00722     def _lookup(self, key=None):
00723         """Return (start, stop) or raise KeyError."""
00724         if self._toc is None:
00725             self._generate_toc()
00726         if key is not None:
00727             try:
00728                 return self._toc[key]
00729             except KeyError:
00730                 raise KeyError('No message with key: %s' % key)

Here is the call graph for this function:

Here is the caller graph for this function:

def mailbox._singlefileMailbox._post_message_hook (   self,
  f 
) [private]
Called after writing each message to file f.

Reimplemented in mailbox.Babyl, and mailbox.MMDF.

Definition at line 710 of file mailbox.py.

00710 
00711     def _post_message_hook(self, f):
00712         """Called after writing each message to file f."""
00713         return

Here is the caller graph for this function:

def mailbox._singlefileMailbox._pre_mailbox_hook (   self,
  f 
) [private]
Called before writing the mailbox to file f.

Reimplemented in mailbox.Babyl.

Definition at line 702 of file mailbox.py.

00702 
00703     def _pre_mailbox_hook(self, f):
00704         """Called before writing the mailbox to file f."""
00705         return

Here is the caller graph for this function:

def mailbox._singlefileMailbox._pre_message_hook (   self,
  f 
) [private]
Called before writing each message to file f.

Reimplemented in mailbox.Babyl, mailbox.MMDF, and mailbox.mbox.

Definition at line 706 of file mailbox.py.

00706 
00707     def _pre_message_hook(self, f):
00708         """Called before writing each message to file f."""
00709         return

Here is the caller graph for this function:

def mailbox._singlefileMailbox.add (   self,
  message 
)
Add message and return assigned key.

Reimplemented from mailbox.Mailbox.

Reimplemented in mailbox.Babyl.

Definition at line 596 of file mailbox.py.

00596 
00597     def add(self, message):
00598         """Add message and return assigned key."""
00599         self._lookup()
00600         self._toc[self._next_key] = self._append_message(message)
00601         self._next_key += 1
00602         self._pending = True
00603         return self._next_key - 1

Here is the call graph for this function:

Here is the caller graph for this function:

def mailbox.Mailbox.clear (   self) [inherited]
Delete all messages.

Definition at line 147 of file mailbox.py.

00147 
00148     def clear(self):
00149         """Delete all messages."""
00150         for key in self.keys():
00151             self.discard(key)

Here is the call graph for this function:

Flush and close the mailbox.

Reimplemented from mailbox.Mailbox.

Definition at line 714 of file mailbox.py.

00714 
00715     def close(self):
00716         """Flush and close the mailbox."""
00717         self.flush()
00718         if self._locked:
00719             self.unlock()
00720         self._file.close()  # Sync has been done by self.flush() above.

Here is the call graph for this function:

Here is the caller graph for this function:

def mailbox.Mailbox.discard (   self,
  key 
) [inherited]
If the keyed message exists, remove it.

Reimplemented in mailbox.Maildir.

Definition at line 57 of file mailbox.py.

00057 
00058     def discard(self, key):
00059         """If the keyed message exists, remove it."""
00060         try:
00061             self.remove(key)
00062         except KeyError:
00063             pass

Here is the call graph for this function:

Here is the caller graph for this function:

Write any pending changes to disk.

Reimplemented from mailbox.Mailbox.

Definition at line 644 of file mailbox.py.

00644 
00645     def flush(self):
00646         """Write any pending changes to disk."""
00647         if not self._pending:
00648             return
00649 
00650         # In order to be writing anything out at all, self._toc must
00651         # already have been generated (and presumably has been modified
00652         # by adding or deleting an item).
00653         assert self._toc is not None
00654 
00655         # Check length of self._file; if it's changed, some other process
00656         # has modified the mailbox since we scanned it.
00657         self._file.seek(0, 2)
00658         cur_len = self._file.tell()
00659         if cur_len != self._file_length:
00660             raise ExternalClashError('Size of mailbox file changed '
00661                                      '(expected %i, found %i)' %
00662                                      (self._file_length, cur_len))
00663 
00664         new_file = _create_temporary(self._path)
00665         try:
00666             new_toc = {}
00667             self._pre_mailbox_hook(new_file)
00668             for key in sorted(self._toc.keys()):
00669                 start, stop = self._toc[key]
00670                 self._file.seek(start)
00671                 self._pre_message_hook(new_file)
00672                 new_start = new_file.tell()
00673                 while True:
00674                     buffer = self._file.read(min(4096,
00675                                                  stop - self._file.tell()))
00676                     if not buffer:
00677                         break
00678                     new_file.write(buffer)
00679                 new_toc[key] = (new_start, new_file.tell())
00680                 self._post_message_hook(new_file)
00681         except:
00682             new_file.close()
00683             os.remove(new_file.name)
00684             raise
00685         _sync_close(new_file)
00686         # self._file is about to get replaced, so no need to sync.
00687         self._file.close()
00688         try:
00689             os.rename(new_file.name, self._path)
00690         except OSError as e:
00691             if e.errno == errno.EEXIST or \
00692               (os.name == 'os2' and e.errno == errno.EACCES):
00693                 os.remove(self._path)
00694                 os.rename(new_file.name, self._path)
00695             else:
00696                 raise
00697         self._file = open(self._path, 'rb+')
00698         self._toc = new_toc
00699         self._pending = False
00700         if self._locked:
00701             _lock_file(self._file, dotlock=False)

Here is the call graph for this function:

Here is the caller graph for this function:

def mailbox.Mailbox.get (   self,
  key,
  default = None 
) [inherited]
Return the keyed message, or default if it doesn't exist.

Definition at line 68 of file mailbox.py.

00068 
00069     def get(self, key, default=None):
00070         """Return the keyed message, or default if it doesn't exist."""
00071         try:
00072             return self.__getitem__(key)
00073         except KeyError:
00074             return default

Here is the call graph for this function:

Here is the caller graph for this function:

def mailbox.Mailbox.get_bytes (   self,
  key 
) [inherited]
Return a byte string representation or raise a KeyError.

Reimplemented in mailbox.Babyl, mailbox.MH, and mailbox.Maildir.

Definition at line 94 of file mailbox.py.

00094 
00095     def get_bytes(self, key):
00096         """Return a byte string representation or raise a KeyError."""
00097         raise NotImplementedError('Method must be implemented by subclass')

Here is the caller graph for this function:

def mailbox.Mailbox.get_file (   self,
  key 
) [inherited]
Return a file-like representation or raise a KeyError.

Reimplemented in mailbox.Babyl, test.test_mailbox.FakeMailBox, mailbox.MH, and mailbox.Maildir.

Definition at line 98 of file mailbox.py.

00098 
00099     def get_file(self, key):
00100         """Return a file-like representation or raise a KeyError."""
00101         raise NotImplementedError('Method must be implemented by subclass')

Here is the caller graph for this function:

def mailbox.Mailbox.get_message (   self,
  key 
) [inherited]
Return a Message representation or raise a KeyError.

Reimplemented in mailbox.Babyl, mailbox.MH, mailbox._mboxMMDF, and mailbox.Maildir.

Definition at line 83 of file mailbox.py.

00083 
00084     def get_message(self, key):
00085         """Return a Message representation or raise a KeyError."""
00086         raise NotImplementedError('Method must be implemented by subclass')

Here is the caller graph for this function:

def mailbox.Mailbox.get_string (   self,
  key 
) [inherited]
Return a string representation or raise a KeyError.

Uses email.message.Message to create a 7bit clean string
representation of the message.

Definition at line 87 of file mailbox.py.

00087 
00088     def get_string(self, key):
00089         """Return a string representation or raise a KeyError.
00090 
00091         Uses email.message.Message to create a 7bit clean string
00092         representation of the message."""
00093         return email.message_from_bytes(self.get_bytes(key)).as_string()

Here is the call graph for this function:

def mailbox.Mailbox.items (   self) [inherited]
Return a list of (key, message) tuples. Memory intensive.

Definition at line 135 of file mailbox.py.

00135 
00136     def items(self):
00137         """Return a list of (key, message) tuples. Memory intensive."""
00138         return list(self.iteritems())

Here is the call graph for this function:

Here is the caller graph for this function:

def mailbox.Mailbox.iteritems (   self) [inherited]
Return an iterator over (key, message) tuples.

Definition at line 126 of file mailbox.py.

00126 
00127     def iteritems(self):
00128         """Return an iterator over (key, message) tuples."""
00129         for key in self.keys():
00130             try:
00131                 value = self[key]
00132             except KeyError:
00133                 continue
00134             yield (key, value)

Here is the call graph for this function:

Here is the caller graph for this function:

Return an iterator over keys.

Reimplemented from mailbox.Mailbox.

Definition at line 616 of file mailbox.py.

00616 
00617     def iterkeys(self):
00618         """Return an iterator over keys."""
00619         self._lookup()
00620         for key in self._toc.keys():
00621             yield key

Here is the call graph for this function:

Here is the caller graph for this function:

def mailbox.Mailbox.itervalues (   self) [inherited]
Return an iterator over all messages.

Definition at line 110 of file mailbox.py.

00110 
00111     def itervalues(self):
00112         """Return an iterator over all messages."""
00113         for key in self.keys():
00114             try:
00115                 value = self[key]
00116             except KeyError:
00117                 continue
00118             yield value

Here is the call graph for this function:

Here is the caller graph for this function:

def mailbox.Mailbox.keys (   self) [inherited]
Return a list of keys.

Definition at line 106 of file mailbox.py.

00106 
00107     def keys(self):
00108         """Return a list of keys."""
00109         return list(self.iterkeys())

Here is the call graph for this function:

Here is the caller graph for this function:

Lock the mailbox.

Reimplemented from mailbox.Mailbox.

Definition at line 632 of file mailbox.py.

00632 
00633     def lock(self):
00634         """Lock the mailbox."""
00635         if not self._locked:
00636             _lock_file(self._file)
00637             self._locked = True

Here is the call graph for this function:

Here is the caller graph for this function:

def mailbox.Mailbox.pop (   self,
  key,
  default = None 
) [inherited]
Delete the keyed message and return it, or default.

Definition at line 152 of file mailbox.py.

00152 
00153     def pop(self, key, default=None):
00154         """Delete the keyed message and return it, or default."""
00155         try:
00156             result = self[key]
00157         except KeyError:
00158             return default
00159         self.discard(key)
00160         return result

Here is the call graph for this function:

Here is the caller graph for this function:

def mailbox.Mailbox.popitem (   self) [inherited]
Delete an arbitrary (key, message) pair and return it.

Definition at line 161 of file mailbox.py.

00161 
00162     def popitem(self):
00163         """Delete an arbitrary (key, message) pair and return it."""
00164         for key in self.keys():
00165             return (key, self.pop(key))     # This is only run once.
00166         else:
00167             raise KeyError('No messages in mailbox')

Here is the call graph for this function:

def mailbox._singlefileMailbox.remove (   self,
  key 
)
Remove the keyed message; raise KeyError if it doesn't exist.

Reimplemented from mailbox.Mailbox.

Reimplemented in mailbox.Babyl.

Definition at line 604 of file mailbox.py.

00604 
00605     def remove(self, key):
00606         """Remove the keyed message; raise KeyError if it doesn't exist."""
00607         self._lookup(key)
00608         del self._toc[key]
00609         self._pending = True

Here is the call graph for this function:

Here is the caller graph for this function:

Unlock the mailbox if it is locked.

Reimplemented from mailbox.Mailbox.

Definition at line 638 of file mailbox.py.

00638 
00639     def unlock(self):
00640         """Unlock the mailbox if it is locked."""
00641         if self._locked:
00642             _unlock_file(self._file)
00643             self._locked = False

Here is the call graph for this function:

def mailbox.Mailbox.update (   self,
  arg = None 
) [inherited]
Change the messages that correspond to certain keys.

Definition at line 168 of file mailbox.py.

00168 
00169     def update(self, arg=None):
00170         """Change the messages that correspond to certain keys."""
00171         if hasattr(arg, 'iteritems'):
00172             source = arg.items()
00173         elif hasattr(arg, 'items'):
00174             source = arg.items()
00175         else:
00176             source = arg
00177         bad_key = False
00178         for key, message in source:
00179             try:
00180                 self[key] = message
00181             except KeyError:
00182                 bad_key = True
00183         if bad_key:
00184             raise KeyError('No message with key(s)')

Here is the caller graph for this function:

def mailbox.Mailbox.values (   self) [inherited]
Return a list of messages. Memory intensive.

Definition at line 122 of file mailbox.py.

00122 
00123     def values(self):
00124         """Return a list of messages. Memory intensive."""
00125         return list(self.itervalues())

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Definition at line 589 of file mailbox.py.

Reimplemented in mailbox.Babyl, mailbox.MMDF, and mailbox.mbox.

Definition at line 594 of file mailbox.py.

Definition at line 593 of file mailbox.py.

Reimplemented in mailbox.Babyl, mailbox.MMDF, and mailbox.mbox.

Definition at line 591 of file mailbox.py.

Definition at line 592 of file mailbox.py.

Reimplemented in mailbox.Babyl, mailbox.MMDF, and mailbox.mbox.

Definition at line 590 of file mailbox.py.


The documentation for this class was generated from the following file: