
                             d Z ddlmZ ddlmZ ddlmZ ddlZddlmZ ddlm	Z	 ddl
Z
ddlmZ  G d	 d
ej                        Z G d de      Z G d de      Zd Zy)z0Low level reading and writing of property files.    )absolute_import)division)unicode_literalsN)
exceptions)files)configparserc                       e Zd ZdZy)Errorz*Exceptions for the properties_file module.N__name__
__module____qualname____doc__     9lib/googlecloudsdk/core/configurations/properties_file.pyr
   r
      s    2r   r
   c                       e Zd ZdZy)PropertiesParseErrorz<An exception to be raised when a properties file is invalid.Nr   r   r   r   r   r   "   s    Dr   r   c                   (    e Zd ZdZd Zd Zd Zd Zy)PropertiesFilez/A class for loading and parsing property files.c                 H    i | _         |D ]  }|s| j                  |        y)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__zPropertiesFile.__init__)   s%     D 	O$ !r   c                 ~   t        j                         }	 |j                  |       |j                         D ]U  }|| j                  vri | j                  |<   | j                  |   j                  t        |j                  |                   W y# t         j                  $ r}t	        t        |            d}~ww xY w)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   __LoadzPropertiesFile.__Load6   s     !--/M)) !))+	((	($&!
w&&tM,?,?,H'IJ , $$ ) Q(()s   B B<#B77B<c                 F    	 | j                   |   |   S # t        $ r Y yw xY w)a  Gets 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   GetzPropertiesFile.GetI   s/    g&t,, s    	  c                 ,    t        | j                        S )z/Returns a dictionary of properties in the file.)r%   r   )r   s    r   AllPropertieszPropertiesFile.AllPropertiesY   s      !!r   N)r   r   r   r   r   r   r.   r0   r   r   r   r   r   &   s    7%K& "r   r   c                 8   t        j                         }|j                  |        |j                  |      s|y|j	                  |       ||j                  ||       n&|j                  ||t        j                  |             t        j                  j                  |       \  }}t        j                  |       t        j                  rt        j                  nt        j                   } ||       5 }|j#                  |       ddd       y# 1 sw Y   yxY w)aD  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)   r-   valuer'   properties_dirunused_namewriterfps	            r   PersistPropertyrF   ^   s     ++--Y		"	"7	+}g&
].gtS]]5%9: "i 8.+-- &)WW5!!%2B2B&iB s   5DD)r   
__future__r   r   r   r8   googlecloudsdk.corer   googlecloudsdk.core.utilr   r6   	six.movesr   r
   r   objectr   rF   r   r   r   <module>rL      sT    7 &  ' 	 * * 
 "3J 3E5 E5"V 5"pr   