
    eZ*                     l    d dl Z d dlZd dlmZ d dlmZmZ d dlmZ  G d de      Z	d Z
 G d d	      Zy)
    N)exc_info)ConfigParserError)	LocalEvalc                   T    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d Zy)CustomConfigParserc                 <    t        j                  |        d | _        y )N)r   __init__	localevalselfs    3/usr/share/offlineimap3/offlineimap/CustomConfig.pyr
   zCustomConfigParser.__init__   s    d#    c                 X    | j                  ||      r | j                  ||f|z   i |S |S )zdSame as config.get, but returns the value of `default`
        if there is no such option specified.)
has_optiongetr   sectionoptiondefaultargskwargss         r   
getdefaultzCustomConfigParser.getdefault   s7     ??7F+488gv.5@@@Nr   c                 X    | j                  ||      r | j                  ||f|z   i |S |S )zgSame as config.getint, but returns the value of `default`
        if there is no such option specified.)r   getintr   s         r   getdefaultintz CustomConfigParser.getdefaultint&   s7     ??7F+4;;& 1D 8CFCCNr   c                 X    | j                  ||      r | j                  ||f|z   i |S |S )ziSame as config.getfloat, but returns the value of `default`
        if there is no such option specified.)r   getfloatr   s         r   getdefaultfloatz"CustomConfigParser.getdefaultfloat/   s7     ??7F+ 4==7F"3d":EfEENr   c                 X    | j                  ||      r | j                  ||f|z   i |S |S )zkSame as config.getboolean, but returns the value of `default`
        if there is no such option specified.)r   
getbooleanr   s         r   getdefaultbooleanz$CustomConfigParser.getdefaultboolean8   s7     ??7F+"4??Wf$5$<GGGNr   c                     	 | j                  ||      j                         }t        j                  ||      S # t        j                  $ r"}t        d|d|t               d         d}~ww xY w)zJParses option as the list of values separated
        by the given regexp.zBad split regexp 'z':    N)r   striprespliterrorr   r   )r   r   r   separator_revales         r   getlistzCustomConfigParser.getlistA   se    	:((7F+113C88L#..xx 	:%q*+3:a=: :	:s   58 A-A((A-c                 N    | j                  ||      r | j                  |||f S |S )zaSame as getlist, but returns the value of `default`
        if there is no such option specified.)r   r,   )r   r   r   r   r)   s        r   getdefaultlistz!CustomConfigParser.getdefaultlistL   s/     ??7F+4<<'6<!@AANr   c                 "   t         j                  j                  t         j                  j                  g}| j	                  ddd      }| j                  ||      }t         j                  j                  |      st        j                  |d       |S )Ngeneralmetadataz~/.offlineimapi  )ospath
expanduser
expandvarsr   apply_xformsexistsmkdir)r   xformsdmetadatadirs       r   getmetadatadirz!CustomConfigParser.getmetadatadirU   si    ''$$bgg&8&89OOIz3CD''62ww~~k*HH[%(r   c                 D   | j                   | j                   S t        j                  j                  t        j                  j                  g}| j                  dd      r%| j                  dd      }| j                  ||      }nd }t        |      | _         | j                   S )Nr0   
pythonfile)	r   r2   r3   r4   r5   r   r   r6   r   )r   r9   r3   s      r   getlocalevalzCustomConfigParser.getlocaleval]   s~    >>%>>!''$$bgg&8&89??9l388I|4D$$T62DD"4~~r   c                     |dz   }| j                         D cg c]!  }|j                  |      r|t        |      d # c}S c c}w )zReturns 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". N)sections
startswithlen)r   keyxs      r   getsectionlistz!CustomConfigParser.getsectionlistl   sH     Ci&*mmo &<<$ #c() & 	& &s   &Ac                 P    | j                  ||      s| j                  |||       yy)zSet a value if it does not exist yet.

        This allows to set default if the user has not explicitly
        configured anything.N)r   set)r   r   r   values       r   set_if_not_existsz$CustomConfigParser.set_if_not_existsx   s'     w/HHWfe, 0r   c                 *    |y|D ]
  } ||      } |S )a  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   string
