
    BC                         d Z ddlZddlZddlmZmZmZ ddlmZ 	 ddl	m
Z
mZ ddlmZ  e       Zd Z G d d	e      Z G d
 de      Z G d dee      Zd Z ed       ed       ed       ed       ed       ed       ed       ed       ed       ed      iZ ed       ed      gZ ed       ed      gZd Zd Zd Zd Zd Z G d d e      Zd! Z  G d" d#e      Z!d$ Z"y# e$ r ddlm
Z
mZ Y w xY w)%zReading and writing Git configuration files.

TODO:
 * preserve formatting when updating configuration files
 * treat subsection names as case-insensitive for [branch.foo] style
   subsections
    N)BinaryIOTupleOptional)OrderedDict)IterableMutableMapping)GitFilec                     t        | t        t        f      r| j                         S t        | t              r t        |       t        t        |             S | S N)
isinstancebytesstrlowerr   typemap	lower_key)keys    !lib/third_party/dulwich/config.pyr   r   7   sC    #s|$yy{#x tCyY,--J    c                   N     e Zd Zedd       Z fdZ fdZefdZefdZ	 xZ
S )CaseInsensitiveDictc                     t        ||       r|S  |        }||S t        |t              st        |j                         D ]
  \  }}|||<    |S r   )r   r   	TypeErroritems)clsdict_inoutr   values        r   makezCaseInsensitiveDict.makeB   sW     gs#Ne?J'>2O!--/JCCH * 
r   c                 F    t        |      }t        t        |   ||fi | y r   )r   superr   __setitem__)selfr   r   kwargs	__class__s       r   r"   zCaseInsensitiveDict.__setitem__U   s#    n!44S%J6Jr   c                 @    t        |      }t        t        |   |      S r   )r   r!   r   __getitem__)r#   itemr   r%   s      r   r'   zCaseInsensitiveDict.__getitem__Z   s    o($;C@@r   c                 `    	 | |   S # t         $ r Y nw xY w|t        u r t        |              S |S r   )KeyErrorSENTINALr   r#   r   defaults      r   getzCaseInsensitiveDict.get_   s@    	9 		 h4:<s    	c                 ^    	 | |   S # t         $ r | j                  ||      | |<   Y | |   S w xY wr   )r*   r.   r,   s      r   
setdefaultzCaseInsensitiveDict.setdefaultj   s>    	/9 	/g.DICy	/s    ,,r   )__name__
__module____qualname__classmethodr   r"   r'   r+   r.   r0   __classcell__r%   s   @r   r   r   A   s5     $K
A
  ( 	 '/ r   r   c                   6    e Zd ZdZd Zd	dZd Zd Zd Zd Z	y)
ConfigzA Git configuration.c                 ,    t        | j                        )a'  Retrieve the contents of a configuration setting.

        Args:
          section: Tuple with section name and optional subsection namee
          subsection: Subsection name
        Returns:
          Contents of the setting
        Raises:
          KeyError: if the value is not set
        )NotImplementedErrorr.   r#   sectionnames      r   r.   z
Config.getv   s     "$((++r   Nc                     	 | j                  ||      }|j                         dk(  ry|j                         dk(  ryt        d|z        # t        $ r |cY S w xY w)aW  Retrieve a configuration setting as boolean.

        Args:
          section: Tuple with section name and optional subsection name
          name: Name of the setting, including section and possible
            subsection.
        Returns:
          Contents of the setting
        Raises:
          KeyError: if the value is not set
           trueT   falseFznot a valid boolean string: %r)r.   r*   r   
ValueError)r#   r<   r=   r-   r   s        r   get_booleanzConfig.get_boolean   sb    	HHWd+E ;;=G#[[]h&9EABB  	N	s   A
 
AAc                 ,    t        | j                        )a	  Set a configuration value.

        Args:
          section: Tuple with section name and optional subsection namee
          name: Name of the configuration value, including section
            and optional subsection
           value: value of the setting
        )r:   setr#   r<   r=   r   s       r   rD   z
Config.set   s     "$((++r   c                 ,    t        | j                        )zIterate over the configuration pairs for a specific section.

        Args:
          section: Tuple with section name and optional subsection namee
        Returns:
          Iterator over (name, value) pairs
        )r:   	iteritemsr#   r<   s     r   rG   zConfig.iteritems   s     "$..11r   c                 ,    t        | j                        )zRIterate over the sections.

        Returns: Iterator over section tuples
        )r:   itersectionsr#   s    r   rJ   zConfig.itersections   s    
 "$"3"344r   c                 &    || j                         v S )zCheck if a specified section exists.

        Args:
          name: Name of section to check for
        Returns:
          boolean indicating whether the section exists
        )rJ   )r#   r=   s     r   has_sectionzConfig.has_section   s     t((***r   r   )
