
    x'                     d   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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jA                  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' G d de      Z(y)z<Implementation of website configuration command for buckets.    )absolute_import)print_function)division)unicode_literalsN)encoding)metrics)Command)CommandArgument)ApiSelector)CommandException)NO_URLS_MATCHED_TARGET)CreateHelpText)storage_v1_messages)NO_MAX)GcloudStorageFlag)GcloudStorageMapzR
  gsutil web set [-m <main_page_suffix>] [-e <error_page>] gs://<bucket_name>...
z%
  gsutil web get gs://<bucket_name>

a  
<B>SET</B>
  The ``gsutil web set`` command allows you to configure or disable the website
  configuration on your buckets. The ``set`` sub-command has the following
  options (omit both options to unset the configuration):

<B>SET OPTIONS</B>
  -m <index.html>      Specifies the object name to serve when a bucket
                       listing is requested via a custom domain.

  -e <404.html>        Specifies the error page to serve when a request is made
                       for a non-existent object via a custom domain

a4  
<B>GET</B>
  The ``gsutil web get`` command retrieves the web semantics configuration for
  a bucket and displays a JSON representation of the configuration.

  In Google Cloud Storage, this would look like the following:

    {
      "notFoundPage": "404.html",
      "mainPageSuffix": "index.html"
    }

ay  
  Cloud Storage allows you to configure a bucket to behave like a static
  website when the bucket is accessed through a `custom domain
  <https://cloud.google.com/storage/docs/request-endpoints#custom-domains>`_.
  For example, if you set a ``main_page_suffix``, a subsequent GET bucket
  request through a custom domain serves the specified "main" page instead of
  performing the usual bucket listing. Similarly, if you set an ``error_page``,
  a subsequent GET object request through a custom domain for a non-existent
  object serves the specified error page instead of the standard Cloud Storage
  error.

  Requests to a bucket through other `endpoints
  <https://cloud.google.com/storage/docs/request-endpoints>`_ are unaffected
  by the bucket's website configuration.

  See `Static website examples and tips
  <https://cloud.google.com/storage/docs/static-website>`_ for additional
  examples and information.

  Notes:

  1. Because the main page is only served when a bucket listing request is made
     via a custom domain endpoint, you can continue to use ``gsutil ls`` as you
     normally would for bucket listing.

  2. The main_page_suffix applies to each subdirectory of the bucket. For
     example, with the main_page_suffix configured to be index.html, a GET
     request for ``http://www.example.com`` retrieves
     ``http://www.example.com/index.html`` but shows ``http://www.example.com`` as
     the URL to the requester, and a GET request for
     ``http://www.example.com/photos`` retrieves
     ``http://www.example.com/photos/index.html`` but shows ``http://www.example.com/photos``
     as the URL to the requester.

  3. There is just one 404.html page. For example, a GET request for
     ``http://www.example.com/photos/missing`` retrieves
     ``http://www.example.com/404.html``, not
     ``http://www.example.com/photos/404.html``.

  The web command has two sub-commands:
c                   d    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j                         g ej                  d      gd	
      Z ej                  dddgddeeed      Z ed eg di d      ii       Z fdZd Zd Zd Z xZS )
WebCommandz%Implementation of gsutil web command.web	setwebcfg	getwebcfg   zm:e:F   )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_helpz(Set a website configuration for a bucket)r   r   )	help_namehelp_name_aliases	help_typehelp_one_line_summary	help_textsubcommand_help_textr   )storagebucketsdescribezk--format=gsutiljson[key=website_config,empty=' has no website configuration.',empty_prefix_key=storage_url]T)gcloud_commandflag_mapsupports_output_translationr2   r3   c           
      N   | j                   d   dk(  rtt        dt        g dt        d      t        d      d      ii       }d| j                   v s2d	| j                   v s$|j                  d   xj                  d
dgz  c_        |}nt        j
                  }t        |   |      S )Nr   r   )r/   r0   updatez--web-error-pagez--web-main-page-suffix)-e-mr5   r8   r9   z--clear-web-error-pagez--clear-web-main-page-suffix)argsr   r   r2   r   gcloud_storage_mapsuperget_gcloud_storage_args)selfset_command_mapr;   	__class__s      %platform/gsutil/gslib/commands/web.pyr=   z"WebCommand.get_gcloud_storage_args   s    yy|u("& !22D E 12J K 	 o  dii4499#4&&u-<<$&DA
 	
