Back to index

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

List of all members.

Public Member Functions

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

Private Member Functions

def _dump_sequences

Private Attributes

 _locked
 _file
 _next_key

Detailed Description

An MH mailbox.

Definition at line 893 of file mailbox.py.


Constructor & Destructor Documentation

def mailbox.MH.__init__ (   self,
  path,
  factory = None,
  create = True 
)
Initialize an MH instance.

Reimplemented from mailbox.Mailbox.

Definition at line 896 of file mailbox.py.

00896 
00897     def __init__(self, path, factory=None, create=True):
00898         """Initialize an MH instance."""
00899         Mailbox.__init__(self, path, factory, create)
00900         if not os.path.exists(self._path):
00901             if create:
00902                 os.mkdir(self._path, 0o700)
00903                 os.close(os.open(os.path.join(self._path, '.mh_sequences'),
00904                                  os.O_CREAT | os.O_EXCL | os.O_WRONLY, 0o600))
00905             else:
00906                 raise NoSuchMailboxError(self._path)
00907         self._locked = False

Here is the caller graph for this function:


Member Function Documentation

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

Reimplemented from mailbox.Mailbox.

Definition at line 1046 of file mailbox.py.

01046 
01047     def __contains__(self, key):
01048         """Return True if the keyed message exists, False otherwise."""
01049         return os.path.exists(os.path.join(self._path, str(key)))

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:

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

Reimplemented from mailbox.Mailbox.

Definition at line 1050 of file mailbox.py.

01050 
01051     def __len__(self):
01052         """Return a count of messages in the mailbox."""
01053         return len(list(self.keys()))

Here is the call graph for this function:

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

Reimplemented from mailbox.Mailbox.

Definition at line 956 of file mailbox.py.

00956 
00957     def __setitem__(self, key, message):
00958         """Replace the keyed message; raise KeyError if it doesn't exist."""
00959         path = os.path.join(self._path, str(key))
00960         try:
00961             f = open(path, 'rb+')
00962         except IOError as e:
00963             if e.errno == errno.ENOENT:
00964                 raise KeyError('No message with key: %s' % key)
00965             else:
00966                 raise
00967         try:
00968             if self._locked:
00969                 _lock_file(f)
00970             try:
00971                 os.close(os.open(path, os.O_WRONLY | os.O_TRUNC))
00972                 self._dump_message(message, f)
00973                 if isinstance(message, MHMessage):
00974                     self._dump_sequences(message, key)
00975             finally:
00976                 if self._locked:
00977                     _unlock_file(f)
00978         finally:
00979             _sync_close(f)

Here is the call graph for this function:

def mailbox.MH._dump_sequences (   self,
  message,
  key 
) [private]
Inspect a new MHMessage and update sequences appropriately.

Definition at line 1189 of file mailbox.py.

01189 
01190     def _dump_sequences(self, message, key):
01191         """Inspect a new MHMessage and update sequences appropriately."""
01192         pending_sequences = message.get_sequences()
01193         all_sequences = self.get_sequences()
01194         for name, key_list in all_sequences.items():
01195             if name in pending_sequences:
01196                 key_list.append(key)
01197             elif key in key_list:
01198                 del key_list[key_list.index(key)]
01199         for sequence in pending_sequences:
01200             if sequence not in all_sequences:
01201                 all_sequences[sequence] = [key]
01202         self.set_sequences(all_sequences)
01203 

Here is the call graph for this function:

Here is the caller graph for this function:

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

Reimplemented from mailbox.Mailbox.

Definition at line 908 of file mailbox.py.

00908 
00909     def add(self, message):
00910         """Add message and return assigned key."""
00911         keys = self.keys()
00912         if len(keys) == 0:
00913             new_key = 1
00914         else:
00915             new_key = max(keys) + 1
00916         new_path = os.path.join(self._path, str(new_key))
00917         f = _create_carefully(new_path)
00918         closed = False
00919         try:
00920             if self._locked:
00921                 _lock_file(f)
00922             try:
00923                 try:
00924                     self._dump_message(message, f)
00925                 except BaseException:
00926                     # Unlock and close so it can be deleted on Windows
00927                     if self._locked:
00928                         _unlock_file(f)
00929                     _sync_close(f)
00930                     closed = True
00931                     os.remove(new_path)
00932                     raise
00933                 if isinstance(message, MHMessage):
00934                     self._dump_sequences(message, new_key)
00935             finally:
00936                 if self._locked:
00937                     _unlock_file(f)
00938         finally:
00939             if not closed:
00940                 _sync_close(f)
00941         return new_key

Here is the call graph for this function:

Here is the caller graph for this function:

def mailbox.MH.add_folder (   self,
  folder 
)
Create a folder and return an MH instance representing it.

Definition at line 1091 of file mailbox.py.

01091 
01092     def add_folder(self, folder):
01093         """Create a folder and return an MH instance representing it."""
01094         return MH(os.path.join(self._path, folder),
01095                   factory=self._factory)

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:

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