r1   r2   r3   __doc__r.   rB   rD   rG   rJ   rM    r   r   r8   r8   s   s$    ,C,	,25+r   r8   c                   p    e Zd ZdZddZd Zd Zd Zd Zd Z	d	 Z
d
 Zed        Zd Zd Zd Zd Zd Zy)
ConfigDictz)Git configuration stored in a dictionary.Nc                 r    |t        j                         }|| _        t        j	                  |      | _        y)zCreate a new ConfigDict.N)sysgetdefaultencodingencodingr   r   _values)r#   valuesrU   s      r   __init__zConfigDict.__init__   s/    --/H *//7r   c                 N    | j                   j                  d| j                  dS )N())r%   r1   rV   rK   s    r   __repr__zConfigDict.__repr__   s    >>22DLLAAr   c                 d    t        || j                        xr |j                  | j                  k(  S r   )r   r%   rV   )r#   others     r   __eq__zConfigDict.__eq__   s%    %0RU]]dll5RRr   c                 8    | j                   j                  |      S r   )rV   r'   r#   r   s     r   r'   zConfigDict.__getitem__       ||'',,r   c                 :    | j                   j                  ||      S r   )rV   r"   )r#   r   r   s      r   r"   zConfigDict.__setitem__   s    ||''U33r   c                 8    | j                   j                  |      S r   )rV   __delitem__ra   s     r   re   zConfigDict.__delitem__   rb   r   c                 6    | j                   j                         S r   )rV   __iter__rK   s    r   rg   zConfigDict.__iter__   s    ||$$&&r   c                 6    | j                   j                         S r   )rV   __len__rK   s    r   ri   zConfigDict.__len__   s    ||##%%r   c                 r    |j                  d      }t        |      dk(  r|d   |d   |d   fS |d   d |d   fS )N.   r         )splitlen)r   r=   partss      r   _parse_settingzConfigDict._parse_setting   sH    

3u:?!HeAha11!HdE!H--r   c           	         t        |t              s|f}t        |D cg c]/  }t        |t              s|j                  | j                        n|1 c}      }t        |t              s|j                  | j                        }||fS c c}w r   )r   tupler   encoderU   )r#   r<   r=   
subsections       r   _check_section_and_namez"ConfigDict._check_section_and_name   s    '5)jG
 #*	 #*J "*e4 !!$--0  #*	
 $&;;t}}-D}s   4Bc                     | j                  ||      \  }}t        |      dkD  r	 | j                  |   |   S | j                  |d   f   |   S # t        $ r Y !w xY w)Nrm   r   )rw   rp   rV   r*   r;   s      r   r.   zConfigDict.get   sm    44WdCw<!||G,T22 ||WQZM*400  s   A 	AAc                     | j                  ||      \  }}t        |      t        t        fvr|j	                  | j
                        }|| j                  j                  |      |<   y r   )rw   r   boolr   ru   rU   rV   r0   rE   s       r   rD   zConfigDict.set  sS    44WdC;tUm+LL/E16(.r   c                 T    | j                   j                  |      j                         S r   )rV   r.   r   rH   s     r   rG   zConfigDict.iteritems  s     ||(..00r   c                 6    | j                   j                         S r   )rV   keysrK   s    r   rJ   zConfigDict.itersections  s    ||  ""r   NN)r1   r2   r3   rN   rX   r\   r_   r'   r"   re   rg   ri   r4   rr   rw   r.   rD   rG   rJ   rO   r   r   rQ   rQ      s\    38BS-4-'& . .$	171#r   rQ   c                     | j                  d      s7| j                  d      s&| j                  d      sd| v s| j                  d      rdt        |       z   dz   S t        |       S )N       	   #   ")
startswithendswith_escape_valuer   s    r   _format_stringr     s]    E">>$5=>>% mE**T11U##r      \r      n   
   tr      b   r      ;r   c                    t        | j                               } t               }t               }d}d}|t        |       k  r| |   }|t        d      k(  rA|dz  }	 t        | |      }|r|j                  |       t               }|j                  |       ne|t        d      k(  r| }nS|t        v r|sn\|t        v r|j                  |       n.|r|j                  |       t               }|j                  |       |dz  }|t        |       k  r|rt        d      t        |      S # t
        $ r t        d| |fz        t        $ r t        d| |   || fz        w xY w)	NFr   r   rm   z1escape character in %r at %d before end of stringz=escape character followed by unknown character %s at %d in %rr   zmissing end quote)	bytearraystriprp   ord_ESCAPE_TABLE