< +%887*+=>>    c                 :   | j                  | j                  d   dg      \  }}|j                  dk(  rEt        j                  j                  | j                  j                  ||j                               y|j                  ru|j                  j                  s|j                  j                  rIt        j                  j                  t        t        j                  |j                              dz          yt        j                  j                  d|z         y)z(Gets website configuration for a bucket.r   websitebucket_fieldss3)providerr   z!%s has no website configuration.
)GetSingleBucketUrlFromArgr:   schemesysstdoutwrite
gsutil_apiXmlPassThroughGetWebsiterD   mainPageSuffixnotFoundPagestrr   MessageToJson)r>   
bucket_urlbucket_metadatas      rA   _GetWebzWebCommand._GetWeb   s    "&"@"@		!YK #A #1J D 	jj
//
2
2:<F<M<M 3 OP  
	 	 o&=&=&L&L&5&=&=&J&J

&&'>'>?@4G	I
  	

=
JKrB   c                 0   d}d}| j                   r$| j                   D ]  \  }}|dk(  r|}|dk(  s|} | j                  }t        j                  j	                  ||      }d}|D ]  }| j                  |dg      }	|	D ]v  }
|
j                  }d}| j                  j                  d	|
       t        j                  |
      }| j                  j                  |j                  ||j                  dg       x  |st        t        t        |      z        y)z(Sets website configuration for a bucket.Nr9   r8   )rP   rQ   FidrE   Tz&Setting website configuration on %s...)rD   )rH   fieldsr   )sub_optsr:   apitools_messagesBucketWebsiteValueGetBucketUrlIterFromArgstorage_urlloggerinforN   PatchBucketbucket_namerJ   r   r   list)r>   main_page_suffix
error_pageoaurl_argsrD   some_matchedurl_strbucket_iterblrurlrU   s                rA   _SetWebzWebCommand._SetWeb   s   J}}--$!Q9
$Y*	   yyH&&33'j 4 BG
 L000Ok#ooA3G+227C##COO$3-0ZZ,06 	$ 	3   3d8nDEErB   c                 .   | j                   j                  d      }| j                  d       |dk(  r| j                  }n.|dk(  r| j                  }nt        d|d| j                  d      t        j                  |g| j                  	        |       S )
z(Command entry point for the web command.r   T)
check_argsr   r   zInvalid subcommand "z
" for the z  command.
See "gsutil help web".)subcommandsrZ   )
r:   popParseSubOptsrV   ro   r   command_namer   LogCommandParamsrZ   )r>   action_subcommandfuncs      rA   
RunCommandzWebCommand.RunCommand  s    		a(&E!\\d	e	#\\d(94;L;LNO O *;)<&*mm56MrB   )__name__
__module____qualname____doc__r	   CreateCommandSpec	_SYNOPSISr   r   XMLJSONr
   %MakeZeroOrMoreCloudBucketURLsArgumentMakeNCloudBucketURLsArgumentcommand_specHelpSpec_DETAILED_HELP_TEXT_get_help_text_set_help_text	help_specr   r;   r=   rV   ro   ry   __classcell__)r@   s   @rA   r   r   }   s    - +**'5!oo{'7'78 %%G/GGIJ>/>>qAB,$ g$k2G#
) (
" .2	 $?:& DrB   r   ))r}   
__future__r   r   r   r   rK   apitools.base.pyr   gslibr   gslib.commandr	   gslib.command_argumentr
   gslib.cs_api_mapr   gslib.exceptionr   r   gslib.help_providerr   "gslib.third_party.storage_apitoolsr   r[   gslib.utils.constantsr   gslib.utils.shim_utilr   r   _SET_SYNOPSIS_GET_SYNOPSISlstripr   _SET_DESCRIPTION_GET_DESCRIPTION_DESCRIPTIONr   r   r   r    rB   rA   <module>r      s    C & %  ' 
 %  ! 2 ( , 2 . W ( 3 2 M0066	  (P Q(P *Q(*T %Y= /?@/?@W WrB   