
    #                        d 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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# eee       Z$ eee!      Z%d ejL                         z   dz   dz   dz   dz   dz   dz   Z' G d de      Z(y) z5This module provides the autoclass command to gsutil.    )absolute_import)print_functionN)metrics)Command)CommandArgument)ApiSelector)CommandException)NO_URLS_MATCHED_TARGET)CreateHelpText)storage_v1_messages)	text_util)NO_MAX)GcloudStorageMap)	shim_utilz7
  gsutil autoclass set (on|off) gs://<bucket_name>...
z.
  gsutil autoclass get gs://<bucket_name>...

z
<B>SET</B>
  The ``set`` sub-command requires an additional sub-command, either ``on``
  or ``off``, which enables or disables Autoclass for the specified
  bucket(s).
a+  
<B>GET</B>
  The ``get`` sub-command gets the current Autoclass configuration for a
  bucket. The returned configuration has the following fields:

  ``enabled``: a boolean field indicating whether the feature is on or off.

  ``toggleTime``: a timestamp indicating when the enabled field was set.
z
  The `Autoclass <https://cloud.google.com/storage/docs/autoclass>`_
  feature automatically selects the best storage class for objects based
  on access patterns. This command has two sub-commands, ``get`` and
  ``set``.
z--format=value[separator="z"](zformat("gs://{}:", name),z format("  Enabled: {}",z&autoclass.enabled.yesno(True, False)),z format("  Toggle Time: {}",zautoclass.toggleTime))c                      e Zd ZdZ ej
                  dg ededdddej                  gej                   e
j                  d      g e
ddd	g
       e
j                         gd      Z ej                  dg ddeeed      Z e eddded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)AutoclassCommandz(Implements the gsutil autoclass command.	autoclass    F   modeonoff)choicesgetset)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_helpzConfigure Autoclass feature)	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)r1   r2   updatez--enable-autoclass)r4   r5   )r1   r2   r7   z--no-enable-autoclass)r   r   c                 l   |j                   }| j                  j                  |j                  dg|j                        }t        |      j                  d      }|j                  r/t        |j                  dd      }t        |j                  dd      }nd}d}t        dj                  |||             y)	z(Gets the autoclass setting for a bucket.r   fieldsprovider/enabledF
toggleTimeNz#{}:
  Enabled: {}
  Toggle Time: {})storage_url
gsutil_api	GetBucketbucket_nameschemestrrstripr   getattrprintformat)selfblr
bucket_urlbucket_metadatabucketr=   toggle_times          +platform/gsutil/gslib/commands/autoclass.py_get_autoclasszAutoclassCommand._get_autoclass   s    Joo//
0F0F8C}9C9J9J 0 LO _##C(F  119eDgO55|TJkgk	 $fVWkBD    c                 R   |j                   }t        j                  j                         }|dk(  |_        t        j                  |      }t        d|dt        |      j                  d             | j                  j                  |j                  |dg|j                         y)	z'Turns autoclass on or off for a bucket.r   )r   zSetting Autoclass z for r<   r   r9   r   )r?   apitools_messagesBucketAutoclassValuer=   rG   rD   rE   r@   PatchBucketrB   rC   )rI   rJ   setting_argrK   autoclass_configrL   s         rO   _set_autoclasszAutoclassCommand._set_autoclass   s    J(//>>@ +t 3'..9IJO	J..s35 6 	OO
 6 6 /(3})3):):   < rQ   c           
         | j                   j                  d      }|dvrt        d      d}g }d}|dk(  r| j                  }nS|dk(  rN| j                  }| j                   j                  d      }t        j                  |d       |j                  |       | j                  j                  d      t        j                  k7  r:t        d	j                  t        j                  d
| j                  z                    d}| j                   }|s| j!                          |D ]  }| j#                  |      }|D ]  }	| j                  j                  |	j$                  j&                        t        j                  k7  r:t        d	j                  t        j                  d| j                  z                    d} ||	g|    |st        t(        t+        |      z        y)z3Handles autoclass command on Cloud Storage buckets.r   r   zautoclass only supports get|setNr   r   z-Only on and off values allowed for set optiongsr   z=The "%s" command can only be with the Cloud Storage JSON API.Fz2The "%s" command can only be used for GCS Buckets.T)argspopr	   rP   rY   r   InsistOnOrOffappendr@   GetApiSelectorr   JSONjointextwrapwrapcommand_name$RaiseWrongNumberOfArgumentsExceptionGetBucketUrlIterFromArgr?   rC   r
   r3   )
rI   
subcommandsubcommand_funcsubcommand_argsrW   some_matchedurl_argsurl_strbucket_iterbucket_listing_refs
             rO   
_autoclasszAutoclassCommand._autoclass   s   q!J'>??OOKU++o	u	++oIIMM!$kkMO[)%%d+{/?/??TYY
-- %(,(9(9: ;< = =
 LyyH
//1009k +
??))**1136A6F6FG mm (+/+<+<= >"? @ @ *=_= !,  3d8nDEErQ   c                    | j                   d   }| j                  d       |dk(  s|dk(  rGt        j                  | j                         t        j                  |g       | j                         S t        d|z        )	z.Command entry point for the autoclass command.r   T)
check_argsr   r   )sub_opts)subcommandsz-Invalid subcommand "%s", use get|set instead.)r\   ParseSubOptsr   LogCommandParamsrs   rp   r	   )rI   action_subcommands     rO   
RunCommandzAutoclassCommand.RunCommand   sz    		!&E!%6%%?6,=+>?__L./ 0 0rQ   N)__name__
__module____qualname____doc__r   CreateCommandSpec	_SYNOPSISr   r   ra   r   MakeNCloudURLsArgument%MakeZeroOrMoreCloudBucketURLsArgumentcommand_specHelpSpec_DETAILED_HELP_TEXT_get_help_text_set_help_text	help_specr   _GCLOUD_FORMAT_STRINGgcloud_storage_maprP   rY   rp   rx    rQ   rO   r   r   R   s>   0***!&&' %%8/88;=ftUm<CoCCE	,( g9#
) ( F4I" .2  +.  (* +.  (*", /$J M'RD$$-^0rQ   r   ))r|   
__future__r   r   rc   gslibr   gslib.commandr   gslib.command_argumentr   gslib.cs_api_mapr   gslib.exceptionr	   r
   gslib.help_providerr   "gslib.third_party.storage_apitoolsr   rS   gslib.utilsr   gslib.utils.constantsr   gslib.utils.shim_utilr   r   _SET_SYNOPSIS_GET_SYNOPSISlstripr~   _SET_DESCRIPTION_GET_DESCRIPTION_DESCRIPTIONr   r   r   get_format_flag_newliner   r   r   rQ   rO   <module>r      s   < & %   ! 2 ( , 2 . W ! ( 2 ! M0066	  
 
 ** %Y= /?@/?@5:::<=?DE45 44 B	B
 88 22 j0w j0rQ   