
    S                     6   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m
Z
 dd	lmZ dd
lmZ dZ G d de	j                         Z G d de      Z G d de      Z G d de      Z G d de      Z G d de      Zd Zd ZddZ G d de      Zy)z+Helper methods for record-set transactions.    )absolute_import)division)unicode_literalsN)apis)
exceptions)yaml)resource_printer)filesztransaction.yamlc                       e Zd ZdZy)Errorz*Base exception for all transaction errors.N__name__
__module____qualname____doc__     2lib/googlecloudsdk/api_lib/dns/transaction_util.pyr   r   "   s    2r   r   c                       e Zd ZdZy)TransactionFileAlreadyExistsz Transaction file already exists.Nr   r   r   r   r   r   &   s    (r   r   c                       e Zd ZdZy)UnableToAccessTransactionFilez"Unable to access transaction file.Nr   r   r   r   r   r   *   s    *r   r   c                       e Zd ZdZy)TransactionFileNotFoundzTransaction file not found.Nr   r   r   r   r   r   .   s    #r   r   c                        e Zd Z fdZ xZS )CorruptedTransactionFileErrorc                 ,    t         t        |   d       y )NzFCorrupted transaction file.

Please abort and start a new transaction.)superr   __init__)self	__class__s    r   r   z&CorruptedTransactionFileError.__init__4   s    	
'7	45r   )r   r   r   r   __classcell__)r!   s   @r   r   r   2   s    5 5r   r   c                       e Zd ZdZy)RecordDoesNotExistz$Specified record-set does not exist.Nr   r   r   r   r$   r$   :   s    ,r   r$   c                 6    t        j                  |gd|        y)zWrites the given change in yaml format to the given file.

  Args:
    yaml_file: file, File into which the change should be written.
    change: Change, Change to be written out.
  r   )print_formatoutN)r	   Print)	yaml_filechanges     r   WriteToYamlFiler+   >   s     &IFr   c                     g }|D ]\  }| j                         }|j                  |_        |d   |_        |d   |_        |d   |_        |d   |_        |j                  |       ^ |S )aF  Converts list of record-set dictionaries into list of ResourceRecordSets.

  Args:
    messages: Messages object for the API with Record Sets to be created.
    record_set_dictionaries: [{str:str}], list of record-sets as dictionaries.

  Returns:
    list of ResourceRecordSets equivalent to given list of yaml record-sets
  namettltyperrdatas)ResourceRecordSetkindr-   r.   r/   r0   append)messagesrecord_set_dictionariesrecord_setsrecord_set_dict
record_sets        r   _RecordSetsFromDictionariesr9   H   ss     +0o++-J ooJO%f-JO$U+JN%f-JO(3Jz" 1 
r   c                 p   t        j                  d|      }	 t        j                  |       xs i }|j                  d      |j                  d      
t               |j                         }t        ||d         |_	        t        ||d         |_
        |S # t        j                  $ r t               w xY w)a[  Returns the change contained in the given yaml file.

  Args:
    yaml_file: file, A yaml file with change.
    api_version: [str], the api version to use for creating the change object.

  Returns:
    Change, the change contained in the given yaml file.

  Raises:
    CorruptedTransactionFileError: if the record_set_dictionaries are invalid
  dns	additions	deletions)r   GetMessagesModuler   loadYAMLParseErrorr   getChanger9   r<   r=   )r)   api_versionr4   change_dictr*   s        r   ChangeFromYamlFilerE   _   s     ##E;7(*))I&,"K ook"*ook"*
'
))??&0K(*&0K(*&	- 
		 *
'
))*s   B B5c                   $    e Zd ZdZddZd Zd Zy)TransactionFilez7Context for reading/writing from/to a transaction file.c                    t         j                  j                  |      st        dj	                  |            || _        	 |dk(  rt        j                  |      | _        y |dk(  rt        j                  |      | _        y t        dj	                  |            # t        $ r$}d}|j	                  ||      }t        |      d }~ww xY w)NzTransaction not found at [{0}]rwzUnrecognized mode [{}]z.Unable to open transaction [{0}] because [{1}])ospathisfiler   format!_TransactionFile__trans_file_pathr
   
FileReader_TransactionFile__trans_file
FileWriter
ValueErrorIOErrorr   )r    trans_file_pathmodeexpmsgs        r   r   zTransactionFile.__init__   s    77>>/*#
*
1
1/
BD D -D
/	!,,_=3;!,,_=188>?? /<cJJ,c)#../s$   B "B B 	C	%CC	c                     | j                   S )N)rQ   )r    s    r   	__enter__zTransactionFile.__enter__   s    r   c                     | j                   j                          |t        u s|t        j                  u r)d}|j                  | j                  |      }t        |      y )Nz4Unable to read/write transaction [{0}] because [{1}])rQ   closerT   r   r   rN   rO   r   )r    typvalue	tracebackrX   s        r   __exit__zTransactionFile.__exit__   sQ    
g~

*BcJJt--u5c)#.. +r   N)rI   )r   r   r   r   r   rZ   r`   r   r   r   rG   rG   |   s    ?/&/r   rG   )v1)r   
__future__r   r   r   rK   googlecloudsdk.api_lib.utilr   googlecloudsdk.corer   r   googlecloudsdk.core.resourcer	   googlecloudsdk.core.utilr
   DEFAULT_PATHr   r   r   r   r   r$   r+   r9   rE   objectrG   r   r   r   <module>ri      s     2 &  ' 	 , * $ 9 * "3J 3)5 )+E +$e $5E 5- -G.:/f /r   