ó
Gh\c           @   sp   d  d l  m Z d  d l m Z d  d l m Z d  d l m Z d  d l Z d  d l	 Td e f d „  ƒ  YZ
 d S(	   iÿÿÿÿ(   t   folder(   t   getglobalui(   t   OfflineImapError(   t   BaseRepositoryN(   t   *t   MaildirRepositoryc           B   s‰   e  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 RS(   c         C   sá   t  j |  | | ƒ |  j ƒ  |  _ d |  _ t ƒ  |  _ |  j d t	 |  j
 ƒ  ƒ ƒ g  |  _ t j j |  j ƒ sˆ t j |  j d ƒ n  t ƒ  |  _ xF d D]> } d | } |  j | d ƒ } | d k	 r› | |  j | <q› q› Wd S(   sw   Initialize a MaildirRepository object.  Takes a path name
        to the directory holding all the Maildir directories.s(   MaildirRepository initialized, sep is %siÀ  t   abcdefghijklmnopqrstuvwxyzt   customflag_N(   R   t   __init__t   getlocalroott   roott   Nonet   foldersR   t   uit   debugt   reprt   getsept   folder_atimest   ost   patht   isdirt   makedirst   dictt   keyword2chart   getconf(   t   selft	   reposnamet   accountt   ct   confkeyt   keyword(    (    s8   /usr/share/offlineimap/offlineimap/repository/Maildir.pyR      s    		
c         C   s}   t  j j |  j | ƒ } t  j j | d ƒ } t  j j | d ƒ } | t  j j | ƒ t  j j | ƒ f } |  j j | ƒ d S(   s<   Store the atimes of a folder's new|cur in self.folder_atimest   newt   curN(   R   R   t   joinR
   t   getatimeR   t   append(   R   t
   foldernamet   pR   R    t   atimes(    (    s8   /usr/share/offlineimap/offlineimap/repository/Maildir.pyt   _append_folder_atimes2   s
    'c         C   s¥   |  j  d t ƒ s d Sxˆ |  j D]} \ } } } t j j | d ƒ } t j j | d ƒ } t j | | t j j | ƒ f ƒ t j | | t j j | ƒ f ƒ q  Wd S(   sq   Sets folders' atime back to their values after a sync

        Controlled by the 'restoreatime' config parameter.t   restoreatimeNR   R    (   t   getconfbooleant   FalseR   R   R   R!   t   utimet   getmtime(   R   t   dirpatht	   new_atimet	   cur_atimet   new_dirt   cur_dir(    (    s8   /usr/share/offlineimap/offlineimap/repository/Maildir.pyt   restore_atime;   s    "c         C   s(   t  j j t  j j g } |  j d | ƒ S(   Nt   localfolders(   R   R   t
   expandusert
   expandvarst   getconf_xform(   R   t   xforms(    (    s8   /usr/share/offlineimap/offlineimap/repository/Maildir.pyR	   I   s    c         C   s   |  j  j d | ƒ d  S(   Nt   maildir(   R   R   (   R   t   msg(    (    s8   /usr/share/offlineimap/offlineimap/repository/Maildir.pyR   M   s    c         C   s   |  j  d d ƒ j ƒ  S(   Nt   sept   .(   R   t   strip(   R   (    (    s8   /usr/share/offlineimap/offlineimap/repository/Maildir.pyR   P   s    c         C   s    t  |  j ƒ d k r |  j Sd  S(   Ni    (   t   lenR   R   (   R   (    (    s8   /usr/share/offlineimap/offlineimap/repository/Maildir.pyt   getkeywordmapS   s    c         C   sÓ  |  j  j |  | ƒ |  j j r# d St j j t j j |  j | ƒ ƒ } |  j	 ƒ  d k rŽ x2 | j
 d ƒ D] } | d k si t d ƒ ‚ qi Wn  | j d ƒ d k s¯ t d	 ƒ ‚ | j d ƒ sË t d
 ƒ ‚ |  j d | ƒ y t j | d ƒ WnK t k
 r=} | j d k r7t j j | ƒ r7|  j d | ƒ q>‚  n XxŽ d d d g D]} } y# t j t j j | | ƒ d ƒ WqNt k
 rÊ} | j d k rÄt j j | ƒ rÄ|  j d | | f ƒ qË‚  qNXqNWd S(   s  Create new Maildir folder if necessary

        This will not update the list cached in getfolders(). You will
        need to invoke :meth:`forgetfolders` to force new caching when
        you are done creating folders yourself.

        :param foldername: A relative mailbox name. The maildir will be
            created in self.root+'/'+foldername. All intermediate folder
            levels will be created if they do not exist yet. 'cur',
            'tmp', and 'new' subfolders will be created in the maildir.
        Nt   /R   R    t   tmpsg   When using nested folders (/ as a Maildir separator), folder names may not contain 'new', 'cur', 'tmp'.s   ../iÿÿÿÿs    Folder names may not contain ../s!   Folder names may not begin with /s!   makefolder: calling makedirs '%s'iÀ  i   s$   makefolder: '%s' already a directorys&   makefolder: '%s' already has subdir %s(   R   R    R@   (   R   t
   makefolderR   t   dryrunR   R   t   abspathR!   R
   R   t   splitt   AssertionErrort   findt
   startswithR   R   t   OSErrort   errnoR   t   mkdir(   R   R$   t	   full_patht	   componentt   et   subdir(    (    s8   /usr/share/offlineimap/offlineimap/repository/Maildir.pyRA   V   s2    $!!#!	c         C   s   |  j  j d | ƒ d  S(   Ns%   NOT YET IMPLEMENTED: DELETE FOLDER %s(   R   t   warn(   R   R$   (    (    s8   /usr/share/offlineimap/offlineimap/repository/Maildir.pyt   deletefolder…   s    c         C   sM   |  j  ƒ  } x! | D] } | | j k r | Sq Wt d | t j j ƒ ‚ d S(   sá   Return a Folder instance of this Maildir

        If necessary, scan and cache all foldernames to make sure that
        we only return existing folders and that 2 calls with the same
        name will return the same object.s0   getfolder() asked for a nonexisting folder '%s'.N(   t
   getfolderst   nameR   t   ERRORt   FOLDER(   R   R$   R   t   f(    (    s8   /usr/share/offlineimap/offlineimap/repository/Maildir.pyt	   getfolderˆ   s    c   
      C   sd  |  j  d | | f ƒ g  } | r; t j j | | ƒ } n | } |  j  d | ƒ xÛt j | ƒ d g D]Ã} |  j  d | ƒ | d k r« | d k	 r« |  j  d ƒ qi n  | d k rÊ |  j  d	 ƒ qi n  t j j | | ƒ } t j j | ƒ s|  j  d
 ƒ qi n  | r"t j j | | ƒ } n | } t j j t j j | d ƒ ƒ ròt j j t j j | d ƒ ƒ ròt j j t j j | d ƒ ƒ rò|  j  d | ƒ |  j d t ƒ r¾|  j	 | ƒ n  |  j
 ƒ  |  j | |  j ƒ  |  ƒ } | j | ƒ n  |  j ƒ  d k ri | d k ri | j |  j | | ƒ ƒ qi qi W|  j  d t g  | D] }	 |	 j ƒ  ^ qCƒ ƒ | S(   sº   Recursively scan folder 'root'; return a list of MailDirFolder

        :param root: (absolute) path to Maildir root
        :param extension: (relative) subfolder to examine within roots7   _GETFOLDERS_SCANDIR STARTING. root = %s, extension = %ss     toppath = %st    s     dirname = %ss#     skip this entry (already scanned)R    R   R@   s#     skip this entry (Maildir special)s#     skip this entry (not a directory)s     This is maildir folder '%s'.R(   R?   s    _GETFOLDERS_SCANDIR RETURNING %sN(   R    R   R@   (   R   R   R   R!   t   listdirR   R   R)   R*   R'   t   getfoldertypeR
   R   R#   t   extendt   _getfolders_scandirR   t   getname(
   R   R
   t	   extensiont   retvalt   toppatht   dirnamet   fullnameR$   t   fdt   x(    (    s8   /usr/share/offlineimap/offlineimap/repository/Maildir.pyR[   —   sH    	!!! 	'c         C   s.   |  j  d  k r' |  j |  j ƒ |  _  n  |  j  S(   N(   R   R   R[   R
   (   R   (    (    s8   /usr/share/offlineimap/offlineimap/repository/Maildir.pyRQ   Ï   s    c         C   s
   t  j j S(   N(   R    t   Maildirt   MaildirFolder(   R   (    (    s8   /usr/share/offlineimap/offlineimap/repository/Maildir.pyRY   Ô   s    c         C   s   d |  _ d S(   sT   Forgets the cached list of folders, if any.  Useful to run
        after a sync run.N(   R   R   (   R   (    (    s8   /usr/share/offlineimap/offlineimap/repository/Maildir.pyt   forgetfolders×   s    N(   t   __name__t
   __module__R   R'   R2   R	   R   R   R>   RA   RP   RV   R   R[   RQ   RY   Rf   (    (    (    s8   /usr/share/offlineimap/offlineimap/repository/Maildir.pyR      s   									/		8		(   t   offlineimapR    t   offlineimap.uiR   t   offlineimap.errorR   t   offlineimap.repository.BaseR   R   t   statR   (    (    (    s8   /usr/share/offlineimap/offlineimap/repository/Maildir.pyt   <module>   s   
