ó
Gh\c           @   s
  d  d l  Z  d  d l Z d  d l Z d  d l m Z d  d l m Z m Z m Z m	 Z	 d  d l
 m Z y d  d l m Z Wn! e k
 r d  d l m Z n Xe ƒ  Z d a d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d S(   iÿÿÿÿN(   t   Lock(   t   listdirt   makedirst   patht   unlink(   t   exc_info(   t   NoSectionErrorc         C   s:   t  j ƒ  t k	 r d  St  t  j |  | | ƒ Wd  QXd  S(   N(   t   _mbnamest
   is_enabledt   Truet   _mbLockt   addAccountFolder(   t   accountnamet   folder_roott
   foldername(    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyt   add$   s    c         C   s%   t  d  k r! t |  | | ƒ a  n  d  S(   N(   R   t   Nonet   _Mbnames(   t   conft   uit   dry_run(    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyt   init.   s    c         C   s0   t  j ƒ  t k r" t  j |  ƒ n
 t  j ƒ  d  S(   N(   R   R   R	   t   prunet   pruneAll(   t   accounts(    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyR   5   s    c           C   s9   t  j ƒ  t k	 r d St  j ƒ  t k	 r5 t  j ƒ  n  d S(   s   Write the mbnames file.N(   R   R   R	   t   get_incrementalt   write(    (    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyR   >   s    c         C   sF   t  j ƒ  t k	 r d St  j |  ƒ t  j ƒ  t k rB t  j ƒ  n  d S(   s    Write intermediate mbnames file.N(   R   R   R	   t   writeIntermediateFileR   R   (   R   (    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyR   J   s
    t   _IntermediateMbnamesc           B   s2   e  Z d  Z d „  Z d „  Z d „  Z d „  Z RS(   s   mbnames data for one account.c         C   sS   | |  _  g  |  _ | |  _ | |  _ | |  _ t j | d | ƒ |  _ | |  _ d  S(   Ns   %s.json(	   R   t   _foldernamest   _accountnamet   _folder_roott   _folderfilterR   t   joint   _patht   _dryrun(   t   selfR   R   t
   mbnamesdirt   folderfilterR   R   (    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyt   __init__Y   s    					c         C   s&   | |  j  k r" |  j  j | ƒ n  d  S(   N(   R   t   append(   R$   R   (    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyR   d   s    c         C   s   |  j  S(   N(   R   (   R$   (    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyt   get_folder_rooth   s    c         C   s¾   g  } xZ |  j  D]O } |  j |  j | ƒ r | j i |  j d 6| j d ƒ d 6|  j d 6ƒ q q W|  j r† |  j j d |  j	 ƒ n4 t
 j |  j	 d d d ƒ } t j | | ƒ Wd	 QXd	 S(
   s/   Write intermediate mbnames file in JSON format.R   s   utf-8R   t   localfolderss   mbnames would write %st   wtt   encodings   UTF-8N(   R   R    R   R(   t   decodeR   R#   R   t   infoR"   t   codecst   opent   jsont   dump(   R$   t   itemlistR   t   intermediateFD(    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyR   k   s    	
	(   t   __name__t
   __module__t   __doc__R'   R   R)   R   (    (    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyR   V   s
   			R   c           B   sb   e  Z d  „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 d	 „  Z RS(
   c         C   sG  | |  _  | |  _ | |  _ d  |  _ i  |  _ d  |  _ d  |  _ d  |  _ d „  |  _	 d „  |  _
 | j ƒ  } t j | j ƒ  d ƒ } d  |  _ d  |  _ d  |  _ d  |  _ y |  j sÁ t | ƒ n  Wn t k
 rÕ n X| |  _ yP|  j  j d d t ƒ |  _ |  j  j d d d d ƒ|  _ | j | j d d ƒ ƒ |  _ | j | j d d	 ƒ ƒ |  _ | j | j d d
 ƒ ƒ |  _ t j t j g } | j | j d d ƒ | ƒ |  _ |  j  j d d ƒ rë| j |  j  j d d ƒ i t d 6ƒ |  _
 n  |  j  j d d ƒ r.| j |  j  j d d ƒ i t d 6ƒ |  _	 n  Wn t k
 rBn Xd  S(   Nc         S   s   t  S(   N(   R	   (   R   R   (    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyt   <lambda>   t    c         S   s   |  d |  d f S(   NR   R   (    (   t   d(    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyR8   Ž   R9   t   mbnamest   enabledt   peritemt   rawi   t   headert   sept   footert   filenamet   sort_keyfunct   reR&   (   t   _configR   R#   R   t   _enabledt   _intermediatest   _incrementalt   _mbnamesdirR"   R    t   _func_sortkeyt   getlocalevalR   R!   t   getmetadatadirt   _peritemt   _headert   _sept   _footerR   t   OSErrort   getdefaultbooleant   Falset   gett   evalt
   expandusert
   expandvarst   apply_xformst
   has_optionRD   R   (   R$   t   configR   R   t	   localevalR%   t   xforms(    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyR'      sP    															(,c         c   sZ   xS t  |  j ƒ D]B } t j |  j | ƒ } t j | ƒ r | d d k r | Vq q Wd  S(   Niûÿÿÿs   .json(   R   RI   R   R!   t   isfile(   R$   t   foo(    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyt   _iterIntermediateFiles³   s    c         C   sB   |  j  r  |  j j d | ƒ n t | ƒ |  j j d | ƒ d  S(   Ns   mbnames would remove %ss
   removed %s(   R#   R   R.   R   (   R$   R   (    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyt   _removeIntermediateFile¹   s    	
c         C   sX   | |  j  k r@ t | | |  j |  j |  j |  j ƒ |  j  | <n  |  j  | j | ƒ d S(   s$   Add foldername entry for an account.N(   RG   R   RI   R    R#   R   R   (   R$   R   R   R   (    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyR   À   s    c         C   s4   |  j  d  k r- |  j j d d t ƒ |  _  n  |  j  S(   NR;   t   incremental(   RH   R   RE   RR   RS   (   R$   (    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyR   Ï   s    	c         C   s   |  j  S(   N(   RF   (   R$   (    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyR   Ö   s    c         C   s{   t  } xO |  j ƒ  D]A } t j | ƒ } | d  } | | k r t } |  j | ƒ q q W| t  k rw |  j j d ƒ n  d  S(   Niûÿÿÿs   no cache file to remove(   RS   R_   R   t   basenameR	   R`   R   R.   (   R$   R   t   removalst   intermediateFileRB   R   (    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyR   Ù   s    
c         C   s(   x! |  j  ƒ  D] } |  j | ƒ q Wd  S(   N(   R_   R`   (   R$   Rd   (    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyR   å   s    c         C   sÓ  g  } xÏ |  j  ƒ  D]Á } yL t j | d d d ƒ. } x$ t j | ƒ D] } | j | ƒ qG WWd  QXWq t t f k
 r  } |  j j	 d | t
 | ƒ f ƒ q t k
 rÓ } |  j j	 | t ƒ  d d | ƒ q Xq W| j d |  j ƒ g  | D] } |  j | ^ qò } |  j r.|  j j d |  j ƒ n¡ ya t j |  j d	 d d ƒ@ } | j |  j ƒ | j |  j j | ƒ ƒ | j |  j ƒ Wd  QXWn= t t f k
 rÎ} |  j j	 | t ƒ  d d
 |  j ƒ n Xd  S(   Nt   rtR,   s   UTF-8s0   could not read intermediate mbnames file '%s':%si   s.   intermediate mbnames file %s not properly readt   keys   mbnames would write %sR+   s$   mbnames file %s not properly written(   R_   R/   R0   R1   t   loadR(   RQ   t   IOErrorR   t   errort   strt	   ExceptionR   t   sortRJ   RM   R#   R.   R"   R   RN   RO   R!   RP   (   R$   R3   Rd   R4   t   itemt   eR:   t   mbnamesFile(    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyR   é   s>    	
 		
c         C   sY   y |  j  | j ƒ  Wn= t t f k
 rT } |  j j | t ƒ  d d |  j ƒ n Xd  S(   Ni   s1   intermediate mbnames file %s not properly written(   RG   R   RQ   Rh   R   Ri   R   R"   (   R$   R   Rn   (    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyR     s    	
(   R5   R6   R'   R_   R`   R   R   R   R   R   R   R   (    (    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyR   €   s   	2								'(   R/   RD   R1   t	   threadingR    t   osR   R   R   R   t   sysR   t   ConfigParserR   t   ImportErrort   configparserR
   R   R   R   R   R   R   R   t   objectR   R   (    (    (    s-   /usr/share/offlineimap/offlineimap/mbnames.pyt   <module>   s$   "		
					*