o
                         @   s   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
 d
ejZG dd deZG dd deZdd ZdS )z0Low level reading and writing of property files.    )absolute_import)division)unicode_literalsN)
exceptions)files)configparserc                   @      e Zd ZdZdS )Errorz*Exceptions for the properties_file module.N__name__
__module____qualname____doc__ r   r   O/tmp/google-cloud-sdk/lib/googlecloudsdk/core/configurations/properties_file.pyr	          r	   c                   @   r   )PropertiesParseErrorz<An exception to be raised when a properties file is invalid.Nr
   r   r   r   r   r   "   r   r   c                   @   s0   e Zd ZdZdd Zdd Zdd Zdd	 Zd
S )PropertiesFilez/A class for loading and parsing property files.c                 C   s"   i | _ |D ]	}|r| | qdS )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_pathr   r   r   __init__)   s   
zPropertiesFile.__init__c              
   C   s~   t  }z|| W n t jy } ztt|d}~ww | D ]}|| jvr/i | j|< | j| t	|
| q#dS )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sectionr   r   r   __Load6   s   

zPropertiesFile.__Loadc                 C   s&   z| j | | W S  ty   Y dS 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$   namer   r   r   GetI   s
   zPropertiesFile.Getc                 C   s
   t | jS )z/Returns a dictionary of properties in the file.)r    r   )r   r   r   r   AllPropertiesY   s   
zPropertiesFile.AllPropertiesN)r   r   r   r   r   r   r(   r)   r   r   r   r   r   &   s    r   c           	      C   s   t  }||  ||s|du rdS || |du r$||| n
|||t| t	j
| \}}t| tjrAtjntj}|| }|| W d   dS 1 sYw   Y  dS )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fpr   r   r   PersistProperty^   s   	




"r>   )r   
__future__r   r   r   r0   googlecloudsdk.corer   googlecloudsdk.core.utilr   r.   	six.movesr   r	   r   objectr   r>   r   r   r   r   <module>   s   8