
Gh\c           @   s   d  d l  Z  d  d l Z d  d l m Z d  d l Z y d  d l m Z m Z Wn' e k
 rw d  d l	 m Z m Z n Xd  d l
 m Z d e f d     YZ d   Z d e f d	     YZ d S(
   iN(   t   exc_info(   t   SafeConfigParsert   Error(   t	   LocalEvalt   CustomConfigParserc           B   st   e  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(   c         C   s   t  j |   d  |  _ d  S(   N(   R   t   __init__t   Nonet	   localeval(   t   self(    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyR      s    c         O   s4   |  j  | |  r, |  j | | f | |   S| Sd S(   sd   Same as config.get, but returns the value of `default`
        if there is no such option specified.N(   t
   has_optiont   get(   R   t   sectiont   optiont   defaultt   argst   kwargs(    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyt
   getdefault!   s    c         O   s4   |  j  | |  r, |  j | | f | |   S| Sd S(   sg   Same as config.getint, but returns the value of `default`
        if there is no such option specified.N(   R	   t   getint(   R   R   R   R   R   R   (    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyt   getdefaultint+   s    c         O   s4   |  j  | |  r, |  j | | f | |   S| Sd S(   si   Same as config.getfloat, but returns the value of `default`
        if there is no such option specified.N(   R	   t   getfloat(   R   R   R   R   R   R   (    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyt   getdefaultfloat5   s    c         O   s4   |  j  | |  r, |  j | | f | |   S| Sd S(   sk   Same as config.getboolean, but returns the value of `default`
        if there is no such option specified.N(   R	   t
   getboolean(   R   R   R   R   R   R   (    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyt   getdefaultboolean>   s    c         C   ss   y, |  j  | |  j   } t j | |  SWn@ t j k
 rn } t j t t d | | f  t   d  n Xd S(   sJ   Parses option as the list of values separated
        by the given regexp.s   Bad split regexp '%s': %si   N(	   R
   t   stript   ret   splitt   errort   sixt   reraiseR   R    (   R   R   R   t   separator_ret   valt   e(    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyt   getlistG   s    	c         C   s0   |  j  | |  r( |  j | | | f   S| Sd S(   sa   Same as getlist, but returns the value of `default`
        if there is no such option specified.N(   R	   R    (   R   R   R   R   R   (    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyt   getdefaultlistT   s    c         C   sh   t  j j t  j j g } |  j d d d  } |  j | |  } t  j j |  sd t  j | d  n  | S(   Nt   generalt   metadatas   ~/.offlineimapi  (   t   ost   patht
   expandusert
   expandvarsR   t   apply_xformst   existst   mkdir(   R   t   xformst   dt   metadatadir(    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyt   getmetadatadir]   s    c         C   s   |  j  d  k	 r |  j  St j j t j j g } |  j d d  rg |  j d d  } |  j | |  } n d  } t	 |  |  _  |  j  S(   NR"   t
   pythonfile(
   R   R   R$   R%   R&   R'   R	   R
   R(   R   (   R   R+   R%   (    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyt   getlocalevale   s    c         C   s@   | d } g  |  j    D]% } | j |  r | t |  ^ q S(   s   Returns a list of sections that start with (str) key + " ".

        That is, if key is "Account", returns all section names that
        start with "Account ", but strips off the "Account ".

        For instance, for "Account Test", returns "Test".t    (   t   sectionst
   startswitht   len(   R   t   keyt   x(    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyt   getsectionlistt   s    
c         C   s,   |  j  | |  s( |  j | | |  n  d S(   s   Set a value if it does not exist yet.

        This allows to set default if the user has not explicitly
        configured anything.N(   R	   t   set(   R   R   R   t   value(    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyt   set_if_not_exists   s    c         C   s1   | d k r d Sx | D] } | |  } q W| S(   s  Applies set of transformations to a string.

        Arguments:
         - string: source string; if None, then no processing will
           take place.
         - transforms: iterable that returns transformation function
           on each turn.

        Returns transformed string.N(   R   (   R   t   stringt
   transformst   f(    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyR(      s
    (   t   __name__t
   __module__R   R   R   R   R   R    R!   R.   R0   R7   R:   R(   (    (    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyR      s   		
	
											
c           C   s   d S(   s   Just a constant that won't occur anywhere else.

    This allows us to differentiate if the user has passed in any
    default value to the getconf* functions in ConfigHelperMixin
    derived classes.N(    (    (    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyt   CustomConfigDefault   s    t   ConfigHelperMixinc           B   sq   e  Z d  Z d   Z d   Z d   Z e d  Z e d  Z e d  Z	 e d  Z
 e d  Z e d	  Z RS(
   s  Allow comfortable retrieving of config values pertaining
    to a section.

    If a class inherits from cls:`ConfigHelperMixin`, it needs
    to provide 2 functions:
    - meth:`getconfig` (returning a CustomConfigParser object)
    - and meth:`getsection` (returning a string which represents
      the section to look up).
    All calls to getconf* will then return the configuration values
    for the CustomConfigParser object in the specific section.
    c         G   sW   |  j    | g } | t k r2 | | t |    S| j |  | | t |    Sd S(   s  Returns configuration or default value for option
        that contains in section identified by getsection().

        Arguments:
        - option: name of the option to retrieve;
        - default: governs which function we will call.
          * When CustomConfigDefault is passed, we will call
          the mainfunc.
          * When any other value is passed, we will call
          the defaultfunc and the value of `default` will
          be passed as the third argument to this function.
        - defaultfunc and mainfunc: processing helpers.
        - args: additional trailing arguments that will be passed
          to all processing helpers.
        N(   t
   getsectionR@   t   listt   append(   R   R   R   t   defaultfunct   mainfuncR   t   lst(    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyt   _confighelper_runner   s
    c         C   s   t  d   d S(   s   Returns CustomConfigParser object that we will use
        for all our actions.

        Must be overriden in all classes that use this mix-in.s0   ConfigHelperMixin.getconfig() is to be overridenN(   t   NotImplementedError(   R   (    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyt	   getconfig   s    c         C   s   t  d   d S(   s   Returns name of configuration section in which our
        class keeps its configuration.

        Must be overriden in all classes that use this mix-in.s1   ConfigHelperMixin.getsection() is to be overridenN(   RI   (   R   (    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyRB      s    c         C   s(   |  j  | | |  j   j |  j   j  S(   s   Retrieves string from the configuration.

        Arguments:
         - option: option name whose value is to be retrieved;
         - default: default return value if no such option
           exists.
        (   RH   RJ   R   R
   (   R   R   R   (    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyt   getconf   s    	c         C   s(   |  j  | |  } |  j   j | |  S(   s  Retrieves string from the configuration transforming the result.

        Arguments:
         - option: option name whose value is to be retrieved;
         - xforms: iterable that returns transform functions
           to be applied to the value of the option,
           both retrieved and default one;
         - default: default value for string if no such option
           exists.
        (   RK   RJ   R(   (   R   R   R+   R   R9   (    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyt   getconf_xform   s    c         C   s(   |  j  | | |  j   j |  j   j  S(   s   Retrieves boolean value from the configuration.

        Arguments:
         - option: option name whose value is to be retrieved;
         - default: default return value if no such option
           exists.
        (   RH   RJ   R   R   (   R   R   R   (    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyt   getconfboolean   s    	c         C   s(   |  j  | | |  j   j |  j   j  S(   s   
        Retrieves integer value from the configuration.

        Arguments:
         - option: option name whose value is to be retrieved;
         - default: default return value if no such option
           exists.

        (   RH   RJ   R   R   (   R   R   R   (    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyt
   getconfint  s    c         C   s(   |  j  | | |  j   j |  j   j  S(   s   Retrieves floating-point value from the configuration.

        Arguments:
         - option: option name whose value is to be retrieved;
         - default: default return value if no such option
           exists.
        (   RH   RJ   R   R   (   R   R   R   (    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyt   getconffloat  s    	c         C   s+   |  j  | | |  j   j |  j   j |  S(   sg  Retrieves strings from the configuration and splits it
        into the list of strings.

        Arguments:
         - option: option name whose value is to be retrieved;
         - separator_re: regular expression for separator
           to be used for split operation;
         - default: default return value if no such option
           exists.
        (   RH   RJ   R!   R    (   R   R   R   R   (    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyt   getconflist,  s    (   R>   R?   t   __doc__RH   RJ   RB   R@   RK   RL   RM   RN   RO   RP   (    (    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyRA      s   			
(   R$   R   t   sysR    R   t   ConfigParserR   R   t   ImportErrort   configparsert   offlineimap.localevalR   R   R@   t   objectRA   (    (    (    s2   /usr/share/offlineimap/offlineimap/CustomConfig.pyt   <module>   s   	