
    ]!                        d 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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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dZeej=                  d      z   ZdZ dZ!de z   e!z   Z" eee"      Z#d ejH                         z   dz   dz   dz   Z% eee!      Z& eee       Z' G d de	      Z(y)z?Implementation of versioning configuration command for buckets.    )absolute_import)print_function)division)unicode_literals)metrics)Command)CommandArgument)ApiSelector)CommandException)NO_URLS_MATCHED_TARGET)CreateHelpText)storage_v1_messages)NO_MAX)GcloudStorageMap)	shim_utilz8
  gsutil versioning set (on|off) gs://<bucket_name>...
z/
  gsutil versioning get gs://<bucket_name>...

z
<B>SET</B>
  The "set" sub-command requires an additional sub-command, either "on" or
  "off", which, respectively, will enable or disable versioning for the
  specified bucket(s).

z
<B>GET</B>
  The "get" sub-command gets the versioning configuration for a
  bucket and displays whether or not it is enabled.
z
  The `Versioning Configuration
  <https://cloud.google.com/storage/docs/object-versioning>`_ feature
  enables you to configure a Google Cloud Storage bucket to keep old
  versions of objects.

  The gsutil versioning command has two sub-commands:
z'--format=value[separator=""](name.sub("z", "gs://").sub("$", ": "),zversioning.enabledz.yesno("Enabled", "Suspended"))c                      e Zd ZdZ ej
                  dddgededdddej                  ej                  gej                   edd	d
g       ej                         g ej                         gd      Z ej                  dddgddeeed      Z e eddddegi d       e eg di        eg di       di       di       Zd Zd Zd Zd Zy )!VersioningCommandz,Implementation of gsutil versioning command.
versioningsetversioninggetversioning    Fmodeonoff)choices)setget)command_name_aliasesusage_synopsismin_argsmax_argssupported_sub_argsfile_url_okprovider_url_okurls_start_arggs_api_supportgs_default_apiargparse_argumentscommand_helpz4Enable or suspend versioning for one or more buckets)r   r   )	help_namehelp_name_aliases	help_typehelp_one_line_summary	help_textsubcommand_help_textstoragebucketslistz--rawT)gcloud_commandflag_mapsupports_output_translation)r2   r3   updatez--versioning)r5   r6   )r2   r3   r8   z--no-versioningr   r   c                 ~    | j                   s| j                          | j                   d   j                         dk(  ryy)Nr   r   r      )args$RaiseWrongNumberOfArgumentsExceptionlower)selfs    ,platform/gsutil/gslib/commands/versioning.py_CalculateUrlsStartArgz(VersioningCommand._CalculateUrlsStartArg   s4    99
//1yy|u$    c                    | j                   d   j                         }|dvrt        d| j                  z        | j                   dd }|s| j	                          d}|D ]  }| j                  |dg      }|D ]  }|j                  }d	}t        j                  t        j                  j                         
      }|dk(  r.| j                  j                  d|       d	|j                  _        n-| j                  j                  d|       d|j                  _        | j                  j                  |j                   ||j"                  dg         |st        t$        t'        |      z        y)z+Gets versioning configuration for a bucket.r   r9   z,Argument to "%s set" must be either <on|off>r;   NFidbucket_fieldsT)r   r   zEnabling versioning for %s...zSuspending versioning for %s...)providerfields)r<   r>   r   command_namer=   GetBucketUrlIterFromArgstorage_urlapitools_messagesBucketVersioningValueloggerinfor   enabled
gsutil_apiPatchBucketbucket_nameschemer   r4   )	r?   versioning_argurl_argssome_matchedurl_strbucket_iterblrurlbucket_metadatas	            r@   _SetVersioningz VersioningCommand._SetVersioning   sX   YYq\'')N]*K"//1 2 2yy}H
//1 L000Ok#oo+22(//??ACT!
++

:C
@/3/
$
$
,
++

<c
B/4/
$
$
,##COO$3-0ZZ,06 	$ 	3  " 3d8nDEE rB   c                    | j                   }d}|D ]  }| j                  |dg      }|D ]  }d}|j                  j                  rH|j                  j                  j                  r(t        d|j                  j                  d      z         ct        d|j                  j                  d      z           |st        t        t        |      z        y)	z6Gets versioning configuration for one or more buckets.Fr   rE   Tz%s: Enabled/z%s: SuspendedN)r<   rJ   root_objectr   rQ   print
url_stringrstripr   r   r4   )r?   rW   rX   rY   rZ   r[   s         r@   _GetVersioningz VersioningCommand._GetVersioning   s    yyH L00@L~ 1 Ok#??%%#//*D*D*L*L
 5 5c ::
;
#.."7"7"<<
=   3d8nDEE rB   c           	         | j                   j                  d      }|dk(  r$| j                  }t        j                  |g       nt|dk(  rF| j
                  }| j                   d   j                         }|dv rBt        j                  ||g       n)t        d|d| j                  d| j                  d	       |        y)
z/Command entry point for the versioning command.r   r   )subcommandsr   r9   zInvalid subcommand "z
" for the z command.
See "gsutil help z".)	r<   popre   r   LogCommandParamsr^   r>   r   rI   )r?   action_subcommandfuncrV   s       r@   
RunCommandzVersioningCommand.RunCommand   s    		a(E!  d,=+>?	e	#  dyy|))+n	=	(  *N;	=  d//1B1BDE E 	FrB   N)__name__
__module____qualname____doc__r   CreateCommandSpec	_SYNOPSISr   r
   XMLJSONr	   %MakeZeroOrMoreCloudBucketURLsArgumentcommand_specHelpSpec_DETAILED_HELP_TEXT_get_help_text_set_help_text	help_specr   _GCLOUD_FORMAT_STRINGgcloud_storage_maprA   r^   re   rl    rB   r@   r   r   N   sX   4 +**+_=!oo{'7'78 %% ftUm<CoCCE H/GGIK,* g

 
@#)  ( FG+" .2  +.  (* +.  (*", /$J M'RFBF&rB   r   N))rp   
__future__r   r   r   r   gslibr   gslib.commandr   gslib.command_argumentr	   gslib.cs_api_mapr
   gslib.exceptionr   r   gslib.help_providerr   "gslib.third_party.storage_apitoolsr   rL   gslib.utils.constantsr   gslib.utils.shim_utilr   gslib.utilsr   _SET_SYNOPSIS_GET_SYNOPSISlstriprr   _SET_DESCRIPTION_GET_DESCRIPTION_DESCRIPTIONrx   get_format_flag_caretr|   ry   rz   r   r~   rB   r@   <module>r      s    F & %  '  ! 2 ( , 2 . W ( 2 ! M0066	    ** %Y= G888:;679MN ;; 
  /?@/?@` `rB   