mò
­fIc           @   sF  d  Z  d Z d Z d Z d Z d k Z d k Z d k Z d k Z d k	 Z	 d k
 Z
 d k l Z d d d	 d
 g Z d e f d „  ƒ  YZ d f  d „  ƒ  YZ e i d ƒ Z d „  Z d	 f  d „  ƒ  YZ d
 e i f d „  ƒ  YZ d e f d „  ƒ  YZ d f  d „  ƒ  YZ d d „ Z d d „ Z d „  Z e d j o e ƒ  n d S(   sŸ	  MH interface -- purely object-oriented (well, almost)

Executive summary:

import mhlib

mh = mhlib.MH()         # use default mailbox directory and profile
mh = mhlib.MH(mailbox)  # override mailbox location (default from profile)
mh = mhlib.MH(mailbox, profile) # override mailbox and profile

mh.error(format, ...)   # print error message -- can be overridden
s = mh.getprofile(key)  # profile entry (None if not set)
path = mh.getpath()     # mailbox pathname
name = mh.getcontext()  # name of current folder
mh.setcontext(name)     # set name of current folder

list = mh.listfolders() # names of top-level folders
list = mh.listallfolders() # names of all folders, including subfolders
list = mh.listsubfolders(name) # direct subfolders of given folder
list = mh.listallsubfolders(name) # all subfolders of given folder

mh.makefolder(name)     # create new folder
mh.deletefolder(name)   # delete folder -- must have no subfolders

f = mh.openfolder(name) # new open folder object

f.error(format, ...)    # same as mh.error(format, ...)
path = f.getfullname()  # folder's full pathname
path = f.getsequencesfilename() # full pathname of folder's sequences file
path = f.getmessagefilename(n)  # full pathname of message n in folder

list = f.listmessages() # list of messages in folder (as numbers)
n = f.getcurrent()      # get current message
f.setcurrent(n)         # set current message
list = f.parsesequence(seq)     # parse msgs syntax into list of messages
n = f.getlast()         # get last message (0 if no messagse)
f.setlast(n)            # set last message (internal use only)

dict = f.getsequences() # dictionary of sequences in folder {name: list}
f.putsequences(dict)    # write sequences back to folder

f.createmessage(n, fp)  # add message from file f as number n
f.removemessages(list)  # remove messages in list from folder
f.refilemessages(list, tofolder) # move messages in list to other folder
f.movemessage(n, tofolder, ton)  # move one message to a given destination
f.copymessage(n, tofolder, ton)  # copy one message to a given destination

m = f.openmessage(n)    # new open message object (costs a file descriptor)
m is a derived class of mimetools.Message(rfc822.Message), with:
s = m.getheadertext()   # text of message's headers
s = m.getheadertext(pred) # text of message's headers, filtered by pred
s = m.getbodytext()     # text of message's body, decoded
s = m.getbodytext(0)    # text of message's body, not decoded
s   ~/.mh_profiles   ~/Mails   .mh_sequencesiÀ  N(   s   bisectt   MHt   Errort   Foldert   Messagec           B   s   t  Z RS(   N(   t   __name__t
   __module__(    (    (    t"   /mit/python/lib/python2.4/mhlib.pyR   W   s   c           B   s’   t  Z d  Z e e d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z RS(   s<  Class representing a particular collection of folders.
    Optional constructor arguments are the pathname for the directory
    containing the collection, and the MH profile to use.
    If either is omitted or empty a default is used; the default
    directory is taken from the MH profile if it is specified there.c         C   sÚ   | d j o
 t } n t i i | ƒ |  _  | d j o |  i d ƒ } n | p
 t } n t i i	 | ƒ o* | d d j o t i i
 d | ƒ } n t i i | ƒ } t i i | ƒ p t d ‚ n | |  _ d S(   s   Constructor.t   Pathi    t   ~s   MH() path not foundN(   t   profilet   Nonet
   MH_PROFILEt   ost   patht
   expandusert   selft
   getprofilet   PATHt   isabst   joint   isdirR   (   R   R   R	   (    (    R   t   __init__b   s      
  
% c         C   s   d |  i |  i f S(   s   String representation.s
   MH(%r, %r)N(   R   R   R	   (   R   (    (    R   t   __repr__n   s     c         G   s   t  i i d | | ƒ d S(   sA   Routine to print an error.  May be overridden by a derived class.s   MH error: %s
N(   t   syst   stderrt   writet   msgt   args(   R   R   R   (    (    R   t   errorr   s     c         C   s   t  |  i | ƒ S(   s*   Return a profile entry, None if not found.N(   t   picklineR   R	   t   key(   R   R   (    (    R   R   v   s     c         C   s   |  i S(   s9   Return the path (the name of the collection's directory).N(   R   R   (   R   (    (    R   t   getpathz   s     c         C   s9   t  t i i |  i ƒ  d ƒ d ƒ } | p
 d } n | S(   s&   Return the name of the current folder.t   contexts   Current-Foldert   inboxN(   R   R   R   R   R   R   R    (   R   R    (    (    R   t
   getcontext~   s     	 
c         C   sI   t  i i |  i ƒ  d ƒ } t | d ƒ } | i d | ƒ | i
 ƒ  d S(   s#   Set the name of the current folder.R    t   ws   Current-Folder: %s
N(   R   R   R   R   R   t   fnt   opent   fR   R    t   close(   R   R    R&   R$   (    (    R   t
   setcontext…   s
     c         C   ss   g  } |  i ƒ  } xP t i | ƒ D]? } t i i | | ƒ } t i i	 | ƒ o | i
 | ƒ q" q" W| i ƒ  | S(   s*   Return the names of the top-level folders.N(   t   foldersR   R   R   R   t   listdirt   nameR   t   fullnameR   t   appendt   sort(   R   R)   R+   R   R,   (    (    R   t   listfoldersŒ   s      
c   	      C   sÝ   t  i i |  i | ƒ } t  i | ƒ i } | d j o g  Sn g  } t  i
 | ƒ } xx | D]p } t  i i | | ƒ } t  i i | ƒ oB t  i i | | ƒ } | i | ƒ | d } | d j o PqË q[ q[ W| i ƒ  | S(   sc   Return the names of the subfolders in a given folder
        (prefixed with the given folder name).i   i   N(   R   R   R   R   R+   R,   t   statt   st_nlinkt   nlinkst
   subfoldersR*   t   subnamest   subnamet   fullsubnameR   t   name_subnameR-   R.   (	   R   R+   R4   R6   R3   R5   R2   R,   R7   (    (    R   t   listsubfolders—   s$      

c         C   s   |  i d ƒ S(   s<   Return the names of all folders and subfolders, recursively.t    N(   R   t   listallsubfolders(   R   (    (    R   t   listallfolders¯   s     c   
      C   s2  t  i i |  i | ƒ } t  i | ƒ i } | d j o g  Sn g  } t  i
 | ƒ } xÍ | D]Å } | d d j p t | ƒ o q[ n t  i i | | ƒ } t  i i | ƒ or t  i i | | ƒ } | i | ƒ t  i i | ƒ p |  i | ƒ }	 | |	 } n | d } | d j o Pq q[ q[ W| i ƒ  | S(   s>   Return the names of subfolders in a given folder, recursively.i   i    t   ,i   N(   R   R   R   R   R+   R,   R0   R1   R2   R3   R*   R4   R5   t	   isnumericR6   R   R7   R-   t   islinkR:   t   subsubfoldersR.   (
   R   R+   R4   R6   R3   R5   R2   R,   R7   R?   (    (    R   R:   ³   s.       

c         C   s   t  |  | ƒ S(   s0   Return a new Folder object for the named folder.N(   R   R   R+   (   R   R+   (    (    R   t
   openfolderÏ   s     c         C   sh   t  |  i d ƒ } | o  t | ƒ o t | d ƒ } n t } t i	 t i
 i |  i ƒ  | ƒ | ƒ d S(   s@   Create a new folder (or raise os.error if it cannot be created).s   Folder-Protecti   N(   R   R   R	   t   protectR=   t   intt   modet   FOLDER_PROTECTR   t   mkdirR   R   R   R+   (   R   R+   RA   RC   (    (    R   t
   makefolderÓ   s     c         C   s–   t  i i |  i ƒ  | ƒ } xg t  i | ƒ D]V } t  i i | | ƒ } y t  i
 | ƒ Wq+ t  i j
 o |  i d | ƒ q+ Xq+ Wt  i | ƒ d S(   s‡   Delete a folder.  This removes files in the folder but not
        subdirectories.  Raise os.error if deleting the folder itself fails.s   %s not deleted, continuing...N(   R   R   R   R   R   R+   R,   R*   R5   R6   t   unlinkR   t   rmdir(   R   R+   R6   R5   R,   (    (    R   t   deletefolderÜ   s      (   R   R   t   __doc__R
   R   R   R   R   R   R"   R(   R/   R8   R;   R:   R@   RF   RI   (    (    (    R   R    [   s    													s   ^[1-9][0-9]*$c         C   s   t  i |  ƒ d  j	 S(   N(   t   numericprogt   matcht   strR
   (   RM   (    (    R   R=   ë   s    c           B   sò   t  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s'   Class representing a particular folder.c         C   s@   | |  _  | |  _ t i i |  i ƒ  ƒ p t d | ‚ n d S(   s   Constructor.s   no folder %sN(   t   mhR   R+   R   R   R   t   getfullnameR   (   R   RN   R+   (    (    R   R   ñ   s
     		c         C   s   d |  i |  i f S(   s   String representation.s   Folder(%r, %r)N(   R   RN   R+   (   R   (    (    R   R   ø   s     c         G   s   |  i i | Œ  d S(   s   Error message handler.N(   R   RN   R   R   (   R   R   (    (    R   R   ü   s     c         C   s   t  i i |  i i |  i ƒ S(   s'   Return the full pathname of the folder.N(   R   R   R   R   RN   R+   (   R   (    (    R   RO      s     c         C   s   t  i i |  i ƒ  t ƒ S(   s8   Return the full pathname of the folder's sequences file.N(   R   R   R   R   RO   t   MH_SEQUENCES(   R   (    (    R   t   getsequencesfilename  s     c         C   s   t  i i |  i ƒ  t | ƒ ƒ S(   s4   Return the full pathname of a message in the folder.N(   R   R   R   R   RO   RM   t   n(   R   RR   (    (    R   t   getmessagefilename  s     c         C   s   |  i i |  i ƒ S(   s!   Return list of direct subfolders.N(   R   RN   R8   R+   (   R   (    (    R   R8     s     c         C   s   |  i i |  i ƒ S(   s   Return list of all subfolders.N(   R   RN   R:   R+   (   R   (    (    R   R:     s     c         C   s‘   g  } t i } | i } x8 t i |  i ƒ  ƒ D]! } | | ƒ o | | ƒ q. q. Wt	 t
 | ƒ } | i ƒ  | o | d |  _ n
 d |  _ | S(   s€   Return the list of messages currently present in the folder.
        As a side effect, set self.last to the last message (or 0).iÿÿÿÿi    N(   t   messagesRK   RL   R-   R   R*   R   RO   R+   t   mapRB   R.   t   last(   R   R+   RT   RL   R-   (    (    R   t   listmessages  s     		 
	c         C   sß   h  } |  i ƒ  } y t | d ƒ } Wn t j
 o | Sn Xx™ | i ƒ  } | p Pn | i	 d ƒ } t | ƒ d j o! |  i d | | i ƒ  f ƒ n | d i ƒ  } t | d i ƒ  d ƒ i ƒ  } | | | <qB W| S(	   s+   Return the set of sequences for the folder.t   ri   t   :i   s   bad sequence in %s: %si    t    N(   t	   sequencesR   RQ   R,   R%   R&   t   IOErrort   readlinet   linet   splitt   fieldst   lenR   t   stripR   t   IntSett   tolistt   value(   R   R&   R`   Re   R   R[   R,   R^   (    (    R   t   getsequences%  s&     	   !c         C   sÅ   |  i ƒ  } d } xm | i ƒ  D]_ \ } } t	 d d ƒ } | i | ƒ | p t | d ƒ } n | i d | | i ƒ  f ƒ q W| p. y t i | ƒ WqÁ t i j
 o qÁ Xn | i ƒ  d S(   s.   Write the set of sequences back to the folder.R9   RZ   R#   s   %s: %s
N(   R   RQ   R,   R
   R&   R[   t	   iteritemsR   t   seqRc   t   st   fromlistR%   R   t   tostringR   RG   R   R'   (   R   R[   Rh   R&   Ri   R   R,   (    (    R   t   putsequences9  s        !	c         C   sG   |  i ƒ  } y t | d ƒ SWn" t t f j
 o t d ‚ n Xd S(   s<   Return the current message.  Raise Error when there is none.t   curs   no cur messageN(   R   Rf   t   seqst   maxt
   ValueErrort   KeyErrorR   (   R   Rn   (    (    R   t
   getcurrentJ  s     c         C   s#   t  |  i ƒ  d t | ƒ d ƒ d S(   s   Set the current message.Rm   i    N(   t
   updatelineR   RQ   RM   RR   (   R   RR   (    (    R   t
   setcurrentR  s     c         C   s…  |  i ƒ  } | p t d |  i ‚ n | d j o | Sn | i d ƒ } | d j oÚ| |  d | | d } } } | d  d j o | d  | d } } n t | ƒ p t d | ‚ n y t | ƒ } Wn% t t f j
 o t | ƒ } n Xy |  i | | ƒ } Wn¡ t j
 o• }
 |  i ƒ  }	 | |	 j o/ |
 p d | }
 n t |
 t i ƒ  d	 ‚ n |	 | } | p t d
 | ‚ n | d j o | | Sq.| |  Sq2X| p | d j o
 d } qÔn | d j o+ t | | ƒ } | t d | | ƒ | !Sq2t | | d ƒ } | | | | !Sn | i d ƒ } | d j o |  i | |  | ƒ } |  i | | d | ƒ } t | | d ƒ } t | | ƒ } | | | !} | p t d | ‚ n | Sn y |  i | | ƒ } WnX t j
 oL }
 |  i ƒ  }	 | |	 j o" |
 p d | }
 n t |
 ‚ n |	 | SnE X| | j o/ t | ƒ o t d | ‚ qt d | ‚ n | g Sd S(   sä   Parse an MH sequence specification into a message list.
        Attempt to mimic mh-sequence(5) as close as possible.
        Also attempt to mimic observed behavior regarding which
        conditions cause which error messages.s   no messages in %st   allRY   i    R9   i   s   -+s   bad message list %si   s   sequence %s emptyt   -t   prevRV   s   message %d doesn't exists   no %s messageN(   Rw   s   last(    R   RW   Ru   R   R+   Rh   t   findt   it   headt   dirt   tailR=   RB   t   countRp   t   OverflowErrorRa   t   _parseindext   anchorR   Rf   Rn   R   t   exc_infot   msgst   bisectRo   t   begint   endt   jRX   RR   (   R   Rh   Ru   R„   RR   R…   R‚   R|   Rz   Rn   R   R}   Ry   R†   RX   R€   R{   (    (    R   t   parsesequenceV  s~      
c         C   sl  t  | ƒ o5 y t | ƒ SWqB t t f j
 o t i SqB Xn | d j o |  i ƒ  Sn | d j o | d Sn | d j o | d Sn | d j oJ |  i ƒ  } t | | ƒ } y | | SWqæ t j
 o t d ‚ qæ Xn | d	 j ol |  i ƒ  } t | | d
 ƒ } | d j o t d ‚ n y | | d
 SWq_t j
 o t d ‚ q_Xn t d ‚ d S(   s7   Internal: parse a message number (or cur, first, etc.).Rm   t   .t   firsti    RV   iÿÿÿÿt   nexts   no next messageRw   i   s   no prev messageN(   s   curRˆ   (   R=   Rh   RB   R~   Rp   R   t   maxintR   Rr   Ru   RR   Rƒ   Ry   t
   IndexErrorR   R
   (   R   Rh   Ru   Ry   RR   (    (    R   R   ª  s:     c         C   s   t  |  | ƒ S(   s+   Open a message -- returns a Message object.N(   R   R   RR   (   R   RR   (    (    R   t   openmessageÉ  s     c         C   s  g  } g  } x© | D]¡ } |  i | ƒ } |  i d t | ƒ ƒ } y t	 i
 | ƒ Wn t	 i j
 o n Xy t	 i | | ƒ Wn% t	 i j
 o } | i | ƒ q X| i | ƒ q W| o |  i | ƒ n | o= t | ƒ d j o t	 i | d ‚ qt	 i d | f ‚ n d S(   s2   Remove one or more messages -- may raise os.error.R<   i   i    s   multiple errors:N(   t   errorst   deletedt   listRR   R   RS   R   RM   t	   commapathR   RG   R   t   renameR   R-   t   removefromallsequencesRa   (   R   R   RŽ   R‘   R   RR   R   R   (    (    R   t   removemessagesÍ  s,      i    c         C   s’  g  } h  }
 xü | D]ô } | i ƒ  d } |  i | ƒ }	 | i | ƒ } y t i |	 | ƒ Wn’ t i j
 oƒ y! t i |	 | ƒ t i |	 ƒ Wqð t t i f j
 oC } | i | ƒ y t i | ƒ Wq t i j
 o q Xq qð Xn X| i | ƒ | |
 | <q W|
 o8 | o | i |  |
 i ƒ  ƒ n |  i |
 i ƒ  ƒ n | o= t | ƒ d j o t i | d ‚ qŽt i d | f ‚ n d S(   s_   Refile one or more messages -- may raise os.error.
        'tofolder' is an open folder object.i   i    s   multiple errors:N(   RŽ   t   refiledR   RR   t   tofoldert   getlastt   tonR   RS   R   t   topathR   R’   R   t   shutilt   copy2RG   R\   R   R-   t   setlastt   keepsequencest   _copysequencest   itemsR“   t   keysRa   (   R   R   R–   R   R™   RŽ   RR   R˜   R   R   R•   (    (    R   t   refilemessagesæ  s>      c         C   sä   | i ƒ  } |  i ƒ  }
 d } x§ | i ƒ  D]™ \ } } y |
 | }	 d } Wn t j
 o g  }	 d } n Xx8 | D]0 \ } } | | j o |	 i | ƒ d } qt qt W| o |	 o |	 |
 | <q+ q+ W| o |  i |
 ƒ n d S(   s.   Helper for refilemessages() to copy sequences.i    i   N(   t
   fromfolderRf   t   fromsequencesR   t   tosequencest   changedRŸ   R+   Rh   t   toseqt   newRq   t   refileditemst   fromnR˜   R-   Rl   (   R   R¢   R¨   R©   Rh   R¥   R£   R˜   R§   R¦   R¤   R+   (    (    R   Rž   	  s*      

 c   	      C   s+  |  i | ƒ } t | ƒ } | i ƒ  ~ | i | ƒ } | i d | ƒ } y t i | | ƒ Wn t i j
 o n Xy t i | | ƒ Wn‰ t i j
 oz d } z' | i d ƒ t i | | ƒ d } Wd | p. y t i | ƒ Wqt i j
 o qXn Xt i | ƒ n X|  i | g ƒ d S(   sa   Move one message over a specific destination message,
        which may or may not already exist.s   ,%di    i   N(   R   RS   RR   R   R%   R&   R'   R–   R˜   R™   t   backuptopathR   R’   R   t   okRœ   R
   Rš   R›   RG   R“   (	   R   RR   R–   R˜   R™   R«   R&   Rª   R   (    (    R   t   movemessage  s6     

 
c   	      C   sá   |  i | ƒ } t | ƒ } | i ƒ  ~ | i | ƒ } | i d | ƒ } y t i | | ƒ Wn t i j
 o n Xd } z' | i d ƒ t i | | ƒ d } Wd | p. y t i | ƒ WqÜ t i j
 o qÜ Xn Xd S(   sa   Copy one message over a specific destination message,
        which may or may not already exist.s   ,%di    i   N(   R   RS   RR   R   R%   R&   R'   R–   R˜   R™   Rª   R   R’   R   R«   Rœ   R
   Rš   R›   RG   (	   R   RR   R–   R˜   R™   R«   R&   Rª   R   (    (    R   t   copymessage>  s,     

 c   	      C   sï   |  i | ƒ } |  i d | ƒ } y t i | | ƒ Wn t i j
 o n Xd } d d } zS t
 | d ƒ } x- | i | ƒ } | p Pn | i | ƒ qt W| i ƒ  d } Wd | p. y t i | ƒ Wqê t i j
 o qê Xn Xd S(   s3   Create a message, with text from the open file txt.s   ,%di    i   i   R#   i   N(   R   RS   RR   R   t
   backuppathR   R’   R   R«   t   BUFSIZER%   R&   t   txtt   readt   bufR   R'   RG   (	   R   RR   R°   R¯   R«   R&   R²   R®   R   (    (    R   t   createmessageX  s2     
  

 c         C   sÐ   t  |  d ƒ o |  i | j o
 |  ` n |  i ƒ  } d } xu | i ƒ  D]g \ } } | d j o qI n xD | D]< } | | j o) | i | ƒ d } | p | | =q¬ qp qp WqI W| o |  i | ƒ n d S(   s`   Remove one or more messages from all sequences (including last)
        -- but not from 'cur'!!!RV   i    Rm   i   N(   t   hasattrR   RV   R   Rf   R[   R¥   RŸ   R+   Rh   RR   t   removeRl   (   R   R   R+   Rh   R¥   RR   R[   (    (    R   R“   r  s$      
  c         C   s%   t  |  d ƒ p |  i ƒ  n |  i S(   s   Return the last message number.RV   N(   R´   R   RW   RV   (   R   (    (    R   R—   …  s     c         C   s8   | d j o t |  d ƒ o
 |  `  q4 n
 | |  _  d S(   s   Set the last message number.RV   N(   RV   R
   R´   R   (   R   RV   (    (    R   Rœ   ‹  s
     (   R   R   RJ   R   R   R   RO   RQ   RS   R8   R:   RW   Rf   Rl   Rr   Rt   R‡   R   R   R”   R¡   Rž   R¬   R­   R³   R“   R—   Rœ   (    (    (    R   R   î   s4    														T			#		 				c           B   sG   t  Z d  d „ Z d „  Z d  d „ Z d d „ Z d „  Z d „  Z RS(   Nc         C   sX   | |  _ | |  _ | d j o" | i | ƒ } t	 | d ƒ } n t
 i i |  | ƒ d S(   s   Constructor.RX   N(   R&   R   t   folderRR   t   numbert   fpR
   RS   R   R%   t	   mimetoolsR   R   (   R   R&   RR   R¸   R   (    (    R   R   •  s     		c         C   s   d t  |  i ƒ |  i f S(   s   String representation.s   Message(%s, %s)N(   t   reprR   R¶   R·   (   R   (    (    R   R   ž  s     c         C   s±   | d j o d i |  i ƒ Sn g  } d } xt |  i D]i } | d i ƒ  p: | i d ƒ } | d j o | | |  i
 ƒ  ƒ } qˆ n | o | i | ƒ q7 q7 Wd i | ƒ S(   sá   Return the message's header text as a string.  If an
        argument is specified, it is used as a filter predicate to
        decide which headers to return (its argument is the header
        name converted to lower case).R9   i    RY   N(   t   predR
   R   R   t   headerst   hitR^   t   isspaceRx   Ry   t   lowerR-   (   R   R»   R½   Ry   R¼   R^   (    (    R   t   getheadertext¢  s     
  i   c         C   s{   |  i i |  i ƒ |  i ƒ  } | p | d j o |  i i ƒ  Sn d k l } | ƒ  } t
 i |  i | | ƒ | i ƒ  S(   sß   Return the message's body text as string.  This undoes a
        Content-Transfer-Encoding, but does not interpret other MIME
        features (e.g. multipart messages).  To suppress decoding,
        pass 0 as an argument.R9   t   7bitt   8bitt   binary(   s   StringION(   R9   RÁ   RÂ   RÃ   (   R   R¸   t   seekt   startofbodyt   getencodingt   encodingt   decodeR±   t   StringIOt   outputR¹   t   getvalue(   R   RÈ   RÉ   RÇ   RÊ   (    (    R   t   getbodytext³  s     	c         C   sÝ   |  i ƒ  d j o t d ‚ n |  i d ƒ } | p t d ‚ n |  i i |  i ƒ t i	 |  i ƒ } | i | ƒ g  } xQ | i ƒ  oC d |  i d t | ƒ f } t |  i | | ƒ } | i | ƒ q~ W| i ƒ  | S(   s¥   Only for multipart messages: return the message's body as a
        list of SubMessage objects.  Each submessage object behaves
        (almost) as a Message object.t	   multiparts   Content-Type is not multipart/*t   boundarys"   multipart/* without boundary params   %s.%ri   N(   R   t   getmaintypeR   t   getparamt   bdryR¸   RÄ   RÅ   t	   multifilet	   MultiFilet   mft   pusht   partsRŠ   R·   Ra   RR   t
   SubMessageR¶   t   partR-   t   pop(   R   RÑ   RÔ   RR   RÖ   RØ   (    (    R   t   getbodypartsÁ  s"      
c         C   s/   |  i ƒ  d j o |  i ƒ  Sn |  i ƒ  Sd S(   s3   Return body, either a string or a list of messages.RÍ   N(   R   RÏ   RÚ   RÌ   (   R   (    (    R   t   getbodyÕ  s     (	   R   R   R
   R   R   RÀ   RÌ   RÚ   RÛ   (    (    (    R   R   “  s   			R×   c           B   s8   t  Z d „  Z d „  Z d d „ Z d „  Z d „  Z RS(   Nc         C   sm   t  i |  | | | ƒ |  i ƒ  d j o t  i |  ƒ |  _ n t  i	 |  ƒ |  _ t  i	 |  d d ƒ|  _
 d S(   s   Constructor.RÍ   RÈ   i    N(   R   R   R   R&   RR   R¸   RÏ   RÚ   t   bodyRÌ   t   bodyencoded(   R   R&   RR   R¸   (    (    R   R   ß  s     c         C   s.   |  i |  i |  i } } } d | | | f S(   s   String representation.s   SubMessage(%s, %s, %s)N(   R   R¶   R·   R¸   R&   RR   (   R   R¸   RR   R&   (    (    R   R   é  s     i   c         C   s=   | p |  i Sn t |  i ƒ t d ƒ j o |  i Sn d  S(   NR9   (   RÈ   R   RÝ   t   typeRÜ   (   R   RÈ   (    (    R   RÌ   î  s    c         C   s+   t  |  i ƒ t  g  ƒ j o |  i Sn d  S(   N(   RÞ   R   RÜ   (   R   (    (    R   RÚ   ô  s    c         C   s   |  i S(   N(   R   RÜ   (   R   (    (    R   RÛ   ø  s    (   R   R   R   R   RÌ   RÚ   RÛ   (    (    (    R   R×   Ý  s
   	
		Rc   c           B   s§   t  Z d  Z e d d d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d	 „  Z
 d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s÷  Class implementing sets of integers.

    This is an efficient representation for sets consisting of several
    continuous ranges, e.g. 1-100,200-400,402-1000 is represented
    internally as a list of three pairs: [(1,100), (200,400),
    (402,1000)].  The internal representation is always kept normalized.

    The constructor has up to three arguments:
    - the string used to initialize the set (default ''),
    - the separator between ranges (default ',')
    - the separator between begin and end of a range (default '-')
    The separators must be strings (not regexprs) and should be different.

    The tostring() function yields a string that can be passed to another
    IntSet constructor; __repr__() is a valid IntSet constructor itself.
    R<   Rv   c         C   s7   g  |  _ | |  _ | |  _ | o |  i | ƒ n d  S(   N(   R   t   pairst   sept   rngt   datat
   fromstring(   R   Râ   Rà   Rá   (    (    R   R     s
    			 c         C   s   g  |  _ d  S(   N(   R   Rß   (   R   (    (    R   t   reset  s    c         C   s   t  |  i | i ƒ S(   N(   t   cmpR   Rß   t   other(   R   Ræ   (    (    R   t   __cmp__  s    c         C   s   t  |  i ƒ S(   N(   t   hashR   Rß   (   R   (    (    R   t   __hash__  s    c         C   s   d |  i ƒ  |  i |  i f S(   Ns   IntSet(%r, %r, %r)(   R   Rk   Rà   Rá   (   R   (    (    R   R   "  s    c         C   s¨   |  i i ƒ  d } xŽ | t |  i ƒ j  ow |  i | d \ } } |  i | \ } } | | d j o. | t	 | | ƒ f g |  i | d | d +q | d } q Wd  S(   Ni   (
   R   Rß   R.   Ry   Ra   t   alot   ahit   blot   bhiRo   (   R   Rí   Rê   Ry   Rë   Rì   (    (    R   t	   normalize%  s     .c         C   s€   d } xs |  i D]h \ } } | | j o t | ƒ } n t | ƒ |  i t | ƒ } | o | |  i | } q | } q W| S(   NR9   (	   Ri   R   Rß   t   lot   hiRº   t   tRá   Rà   (   R   Rï   Ri   Rð   Rñ   (    (    R   Rk   0  s    
   
c         C   sA   g  } x4 |  i D]) \ } } t | | d ƒ } | | } q W| S(   Ni   (   t   lR   Rß   Rï   Rð   t   ranget   m(   R   Rï   Rô   Rò   Rð   (    (    R   Rd   9  s    
 c         C   s"   x | D] } |  i | ƒ q Wd  S(   N(   R   Ry   R   R-   (   R   R   Ry   (    (    R   Rj   @  s     c         C   s   t  ƒ  } |  i | _ | S(   N(   Rc   R§   R   Rß   (   R   R§   (    (    R   t   cloneD  s    	c         C   s   |  i d d S(   Ni    (   R   Rß   (   R   (    (    R   t   minI  s    c         C   s   |  i d d S(   Niÿÿÿÿ(   R   Rß   (   R   (    (    R   Ro   L  s    c         C   sD   x= |  i D]2 \ } } | | j o
 | j n o t Sq
 q
 Wt S(   N(   R   Rß   Rï   Rð   t   xt   Truet   False(   R   R÷   Rï   Rð   (    (    R   t   containsO  s
    
  c         C   s‡  xt  t |  i ƒ ƒ D]ð } |  i | \ } } | | j  oµ | d | j o | | f |  i | <n |  i i | | | f ƒ | d j o_ | d |  i | d d j o? |  i | d d |  i | d f g |  i | d | d +n d  Sn | | j o d  Sq q Wt |  i ƒ d } | d j oC |  i | \ } } | d | j o | | f |  i | <d  Sqmn |  i i	 | | f ƒ d  S(   Ni   i    (
   Ró   Ra   R   Rß   Ry   Rï   Rð   R÷   t   insertR-   (   R   R÷   Ry   Rï   Rð   (    (    R   R-   T  s&     -?c         C   s9   | | j o d  Sn |  i i | | f ƒ |  i ƒ  d  S(   N(   t   xlot   xhiR   Rß   R-   Rî   (   R   Rü   Rý   (    (    R   t   addpairm  s     c         C   s  g  } xÞ | i |  i ƒ D]Ê } g  } x9 | i |  i ƒ D]% } | i	 ƒ  } | i t | ƒ ƒ q8 Wt | ƒ d j o | i | d | d f ƒ q t | ƒ d j o4 | d | d j o | i | d | d f ƒ q t d ‚ q W|  i | |  _ |  i ƒ  d  S(   Ni   i    i   s   bad data passed to IntSet(   R§   Râ   R_   R   Rà   RØ   R   Rá   t   subpRb   Ri   R-   RB   Ra   Rp   Rß   Rî   (   R   Râ   Rÿ   R   Ri   RØ   R§   (    (    R   Rã   r  s      ((   R   R   RJ   R
   R   Rä   Rç   Ré   R   Rî   Rk   Rd   Rj   Rõ   Rö   Ro   Rú   R-   Rþ   Rã   (    (    (    R   Rc   ü  s"    															i   c         C   sï   y t  |  d ƒ } Wn t j
 o d  Sn Xt i | ƒ d } t i	 | | o t i ƒ } xŒ | i ƒ  } | p Pn | i | ƒ o_ | t | ƒ d } x: | i ƒ  } | p | d i ƒ  o Pn | | } qž W| i ƒ  Sq_ q_ Wd  S(   NRX   RY   i   i    (   R%   t   fileR&   R\   R
   t   ret   escapeR   t   patt   compilet   casefoldt
   IGNORECASEt   progR]   R^   RL   Ra   t   textR¾   Rb   (   R   R   R  R  R&   R  R  R^   (    (    R   R   …  s*    	     c         C   sl  y) t  |  d ƒ } | i ƒ  } | i ƒ  Wn t j
 o g  } n Xt i | ƒ d } t i | | o t i ƒ }
 | d  j o
 d  } n d | | f } xy t t | ƒ ƒ D]G } | | } |
 i | ƒ o' | d  j o | | =n | | | <Pq® q® W| d  j	 o | i | ƒ n |  d }	 t  |	 d ƒ } x | D] } | i | ƒ q7W| i ƒ  t i |	 |  ƒ d  S(   NRX   s   :(.*)
s   %s: %s
R   R#   (   R%   R   R&   t	   readlinest   linesR'   R\   R  R  R   R  R  R  R  R  Re   R
   t   newlineRó   Ra   Ry   R^   RL   R-   t   tempfileR   R   R’   (   R   R   Re   R  R  R&   Ry   R  R
  R  R  R^   (    (    R   Rs   ™  s8    
 

	
 
c    	      C   s  t  i d ƒ t ƒ  a d „  }  |  d ƒ |  d ƒ d d d d d	 d
 g } x | D] } |  d | f ƒ qR W|  d ƒ |  d ƒ t i d ƒ a |  d ƒ |  d ƒ |  d ƒ t i	 ƒ  } t d d ƒ i ƒ  | d <| GHt i | ƒ |  d ƒ x% t | ƒ D] } |  d | f ƒ qü W|  d ƒ t i ƒ  } t i | ƒ a |  d ƒ x· d d d d d d d d d d  d! d" d# d$ d% d& d' d( d) g D]v } y |  d* | f ƒ Wn t j
 o } d+ G| GHn Xt  i d, | f ƒ i ƒ  } t t | i ƒ  ƒ } | Gd- GHq†W|  d. ƒ d  S(/   Ns   rm -rf $HOME/Mail/@testc         C   s   |  GHt |  ƒ GHd  S(   N(   Ri   t   eval(   Ri   (    (    R   t   do¿  s    s   mh.listfolders()s   mh.listallfolders()s   @tests   @test/test1s   @test/test2s   @test/test1/test11s   @test/test1/test12s   @test/test1/test11/test111s   mh.makefolder(%r)s   mh.listsubfolders('@test')s   mh.listallsubfolders('@test')s   f.listsubfolders()s   f.listallsubfolders()s   f.getsequences()s
   1-10 12-20RZ   t   foos   mh.deletefolder(%r)s   mh.getcontext()s   f.getcurrent()R‰   RV   Rm   Rˆ   Rw   RŠ   s   first:3s   last:3s   cur:3s   cur:-3s   prev:3s   next:3s   1:3s   1:-3s   100:3s   100:-3s   10000:3s   10000:-3Ru   s   f.parsesequence(%r)s   Error:s   pick %r 2>/dev/nulls   <-- picks   f.listmessages()(   R   t   systemR    RN   R  t   testfoldersRñ   R@   R&   Rf   Rn   Rc   Rd   Rl   t   reversedR"   R    Rh   R   R   t   popenR±   t   stuffRU   RB   R_   R   (	   R  Rh   R  R   R   R  Rñ   R    Rn   (    (    R   t   test»  sL     		

  





  

@ t   __main__(   RJ   R   R   RP   RD   R   R   R  R¹   RÒ   Rš   Rƒ   t   __all__t	   ExceptionR   R    R  RK   R=   R   R   R×   Rc   R   Rs   R  R   (   R¹   Rc   R   R   Rš   R  R  R=   R   RD   R   R  R   RK   R   RP   Rs   Rƒ   R    RÒ   R×   R   R   (    (    R   t   ?6   s4   							ÿ ¦J‰"	*