transformsfs       r   r6   zCustomConfigParser.apply_xforms   s)     > 	AvYF	r   N)__name__
__module____qualname__r
   r   r   r   r"   r,   r.   r<   r?   rG   rK   r6   rM   r   r   r   r      s>    	:
&-r   r   c                       y)zJust 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.NrM   rM   r   r   CustomConfigDefaultrU      s     	r   c                   ^    e Zd ZdZd Zd Zd ZefdZefdZ	efdZ
efdZefd	Zefd
Zy)ConfigHelperMixina  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                     | j                         |g}|t        k(  r ||t        |      z    S |j                  |        ||t        |      z    S )a  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.
        )
getsectionrU   listappend)r   r   r   defaultfuncmainfuncr   lsts          r   _confighelper_runnerz&ConfigHelperMixin._confighelper_runner   sR    "  &)))cDJ.00JJwtDz!133r   c                     t        d      )zReturns CustomConfigParser object that we will use
        for all our actions.

        Must be overriden in all classes that use this mix-in.z0ConfigHelperMixin.getconfig() is to be overridenNotImplementedErrorr   s    r   	getconfigzConfigHelperMixin.getconfig        " #7 8 	8r   c                     t        d      )zReturns name of configuration section in which our
        class keeps its configuration.

        Must be overriden in all classes that use this mix-in.z1ConfigHelperMixin.getsection() is to be overridenra   r   s    r   rY   zConfigHelperMixin.getsection   rd   r   c                     | j                  ||| j                         j                  | j                         j                        S )zRetrieves string from the configuration.

        Arguments:
         - option: option name whose value is to be retrieved;
         - default: default return value if no such option
           exists.
        )r_   rc   r   r   r   r   r   s      r   getconfzConfigHelperMixin.getconf   s;     (()-)9)D)D)-)9)=)=? 	?r   c                 f    | j                  ||      }| j                         j                  ||      S )a  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.
        )rh   rc   r6   )r   r   r9   r   rJ   s        r   getconf_xformzConfigHelperMixin.getconf_xform   s.     VW-~~,,UF;;r   c                     | j                  ||| j                         j                  | j                         j                        S )zRetrieves boolean value from the configuration.

        Arguments:
         - option: option name whose value is to be retrieved;
         - default: default return value if no such option
           exists.
        )r_   rc   r"   r!   rg   s      r   getconfbooleanz ConfigHelperMixin.getconfboolean   s=     (()-)9)K)K)-)9)D)DF 	Fr   c                     | j                  ||| j                         j                  | j                         j                        S )z
        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.

        )r_   rc   r   r   rg   s      r   
getconfintzConfigHelperMixin.getconfint   s=     (()-)9)G)G)-)9)@)@B 	Br   c                     | j                  ||| j                         j                  | j                         j                        S )zRetrieves 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.
        )r_   rc   r   r   rg   s      r   getconffloatzConfigHelperMixin.getconffloat  s=     (()-)9)I)I)-)9)B)BD 	Dr   c                     | j                  ||| j                         j                  | j                         j                  |      S )ag  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.
        )r_   rc   r.   r,   )r   r   r)   r   s       r   getconflistzConfigHelperMixin.getconflist  s?     (()-)9)H)H)-)9)A)A<Q 	Qr   N)rQ   rR   rS   __doc__r_   rc   rY   rU   rh   rj   rl   rn   rp   rr   rM   r   r   rW   rW      sX    
4088 ': ? 5H < .A F *= B ,? D 0Qr   rW   )r2   r&   sysr   configparserr   r   offlineimap.localevalr   r   rU   rW   rM   r   r   <module>rw      s8   " 
 	  , +x xv	KQ KQr   