IndexErrorrA   r*   extendappend_COMMENT_CHARS_WHITESPACE_CHARSr   )r   ret
whitespace	in_quotesicvs          r   _parse_stringr   +  sk   ekkm$E
+CJI	A
c%j.!HE
?FA
!%(+ 

:&&[
JJqM#d)^%I. ##a 

:&&[
JJqM	Q? c%j.B ,--:=   G5RS*T    %(-a!U';< s   D% %6Ec                     | j                  dd      } | j                  dd      } | j                  dd      } | j                  dd      } | S )	zEscape a value.r   s   \\r   s   \nr   s   \tr   s   \")replacer   s    r   r   r   X  sG    MM%)EMM%(EMM%(EMM$'ELr   c                 v    t        t        |             D ]"  }| ||dz    }|j                         r|dk7  s" y y)Nrm      -FTrangerp   isalnumr=   r   r   s      r   _check_variable_namer   a  s;    3t9QUOyy{qDy  r   c                 t    t        t        |             D ]!  }| ||dz    }|j                         r|dvs! y y)Nrm   )r      .FTr   r   s      r   _check_section_namer   i  s<    3t9QUOyy{q4  r   c                     t        d      t        d      h}t        d      }d}t        t        |             D ]  \  }}||k(  r| }|r||v s| d | c S  | S )Nr   r   r   F)r   	enumerater   )linecomment_bytesquotestring_openr   	characters         r   _strip_commentsr   q  sd    YD	*MIEK!)D/29)/Km!;8O 3 Kr   c                   j     e Zd ZdZd
 fd	Zededd fd       Zedd       ZdddZ	deddfd	Z
 xZS )
ConfigFilez;A Git configuration file, like .git/config or ~/.gitconfig.Nc                 >    t         t        |   ||       d | _        y )N)rW   rU   )r!   r   rX   path)r#   rW   rU   r%   s      r   rX   zConfigFile.__init__  s    j$((J	r   freturnc                 $    |        }d}d}d}t        |j                               D ]:  \  }}|j                         }|t        |      dkD  r)|dd dk(  r t	        |      j                         }	 |j                  d      }|d| j                  dd      }	||dz   d }t        |	      dk(  r\|	d   dd d	k7  s|	d   d
d d	k7  rt        d|	d   z        |	d   dd
 |	d<   t        |	d         st        d|	d   z        |	d   |	d   f}nSt        |	d         st        d|	d   z        |	d   j                  dd      }	t        |	      dk(  r|	d   |	d   f}n|	d   f}|j                  j                  |       t	        |      j                         dk(  rp|t        d|z        	 |j                  dd      \  }}
|j                         }t        |      st        d|z        |
j                  d      r|
dd }d}t        |
      }
|
|j                  |   |<   d}|j                  d      r
||dd z  }||z  }t        |      }
|
|j                  |   |<   d}d}= |S # t        $ r t        d      w xY w# t        $ r |}d}
Y w xY w)z+Read configuration from a file-like object.Nr   rm      [   ]zexpected trailing ]r   rn   r   zInvalid subsection %rzinvalid section name %rr   r   zsetting %r without section   =r?   zinvalid variable name %rs   \
)r   	readlineslstriprp   r   rstripindexrA   ro   r   rV   r0   r   r   r   r   )r   r   r   r<   settingcontinuationlinenor   lastptsr   s              r   	from_filezConfigFile.from_file  s    e%akkm4LFD;;=Dt9q=T"1X%5*40779D@#zz$/ q,,,T15Cq
+D3x1}q6"1:-Q1D",-Ds1v-M"NN%(VAb\CF23q6:",-FQ-O"PP#&q63q6"223q6:",-FQ-O"PP!!fll43s8q='*1vs1v&6G'*1viGKK**73"4(..0C7?$%AD%HII$%)ZZa%8NGU "--/+G4$%?'%IJJ>>'*#(":L#'L)%0E49CKK(1"G==) D"I-L D(L),7E49CKK(1#'L"Gq 5r 
c & @()>??@6 " $"G#E$s   3I'(I?'I<?JJc                 z    t        |d      5 }| j                  |      }||_        |cddd       S # 1 sw Y   yxY w)z'Read configuration from a file on disk.rbN)r	   r   r   )r   r   r   r   s       r   	from_pathzConfigFile.from_path  s4     T4 A--"CCH !  s   1:c                     || j                   }t        |d      5 }| j                  |       ddd       y# 1 sw Y   yxY w)z&Write configuration to a file on disk.Nwb)r   r	   write_to_file)r#   r   r   s      r   write_to_pathzConfigFile.write_to_path  s6    <99DT4 Aq! !  s   6?c                    | j                   j                         D ]  \  }}	 |\  }}||j                  d|z   dz          n|j                  d|z   dz   |z   dz          |j                         D ];  \  }}|du rd}n|du rd	}nt	        |      }|j                  d
|z   dz   |z   dz          =  y# t        $ r	 |\  }d}Y w xY w)z*Write configuration to a file-like object.Nr   s   ]
s    "s   "]
Tr?   Fr@   r   s    = r   )rV   r   rA   writer   )r#   r   r<   rW   section_namesubsection_namer   r   s           r   r   zConfigFile.write_to_file  s    #||113OGV'07-o &|+f45|+e3oEOP$lln
UD=#Ee^$E*51Ef,u4u<= -  4  '")"&'s   B//C Cr~   )r   r   r   )r   N)r1   r2   r3   rN   rX   r4   r   r   r   r   r   r5   r6   s   @r   r   r     sZ    E ?( ?| ? ?B  ">x >D >r   r   c                      t         j                  j                  dt         j                  j	                  d            }t        j                  j
                  |g|  S )NXDG_CONFIG_HOMEz
