Back to index

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

List of all members.

Public Member Functions

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

Private Member Functions

def _string_to_bytes
def _dump_message

Private Attributes

 _path
 _factory

Detailed Description

A group of messages in a particular place.

Definition at line 38 of file mailbox.py.


Constructor & Destructor Documentation

def mailbox.Mailbox.__init__ (   self,
  path,
  factory = None,
  create = True 
)
A group of messages in a particular place.
Initialize a Mailbox instance.

Reimplemented in mailbox.Babyl, mailbox.MH, mailbox.MMDF, mailbox.mbox, mailbox._singlefileMailbox, and mailbox.Maildir.

Definition at line 41 of file mailbox.py.

00041 
00042     def __init__(self, path, factory=None, create=True):
00043         """Initialize a Mailbox instance."""
00044         self._path = os.path.abspath(os.path.expanduser(path))
00045         self._factory = factory

Here is the caller graph for this function:


Member Function Documentation

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

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

Definition at line 139 of file mailbox.py.

00139 
00140     def __contains__(self, key):
00141         """Return True if the keyed message exists, False otherwise."""
00142         raise NotImplementedError('Method must be implemented by subclass')

def mailbox.Mailbox.__delitem__ (   self,
  key 
)

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 
)
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)

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:

def mailbox.Mailbox.__len__ (   self)
Return a count of messages in the mailbox.

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

Definition at line 143 of file mailbox.py.

00143 
00144     def __len__(self):
00145         """Return a count of messages in the mailbox."""
00146         raise NotImplementedError('Method must be implemented by subclass')

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

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

Definition at line 64 of file mailbox.py.

00064 
00065     def __setitem__(self, key, message):
00066         """Replace the keyed message; raise KeyError if it doesn't exist."""
00067         raise NotImplementedError('Method must be implemented by subclass')

def mailbox.Mailbox._dump_message (   self,
  message,
  target,
  mangle_from_ = False 
) [private]
Dump message contents to target file.

Definition at line 211 of file mailbox.py.

00211 
00212     def _dump_message(self, message, target, mangle_from_=False):
00213         # This assumes the target file is open in binary mode.
00214         """Dump message contents to target file."""
00215         if isinstance(message, email.message.Message):
00216             buffer = io.BytesIO()
00217             gen = email.generator.BytesGenerator(buffer, mangle_from_, 0)
00218             gen.flatten(message)
00219             buffer.seek(0)
00220             data = buffer.read()
00221             data = data.replace(b'\n', linesep)
00222             target.write(data)
00223         elif isinstance(message, (str, bytes, io.StringIO)):
00224             if isinstance(message, io.StringIO):
00225                 warnings.warn("Use of StringIO input is deprecated, "
00226                     "use BytesIO instead", DeprecationWarning, 3)
00227                 message = message.getvalue()
00228             if isinstance(message, str):
00229                 message = self._string_to_bytes(message)
00230             if mangle_from_:
00231                 message = message.replace(b'\nFrom ', b'\n>From ')
00232             message = message.replace(b'\n', linesep)
00233             target.write(message)
00234         elif hasattr(message, 'read'):
00235             if hasattr(message, 'buffer'):
00236                 warnings.warn("Use of text mode files is deprecated, "
00237                     "use a binary mode file instead", DeprecationWarning, 3)
00238                 message = message.buffer
00239             while True:
00240                 line = message.readline()
00241                 # Universal newline support.
00242                 if line.endswith(b'\r\n'):
00243                     line = line[:-2] + b'\n'
00244                 elif line.endswith(b'\r'):
00245                     line = line[:-1] + b'\n'
00246                 if not line:
00247                     break
00248                 if mangle_from_ and line.startswith(b'From '):
00249                     line = b'>From ' + line[5:]
00250                 line = line.replace(b'\n', linesep)
00251                 target.write(line)
00252         else:
00253             raise TypeError('Invalid message type: %s' % type(message))
00254 

Here is the call graph for this function:

Here is the caller graph for this function:

def mailbox.Mailbox._string_to_bytes (   self,
  message 
) [private]

Definition at line 201 of file mailbox.py.

00201 
00202     def _string_to_bytes(self, message):
00203         # If a message is not 7bit clean, we refuse to handle it since it
00204         # likely came from reading invalid messages in text mode, and that way
00205         # lies mojibake.
00206         try:
00207             return message.encode('ascii')
00208         except UnicodeError:
00209             raise ValueError("String input must be ASCII-only; "
00210                 "use bytes or a Message instead")

Here is the caller graph for this function:

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

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

Definition at line 46 of file mailbox.py.

00046 
00047     def add(self, message):
00048         """Add message and return assigned key."""
00049         raise NotImplementedError('Method must be implemented by subclass')

Here is the caller graph for this function:

def mailbox.Mailbox.clear (   self)
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:

def mailbox.Mailbox.close (   self)
Flush and close the mailbox.

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

Definition at line 197 of file mailbox.py.

00197 
00198     def close(self):
00199         """Flush and close the mailbox."""
00200         raise NotImplementedError('Method must be implemented by subclass')

Here is the caller graph for this function:

def mailbox.Mailbox.discard (   self,
  key 
)
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:

def mailbox.Mailbox.flush (   self)
Write any pending changes to the disk.

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

Definition at line 185 of file mailbox.py.

00185 
00186     def flush(self):
00187         """Write any pending changes to the disk."""
00188         raise NotImplementedError('Method must be implemented by subclass')

Here is the caller graph for this function:

def mailbox.Mailbox.get (   self,
  key,
  default = None 
)
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 
)
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 
)
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 
)
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 
)
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)
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)
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:

def mailbox.Mailbox.iterkeys (   self)
Return an iterator over keys.

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

Definition at line 102 of file mailbox.py.

00102 
00103     def iterkeys(self):
00104         """Return an iterator over keys."""
00105         raise NotImplementedError('Method must be implemented by subclass')

Here is the caller graph for this function:

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)
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:

def mailbox.Mailbox.lock (   self)
Lock the mailbox.

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

Definition at line 189 of file mailbox.py.

00189 
00190     def lock(self):
00191         """Lock the mailbox."""
00192         raise NotImplementedError('Method must be implemented by subclass')

Here is the caller graph for this function:

def mailbox.Mailbox.pop (   self,
  key,
  default = None 
)
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)
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.Mailbox.remove (   self,
  key 
)
Remove the keyed message; raise KeyError if it doesn't exist.

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

Definition at line 50 of file mailbox.py.

00050 
00051     def remove(self, key):
00052         """Remove the keyed message; raise KeyError if it doesn't exist."""
00053         raise NotImplementedError('Method must be implemented by subclass')

Here is the caller graph for this function:

def mailbox.Mailbox.unlock (   self)
Unlock the mailbox if it is locked.

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

Definition at line 193 of file mailbox.py.

00193 
00194     def unlock(self):
00195         """Unlock the mailbox if it is locked."""
00196         raise NotImplementedError('Method must be implemented by subclass')

def mailbox.Mailbox.update (   self,
  arg = None 
)
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)
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 44 of file mailbox.py.

Definition at line 43 of file mailbox.py.


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