Reimplemented from mailbox.Mailbox.

Definition at line 1073 of file mailbox.py.

01073 
01074     def close(self):
01075         """Flush and close the mailbox."""
01076         if self._locked:
01077             self.unlock()

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:

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

Reimplemented from mailbox.Mailbox.

Definition at line 1069 of file mailbox.py.

01069 
01070     def flush(self):
01071         """Write any pending changes to the disk."""
01072         return

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.MH.get_bytes (   self,
  key 
)
Return a bytes representation or raise a KeyError.

Reimplemented from mailbox.Mailbox.

Definition at line 1007 of file mailbox.py.

01007 
01008     def get_bytes(self, key):
01009         """Return a bytes representation or raise a KeyError."""
01010         try:
01011             if self._locked:
01012                 f = open(os.path.join(self._path, str(key)), 'rb+')
01013             else:
01014                 f = open(os.path.join(self._path, str(key)), 'rb')
01015         except IOError as e:
01016             if e.errno == errno.ENOENT:
01017                 raise KeyError('No message with key: %s' % key)
01018             else:
01019                 raise
01020         try:
01021             if self._locked:
01022                 _lock_file(f)
01023             try:
01024                 return f.read().replace(linesep, b'\n')
01025             finally:
01026                 if self._locked:
01027                     _unlock_file(f)
01028         finally:
01029             f.close()

Here is the call graph for this function:

def mailbox.MH.get_file (   self,
  key 
)
Return a file-like representation or raise a KeyError.

Reimplemented from mailbox.Mailbox.

Definition at line 1030 of file mailbox.py.

01030 
01031     def get_file(self, key):
01032         """Return a file-like representation or raise a KeyError."""
01033         try:
01034             f = open(os.path.join(self._path, str(key)), 'rb')
01035         except IOError as e:
01036             if e.errno == errno.ENOENT:
01037                 raise KeyError('No message with key: %s' % key)
01038             else:
01039                 raise
01040         return _ProxyFile(f)

def mailbox.MH.get_folder (   self,
  folder 
)
Return an MH instance for the named folder.

Definition at line 1086 of file mailbox.py.

01086 
01087     def get_folder(self, folder):
01088         """Return an MH instance for the named folder."""
01089         return MH(os.path.join(self._path, folder),
01090                   factory=self._factory, create=False)

def mailbox.MH.get_message (   self,
  key 
)
Return a Message representation or raise a KeyError.

Reimplemented from mailbox.Mailbox.

Definition at line 980 of file mailbox.py.

00980 
00981     def get_message(self, key):
00982         """Return a Message representation or raise a KeyError."""
00983         try:
00984             if self._locked:
00985                 f = open(os.path.join(self._path, str(key)), 'rb+')
00986             else:
00987                 f = open(os.path.join(self._path, str(key)), 'rb')
00988         except IOError as e:
00989             if e.errno == errno.ENOENT:
00990                 raise KeyError('No message with key: %s' % key)
00991             else:
00992                 raise
00993         try:
00994             if self._locked:
00995                 _lock_file(f)
00996             try:
00997                 msg = MHMessage(f)
00998             finally:
00999                 if self._locked:
01000                     _unlock_file(f)
01001         finally:
01002             f.close()
01003         for name, key_list in self.get_sequences().items():
01004             if key in key_list:
01005                 msg.add_sequence(name)
01006         return msg

Here is the call graph for this function:

def mailbox.MH.get_sequences (   self)
Return a name-to-key-list dictionary to define each sequence.

Definition at line 1108 of file mailbox.py.

01108 
01109     def get_sequences(self):
01110         """Return a name-to-key-list dictionary to define each sequence."""
01111         results = {}
01112         f = open(os.path.join(self._path, '.mh_sequences'), 'r')
01113         try:
01114             all_keys = set(self.keys())
01115             for line in f:
01116                 try:
01117                     name, contents = line.split(':')
01118                     keys = set()
01119                     for spec in contents.split():
01120                         if spec.isdigit():
01121                             keys.add(int(spec))
01122                         else:
01123                             start, stop = (int(x) for x in spec.split('-'))
01124                             keys.update(range(start, stop + 1))
01125                     results[name] = [key for key in sorted(keys) \
01126                                          if key in all_keys]
01127                     if len(results[name]) == 0:
01128                         del results[name]
01129                 except ValueError:
01130                     raise FormatError('Invalid sequence specification: %s' %
01131                                       line.rstrip())
01132         finally:
01133             f.close()
01134         return results

Here is the call graph for this function:

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:

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

Reimplemented from mailbox.Mailbox.

Definition at line 1041 of file mailbox.py.

01041 
01042     def iterkeys(self):
01043         """Return an iterator over keys."""
01044         return iter(sorted(int(entry) for entry in os.listdir(self._path)
01045                                       if entry.isdigit()))

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:

def mailbox.MH.list_folders (   self)
Return a list of folder names.

