
                             S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKJ	r	  SSK
r
SSKJr   " S	 S
\R                  5      r " S S\5      r " S S\5      rS rg)z0Low level reading and writing of property files.    )absolute_import)division)unicode_literalsN)
exceptions)files)configparserc                       \ rS rSrSrSrg)Error   z*Exceptions for the properties_file module. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       9lib/googlecloudsdk/core/configurations/properties_file.pyr
   r
      s    2r   r
   c                       \ rS rSrSrSrg)PropertiesParseError"   z<An exception to be raised when a properties file is invalid.r   Nr   r   r   r   r   r   "   s    Dr   r   c                   0    \ rS rSrSrS rS rS rS rSr	g)	PropertiesFile&   z/A class for loading and parsing property files.c                 X    0 U l         U H  nU(       d  M  U R                  U5        M     g)zCreates a new PropertiesFile and load from the given paths.

Args:
  paths: [str], List of files to load properties from, in order.
N)_properties_PropertiesFile__Load)selfpathsproperties_paths      r   __init__PropertiesFile.__init__)   s'     D 	O$ !r   c                    [         R                  " 5       n UR                  U5        UR                  5        HW  nX@R                  ;  a  0 U R                  U'   U R                  U   R                  [        UR                  U5      5      5        MY     g! [         R                   a  n[	        [        U5      5      eSnAff = f)zLoads properties from the given file.

Overwrites anything already known.

Args:
  properties_path: str, Path to the file containing properties info.
N)r   ConfigParserreadParsingErrorr   strsectionsr   updatedictitems)r   r!   parsed_configesections        r   __LoadPropertiesFile.__Load6   s     !--/M)) !))+	((	($&!
w&&tM,?,?,H'IJ , $$ ) Q(()s   B C(B<<Cc                 H     U R                   U   U   $ ! [         a     gf = f)zGets the value of the given property.

Args:
  section: str, The section name of the property to get.
  name: str, The name of the property to get.

Returns:
  str, The value for the given section and property, or None if it is not
    set.
N)r   KeyError)r   r/   names      r   GetPropertiesFile.GetI   s/    g&t,, s    
!!c                 ,    [        U R                  5      $ )z/Returns a dictionary of properties in the file.)r+   r   )r   s    r   AllPropertiesPropertiesFile.AllPropertiesY   s      !!r   )r   N)
r   r   r   r   r   r"   r   r5   r8   r   r   r   r   r   r   &   s    7%K& "r   r   c                 Z   [         R                  " 5       nUR                  U 5        UR                  U5      (       d  Uc  gUR	                  U5        Uc  UR                  X5        O&UR                  X[        R                  " U5      5        [        R                  R                  U 5      u  pV[        R                  " U5        [        R                  (       a  [        R                  O[        R                   nU" U 5       nUR#                  U5        SSS5        g! , (       d  f       g= f)a8  Persists a value for a given property to a specific property file.

Args:
  file_path: str, The path to the property file to update.
  section: str, The section name of the property to set.
  name: str, The name of the property to set.
  value: str, The value to set for the given property, or None to unset it.
N)r   r%   r&   has_sectionadd_sectionremove_optionsetsix	text_typeospathsplitr   MakeDirPY2BinaryFileWriter
FileWriterwrite)		file_pathr/   r4   valuer-   properties_dirunused_namewriterfps	            r   PersistPropertyrO   ^   s     ++--Y		"	"7	+	+}g&
].gS]]5%9: "i 8.-- &)WW5!!%2B2B&iB s   D
D*)r   
__future__r   r   r   rA   googlecloudsdk.corer   googlecloudsdk.core.utilr   r?   	six.movesr   r
   r   objectr   rO   r   r   r   <module>rU      sT    7 &  ' 	 * * 
 "3J 3E5 E5"V 5"pr   