
    S                     8   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J
r
  SS	KJr  SS
KJr  Sr " S S\	R                   5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      rS rS rSS jr " S S\5      rg)z+Helper methods for record-set transactions.    )absolute_import)division)unicode_literalsN)apis)
exceptions)yaml)resource_printer)filesztransaction.yamlc                       \ rS rSrSrSrg)Error"   z*Base exception for all transaction errors. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       2lib/googlecloudsdk/api_lib/dns/transaction_util.pyr   r   "   s    2r   r   c                       \ rS rSrSrSrg)TransactionFileAlreadyExists&   z Transaction file already exists.r   Nr   r   r   r   r   r   &   s    (r   r   c                       \ rS rSrSrSrg)UnableToAccessTransactionFile*   z"Unable to access transaction file.r   Nr   r   r   r   r   r   *   s    *r   r   c                       \ rS rSrSrSrg)TransactionFileNotFound.   zTransaction file not found.r   Nr   r   r   r   r   r   .   s    #r   r   c                   (   ^  \ rS rSrU 4S jrSrU =r$ )CorruptedTransactionFileError2   c                 ,   > [         [        U ]  S5        g )NzFCorrupted transaction file.

Please abort and start a new transaction.)superr"   __init__)self	__class__s    r   r&   &CorruptedTransactionFileError.__init__4   s    	
'7	45r   r   )r   r   r   r   r&   r   __classcell__)r(   s   @r   r"   r"   2   s    5 5r   r"   c                       \ rS rSrSrSrg)RecordDoesNotExist:   z$Specified record-set does not exist.r   Nr   r   r   r   r,   r,   :   s    ,r   r,   c                 2    [         R                  " U/SU S9  g)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   WriteToYamlFiler4   >   s     &IFr   c                     / nU H]  nU R                  5       nUR                  Ul        US   Ul        US   Ul        US   Ul        US   Ul        UR                  U5        M_     U$ )a:  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kindr6   r7   r8   r9   append)messagesrecord_set_dictionariesrecord_setsrecord_set_dict
record_sets        r   _RecordSetsFromDictionariesrB   H   ss     +0o++-J ooJO%f-JO$U+JN%f-JO(3Jz" 1 
r   c                    [         R                  " SU5      n [        R                  " U 5      =(       d    0 nUR                  S5      b  UR                  S5      c
  [        5       eUR                  5       n[        X#S   5      Ul	        [        X#S   5      Ul
        U$ ! [        R                   a    [        5       ef = f)aK  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ChangerB   rE   rF   )r2   api_versionr=   change_dictr3   s        r   ChangeFromYamlFilerN   _   s     ##E;7(*))I&,"K ook"*ook"*
'
))??&0K(*&0K(*&	- 
		 *
'
))*s   B B=c                   .    \ rS rSrSrSS jrS rS rSrg)	TransactionFile|   z7Context for reading/writing from/to a transaction file.c                    [         R                  R                  U5      (       d  [        SR	                  U5      5      eXl         US:X  a  [        R                  " U5      U l        g US:X  a  [        R                  " U5      U l        g [        SR	                  U5      5      e! [         a#  nSnUR	                  X5      n[        U5      eS nAff = f)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&   TransactionFile.__init__   s    77>>/**#
*
1
1/
BD D -
/	!,,_=3;!,,_=188>?? /<cJJ,c)#../s$   !B$ (!B$ 
B$ $
C.CCc                     U R                   $ )N)r[   )r'   s    r   	__enter__TransactionFile.__enter__   s    r   c                     U R                   R                  5         U[        L d  U[        R                  L a)  SnUR                  U R                  U5      n[        U5      eg )Nz4Unable to read/write transaction [{0}] because [{1}])r[   closer^   r   r   rX   rY   r   )r'   typvalue	tracebackrb   s        r   __exit__TransactionFile.__exit__   sQ    
g~

*BcJJt--u5c)#.. +r   )__trans_file__trans_file_pathN)rS   )	r   r   r   r   r   r&   re   rl   r   r   r   r   rP   rP   |   s    ?/&/r   rP   )v1)r   
__future__r   r   r   rU   googlecloudsdk.api_lib.utilr   googlecloudsdk.corer   r   googlecloudsdk.core.resourcer	   googlecloudsdk.core.utilr
   DEFAULT_PATHr   r   r   r   r"   r,   r4   rB   rN   objectrP   r   r   r   <module>rx      s     2 &  ' 	 , * $ 9 * "3J 3)5 )+E +$e $5E 5- -G.:/f /r   