Definition at line 1078 of file mailbox.py.

01078 
01079     def list_folders(self):
01080         """Return a list of folder names."""
01081         result = []
01082         for entry in os.listdir(self._path):
01083             if os.path.isdir(os.path.join(self._path, entry)):
01084                 result.append(entry)
01085         return result

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

Reimplemented from mailbox.Mailbox.

Definition at line 1054 of file mailbox.py.

01054 
01055     def lock(self):
01056         """Lock the mailbox."""
01057         if not self._locked:
01058             self._file = open(os.path.join(self._path, '.mh_sequences'), 'rb+')
01059             _lock_file(self._file)
01060             self._locked = True

Here is the caller graph for this function:

def mailbox.MH.pack (   self)
Re-name messages to eliminate numbering gaps. Invalidates keys.

Definition at line 1164 of file mailbox.py.

01164 
01165     def pack(self):
01166         """Re-name messages to eliminate numbering gaps. Invalidates keys."""
01167         sequences = self.get_sequences()
01168         prev = 0
01169         changes = []
01170         for key in self.keys():
01171             if key - 1 != prev:
01172                 changes.append((key, prev + 1))
01173                 if hasattr(os, 'link'):
01174                     os.link(os.path.join(self._path, str(key)),
01175                             os.path.join(self._path, str(prev + 1)))
01176                     os.unlink(os.path.join(self._path, str(key)))
01177                 else:
01178                     os.rename(os.path.join(self._path, str(key)),
01179                               os.path.join(self._path, str(prev + 1)))
01180             prev += 1
01181         self._next_key = prev + 1
01182         if len(changes) == 0:
01183             return
01184         for name, key_list in sequences.items():
01185             for old, new in changes:
01186                 if old in key_list:
01187                     key_list[key_list.index(old)] = new
01188         self.set_sequences(sequences)

Here is the call 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.MH.remove (   self,
  key 
)
Remove the keyed message; raise KeyError if it doesn't exist.

Reimplemented from mailbox.Mailbox.

Definition at line 942 of file mailbox.py.

00942 
00943     def remove(self, key):
00944         """Remove the keyed message; raise KeyError if it doesn't exist."""
00945         path = os.path.join(self._path, str(key))
00946         try:
00947             f = open(path, 'rb+')
00948         except IOError as e:
00949             if e.errno == errno.ENOENT:
00950                 raise KeyError('No message with key: %s' % key)
00951             else:
00952                 raise
00953         else:
00954             f.close()
00955             os.remove(path)

Here is the caller graph for this function:

def mailbox.MH.remove_folder (   self,
  folder 
)
Delete the named folder, which must be empty.

Definition at line 1096 of file mailbox.py.

01096 
01097     def remove_folder(self, folder):
01098         """Delete the named folder, which must be empty."""
01099         path = os.path.join(self._path, folder)
01100         entries = os.listdir(path)
01101         if entries == ['.mh_sequences']:
01102             os.remove(os.path.join(path, '.mh_sequences'))
01103         elif entries == []:
01104             pass
01105         else:
01106             raise NotEmptyError('Folder not empty: %s' % self._path)
01107         os.rmdir(path)

def mailbox.MH.set_sequences (   self,
  sequences 
)
Set sequences using the given name-to-key-list dictionary.

Definition at line 1135 of file mailbox.py.

01135 
01136     def set_sequences(self, sequences):
01137         """Set sequences using the given name-to-key-list dictionary."""
01138         f = open(os.path.join(self._path, '.mh_sequences'), 'r+')
01139         try:
01140             os.close(os.open(f.name, os.O_WRONLY | os.O_TRUNC))
01141             for name, keys in sequences.items():
01142                 if len(keys) == 0:
01143                     continue
01144                 f.write(name + ':')
01145                 prev = None
01146                 completing = False
01147                 for key in sorted(set(keys)):
01148                     if key - 1 == prev:
01149                         if not completing:
01150                             completing = True
01151                             f.write('-')
01152                     elif completing:
01153                         completing = False
01154                         f.write('%s %s' % (prev, key))
01155                     else:
01156                         f.write(' %s' % key)
01157                     prev = key
01158                 if completing:
01159                     f.write(str(prev) + '\n')
01160                 else:
01161                     f.write('\n')
01162         finally:
01163             _sync_close(f)

Here is the call graph for this function:

Here is the caller graph for this function:

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

Reimplemented from mailbox.Mailbox.

Definition at line 1061 of file mailbox.py.

01061 
01062     def unlock(self):
01063         """Unlock the mailbox if it is locked."""
01064         if self._locked:
01065             _unlock_file(self._file)
01066             _sync_close(self._file)
01067             del self._file
01068             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

mailbox.MH._file [private]

Definition at line 1057 of file mailbox.py.

mailbox.MH._locked [private]

Definition at line 906 of file mailbox.py.

Definition at line 1180 of file mailbox.py.


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