
    }                         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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e
j,                   G d de
j.                               Zy)zDImplementation of Unix-like cat command for cloud storage providers.    )absolute_import)division)unicode_literalsN)	cloud_api)arg_parsers)base)encryption_util)errors)flags)name_expansion)storage_url)task_executor)cat_task_iteratorc                 0   | dk(  rt        j                  dd      S | j                  d      \  }}}|s#t        j                  dt        |      z  d      S |s t        j                  t        |      d      S t         j                  j	                  |       S )zCreates Range object out of given string value.

  Args:
    string_value (str): The range the user entered.

  Returns:
    Range(int, int|None): The Range object from the given string value.
  -r   N)startend)r   Range	partitionintParse)string_valuerange_start_	range_ends       lib/surface/storage/cat.py_range_parserr   #   s     S1$//*44S9+q)	2I#6DAA	3{#3>>				 	 	..    c                   0    e Zd ZdZdddZed        Zd Zy)Catz3Outputs the contents of one or more URLs to stdout.a"  
      The cat command outputs the contents of one or more URLs to stdout. While
      the cat command does not compute a checksum, it is otherwise equivalent to
      doing:

        $ {parent_command} cp url... -

      (The final '-' causes gcloud to stream the output to stdout.)
      a  

      The following command writes all text files in a bucket to stdout:

        $ {command} gs://bucket/*.txt

      The following command outputs a short header describing file.txt, along
      with its contents:

        $ {command} -d gs://my-bucket/file.txt

      The following command outputs bytes 256-939 of file.txt:

        $ {command} -r 256-939 gs://my-bucket/file.txt

      The following command outputs the last 5 bytes of file.txt:

        $ {command} -r -5 gs://my-bucket/file.txt

      )DESCRIPTIONEXAMPLESc                    | j                  ddd       | j                  dddd	       | j                  d
dt        t        j                  d             t	        j
                  |        t	        j                  | d       y )Nurl+zThe url of objects to list.)nargshelpz-dz--display-url
store_truez%Prints the header before each object.)actionr(   z-rz--rangea              Causes gcloud storage to output just the specified byte range of
            the object. In a case where "start" = 'x', and "end" = 'y',
            ranges take the form:
            `x-y` (e.g., `-r 256-5939`), `x-` (e.g., `-r 256-`),
            `-y` (e.g., `-r -5`)

            When offsets start at 0, x-y means to return bytes x
            through y (inclusive), x- means to return bytes x through
            the end of the object, and -y changes the role of y.
            If -y is present, then it returns the last y bytes of the object.

            If the bytes are out of range of the object,
            then nothing is printed)typer(   T)command_only_reads_data)add_argumentr   textwrapdedentr   add_additional_headers_flagadd_encryption_flags)parsers    r   ArgszCat.Args^   s    
S/LM
4	  6
 __ ' (	  )& 
%%f-	vtDr   c           
         t        j                  |       |j                  rg }|j                  D ]p  }t        j                  |      }t        |t        j                        s.t        j                  dj                  |j                              |j                  |       r t        j                  |j                  t        j                  j                   t        j"                  j$                        }t'        j(                  ||j*                  t-        |j.                  dd      t-        |j.                  dd             }t1        j2                  |      | _        y )Nz@cat only works for valid cloud URLs. {} is an invalid cloud URL.)fields_scoperecursion_requestedr   r   r   )
start_byteend_byte)task_iterator)r	   initialize_key_storer%   r   storage_url_from_string
isinstanceCloudUrlr
   InvalidUrlErrorformat
url_stringappendr   NameExpansionIteratorr   FieldsScopeNO_ACLRecursionSettingNOr   get_cat_task_iteratordisplay_urlgetattrranger   execute_tasks	exit_code)selfargsstorage_urlsr@   
url_objectsource_expansion_iteratorr9   s          r   RunzCat.Run|   s   ((.xxl* 88D
*k&:&:;&& (FFLf+5+@+@GBC C 	J' ! !/ D D**11*;;>>!@
 &;;!4::w2UD1	3M
 #00}MDNr   N)__name__
__module____qualname____doc__detailed_helpstaticmethodr3   rR    r   r   r!   r!   8   s3    ;

 -D E E:Nr   r!   )rV   
__future__r   r   r   r.   googlecloudsdk.api_lib.storager   googlecloudsdk.callioper   r   "googlecloudsdk.command_lib.storager	   r
   r   r   r   (googlecloudsdk.command_lib.storage.tasksr   ,googlecloudsdk.command_lib.storage.tasks.catr   r   UniverseCompatibleCommandr!   rY   r   r   <module>rb      se    K &  '  4 / ( > 5 4 = : B J/* ]N$,, ]N ]Nr   