~/.config/)osenvironr.   r   
expanduserjoin)path_segmentsxdg_config_homes     r   get_xdg_config_home_pathr     sC    jjnn
<(O 77<<8-88r   c                   J    e Zd ZdZd	dZd Zed        Zed        Zd Z	d Z
y)
StackedConfigz6Configuration which reads from multiple config files..Nc                      || _         || _        y r   )backendswritable)r#   r   r   s      r   rX   zStackedConfig.__init__  s      r   c                 P    d| j                   j                  d| j                  dS )N<z for >)r%   r1   r   rK   s    r   r\   zStackedConfig.__repr__  s     $ 7 7GGr   c                 .     | | j                               S r   )default_backends)r   s    r   r-   zStackedConfig.default  s    3'')**r   c                 `   g }|j                  t        j                  j                  d             |j                  t	        dd             dt        j
                  vr|j                  d       g }|D ])  }	 t        j                  |      }|j                  |       + |S # t        $ r Y 9w xY w)zjRetrieve the default configuration.

        See git-config(1) for details on the files searched.
        z~/.gitconfiggitconfigGIT_CONFIG_NOSYSTEMz/etc/gitconfig)	r   r   r   r   r   r   r   r   FileNotFoundError)r   pathsr   r   cfs        r   r   zStackedConfig.default_backends  s     RWW''78-eX>? 

2LL)*D))$/ OOB   % s   7B!!	B-,B-c                     t        |t              s|f}| j                  D ]  }	 |j                  ||      c S  t	        |      # t        $ r Y /w xY wr   )r   rt   r   r.   r*   )r#   r<   r=   backends       r   r.   zStackedConfig.get  sW    '5)jG}}G{{7D11 %
 tn  s   A	AAc                 ~    | j                   t        | j                        | j                   j                  |||      S r   )r   r:   rD   rE   s       r   rD   zStackedConfig.set#  s4    == %dhh//}}  $66r   r   )r1   r2   r3   rN   rX   r\   r4   r-   r   r.   rD   rO   r   r   r   r     sB    @!H + +  *7r   r   c              #      K   | j                         D ]8  }|\  }}|dk(  s| j                  |d      }| j                  |d      }|||f : yw)zParse a gitmodules GitConfig file, returning submodules.

    Args:
      config: A `ConfigFile`
    Returns:
      list of tuples (submodule path, url, name),
        where name is quoted part of the section's name.
    s	   submodules   paths   urlN)r}   r.   )r   r<   section_kindr   sm_pathsm_urls         r   parse_submodulesr   )  sW      ;;=%,"l<'jj'2GZZ0FFL11 !s
   A.A)#rN   r   rS   typingr   r   r   collectionsr   collections.abcr   r   ImportErrordulwich.filer	   objectr+   r   r   r8   rQ   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rO   r   r   <module>r      s9  * 
 
 , ,	 ! 8/+ /dJ+V J+ZP# P#f
$ JE
Is4yIs5zIs5zIs5z d)SY'ZT+ *Zk> k>\917F 17h2}   s   C- -C=<C=