
    Kii@                         S r SSKJr  SSKJr  SSKJrJrJrJrJ	r	J
r
  SSKJr  SSKJrJr  SSKJrJr  SS	KJrJrJr  SS
KJr  SSKJrJr  SSKJrJrJrJ r    " S S5      r!g)zn
gspread.client
~~~~~~~~~~~~~~

This module contains Client class responsible for managing spreadsheet files

    )datetime)
HTTPStatus)AnyDictListOptionalTupleUnion)Credentials)ResponseSession   )APIErrorSpreadsheetNotFound)
HTTPClientHTTPClientType
ParamsType)Spreadsheet)DRIVE_FILES_API_V3_COMMENTS_URLDRIVE_FILES_API_V3_URL)ExportFormatMimeTypeextract_id_from_urlfinditemc                      \ rS rSrSrS\4S\S\\   S\	SS4S jjr
\S\\   4S	 j5       r S-S
\\\\\\4   4      SS4S jjrS\S\4S jr S.S\\   S\\   S\\\\4      4S jjr S.S\\   S\\   S\\\\\4      \4   4S jjrS-S\S\\   S\4S jjrS\S\4S jrS\S\4S jrS-S\\   S\\   4S jjrS-S\S\\   S\4S jjr\ RB                  4S\S\S\"4S jjr#    S/S\S\\   S\$S\\   S\$S\4S jjr%S\SS4S jr&S\S \\\"4   S\4S! jr'S\S\\\\\\$4   4      4S" jr(      S0S\S#\\   S$\\   S%\\   S&\$S'\\   S(\$S\4S) jjr)S\S*\SS4S+ jr*S,r+g)1Client   a  An instance of this class Manages Spreadsheet files

It is used to:
    - open/create/list/delete spreadsheets
    - create/delete/list spreadsheet permission
    - etc

It is the gspread entry point.
It will handle creating necessary :class:`~gspread.models.Spreadsheet` instances.
Nauthsessionhttp_clientreturnc                     U" X5      U l         g Nr    )selfr   r   r    s       aC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\gspread/client.py__init__Client.__init__#   s     't5    c                 B    U R                   R                  R                  $ )zReturns the expiry date of the curenlty loaded credentials

:returns: (optional) datetime the expiry date time object.

.. note::

   It only applies to gspread client created using oauth
)r    r   expiry)r%   s    r&   r+   Client.expiry+   s     $$+++r)   timeoutc                 :    U R                   R                  U5        g)zHow long to wait for the server to send
data before giving up, as a float, or a ``(connect timeout,
read timeout)`` tuple.

Use value ``None`` to restore default timeout

Value for ``timeout`` is in seconds (s).
N)r    set_timeout)r%   r-   s     r&   r/   Client.set_timeout7   s     	$$W-r)   idc                 8    U R                   R                  U5      $ )zGet the metadata from the Drive API for a specific file
This method is mainly here to retrieve the create/update time
of a file (these metadata are only accessible from the Drive API).
)r    get_file_drive_metadata)r%   r1   s     r&   r3   Client.get_file_drive_metadataD   s    
 77;;r)   title	folder_idc                 (    U R                  XS9u  p4U$ )a  List all the spreadsheet files

Will list all spreadsheet files owned by/shared with this user account.

:param str title: Filter only spreadsheet files with this title
:param str folder_id: Only look for spreadsheet files in this folder
    The parameter ``folder_id`` can be obtained from the URL when looking at
    a folder in a web browser as follow:
    ``https://drive.google.com/drive/u/0/folders/<folder_id>``

:returns: a list of dicts containing the keys id, name, createdTime and modifiedTime.
)r5   r6   )_list_spreadsheet_files)r%   r5   r6   files_s        r&   list_spreadsheet_filesClient.list_spreadsheet_filesK   s     //e/Qr)   c                 X   / nSn[         nS[        R                   S3nU(       a	  USU S3-  nU(       a	  USU S3-  nUSSSSS	.n U(       a  XGS
'   U R                  R	                  SXWS9nUR                  5       n	UR                  U	S   5        U	R                  SS 5      nUc   X84$ Md  )N z
mimeType=""z and name = "z and parents in "i  Tz:kind,nextPageToken,files(id,name,createdTime,modifiedTime))qpageSizesupportsAllDrivesincludeItemsFromAllDrivesfields	pageTokengetparamsr9   nextPageToken)r   r   google_sheetsr    requestjsonextendrF   )
r%   r5   r6   r9   
page_tokenurlqueryrH   responseresponse_jsons
             r&   r8   Client._list_spreadsheet_files]   s     
$X334A6}UG1--E(155E !%)-R
 &0{#''//s/JH$MMOMLLw/0&**?DAJ! r)   c                    ^ U R                  TU5      u  p4 [        U4S jU5      nUS   US'   [	        U R
                  U5      $ ! [         a  n[        U5      UeSnAff = f)a  Opens a spreadsheet.

:param str title: A title of a spreadsheet.
:param str folder_id: (optional) If specified can be used to filter
    spreadsheets by parent folder ID.
:returns: a :class:`~gspread.models.Spreadsheet` instance.

If there's more than one spreadsheet with same title the first one
will be opened.

:raises gspread.SpreadsheetNotFound: if no spreadsheet with
                                     specified `title` is found.

>>> gc.open('My fancy spreadsheet')
c                    > U S   T:H  $ )Nname )xr5   s    r&   <lambda>Client.open.<locals>.<lambda>   s    !F)u,r)   NrV   r5   )r8   r   StopIterationr   r   r    )r%   r5   r6   spreadsheet_filesrQ   
propertiesexs    `     r&   openClient.open   ss      '+&B&B5)&T#	8!,!J )0
74++Z88  	8%h/R7	8s   A 
A AA keyc                 :    [        U R                  SU05      nU$ ! [         at  nUR                  R                  [
        R                  :X  a  [        UR                  5      UeUR                  R                  [
        R                  :X  a  [        UeUeSnAff = f)a  Opens a spreadsheet specified by `key` (a.k.a Spreadsheet ID).

:param str key: A key of a spreadsheet as it appears in a URL in a browser.
:returns: a :class:`~gspread.models.Spreadsheet` instance.

>>> gc.open_by_key('0BmgG6nO_6dprdS1MN3d3MkdPa142WFRrdnRRUWl1UFE')
r1   N)
r   r    r   rQ   status_coder   	NOT_FOUNDr   	FORBIDDENPermissionError)r%   ra   spreadsheetr^   s       r&   open_by_keyClient.open_by_key   s    	%d&6&6sDK   	{{&&**>*>>)"++6B>{{&&**>*>>%2-H	s    
BA/BBrO   c                 6    U R                  [        U5      5      $ )aq  Opens a spreadsheet specified by `url`.

:param str url: URL of a spreadsheet as it appears in a browser.

:returns: a :class:`~gspread.models.Spreadsheet` instance.

:raises gspread.SpreadsheetNotFound: if no spreadsheet with
                                     specified `url` is found.

>>> gc.open_by_url('https://docs.google.com/spreadsheet/ccc?key=0Bm...FE&hl')
)rh   r   )r%   rO   s     r&   open_by_urlClient.open_by_url   s      3C 899r)   c                     U R                  U5      nU(       a  U Vs/ s H  o1US   :X  d  M  UPM     nnU Vs/ s H'  n[        U R                  [        SSUS   0UD65      PM)     sn$ s  snf s  snf )zOpens all available spreadsheets.

:param str title: (optional) If specified can be used to filter
    spreadsheets by title.

:returns: a list of :class:`~gspread.models.Spreadsheet` instances.
rV   r5   rW   )r;   r   r    dict)r%   r5   r\   spreadrX   s        r&   openallClient.openall   s     !77>%6!%666&>:Q%6  ! '
& (($*DQvY*D!*DE&
 	
	!
s   A*A*.A/c                     U[         R                  S.nSS0nUb  U/US'   U R                  R                  S[        X4S9nUR                  5       S   nU R                  U5      $ )zCreates a new spreadsheet.

:param str title: A title of a new spreadsheet.

:param str folder_id: Id of the folder where we want to save
    the spreadsheet.

:returns: a :class:`~gspread.models.Spreadsheet` instance.

rV   mimeTyperB   TparentspostrL   rH   r1   )r   rJ   r    rK   r   rL   rh   )r%   r5   r6   payloadrH   rspreadsheet_ids          r&   createClient.create   s~      ..#
  
  "+GI$$* % 
 $//r)   file_idformatc                 4    U R                   R                  XS9$ )ao  Export the spreadsheet in the given format.

:param str file_id: The key of the spreadsheet to export

:param str format: The format of the resulting file.
    Possible values are:

        * ``ExportFormat.PDF``
        * ``ExportFormat.EXCEL``
        * ``ExportFormat.CSV``
        * ``ExportFormat.OPEN_OFFICE_SHEET``
        * ``ExportFormat.TSV``
        * ``ExportFormat.ZIPPED_HTML``

    See `ExportFormat`_ in the Drive API.

:type format: :class:`~gspread.utils.ExportFormat`

:returns bytes: The content of the exported file.

.. _ExportFormat: https://developers.google.com/drive/api/guides/ref-export-formats
)r}   r~   )r    export)r%   r}   r~   s      r&   r   Client.export   s    0 &&w&FFr)   copy_permissionscopy_commentsc           	         SR                  [        U5      nU[        R                  S.nUb  U/US'   SS0nU R                  R                  SXgUS9n	U	R                  5       S	   n
U R                  U
5      nUSL a  U R                  U5      nUR                  5       nU H  nUR                  S
5      (       a  M  Sn[        US   5      S:X  a  [        US   5      nO [        US   5      S;   a  [        US   5      nUR                  U[        US   5      [        US   5      SS9  M     USL a  [        U-  nSn/ nSSSS.nUbY  UUS'   U R                  R                  SUUS9R                  5       nUR                  US   5        UR                  SS5      nUb  MY  [        UR                  -  nSS	0nU H  nU R                  R                  SUUUS9  M!     U$ )a  Copies a spreadsheet.

:param str file_id: A key of a spreadsheet to copy.
:param str title: (optional) A title for the new spreadsheet.

:param bool copy_permissions: (optional) If True, copy permissions from
    the original spreadsheet to the new spreadsheet.

:param str folder_id: Id of the folder where we want to save
    the spreadsheet.

:param bool copy_comments: (optional) If True, copy the comments from
    the original spreadsheet to the new spreadsheet.

:returns: a :class:`~gspread.models.Spreadsheet` instance.

.. versionadded:: 3.1.0

.. note::

   If you're using custom credentials without the Drive scope, you need to add
   ``https://www.googleapis.com/auth/drive`` to your OAuth scope in order to use
   this method.

   Example::

      scope = [
          'https://www.googleapis.com/auth/spreadsheets',
          'https://www.googleapis.com/auth/drive'
      ]

   Otherwise, you will get an ``Insufficient Permission`` error
   when you try to copy a spreadsheet.

z
{}/{}/copyrs   Nru   rB   Trv   rw   r1   deletedr>   typedomain)usergroupemailAddressroleF)email_address	perm_typer   notifyz.comments/content,comments/anchor,nextPageTokend   )rD   includeDeletedrA   rE   rF   rG   commentsrI   rD   )r~   r   r   rJ   r    rK   rL   rh   list_permissionsrF   strsharer   rM   r1   )r%   r}   r5   r   r6   r   rO   rx   rH   ry   rz   new_spreadsheetoriginalpermissionspemail_or_domain
source_urlrN   r   resdestination_urlcomments                         r&   copyClient.copy
  s   V !!"8'B  ..#

  "+GI148$$VSv$N$**>:t#''0H"335K 55## #%qy>X-&)!H+&6O6^'88&)!N*;&<O%%"1!!F)nQvY 	 &  !* D 8GDJJHJ"'F (&0{#&&..uj.PUUWJ0 WW_d;
 ( >ASASTO %F#  ((O'& )  $
 r)   c                 l    SR                  [        U5      nSS0nU R                  R                  SX#S9  g)zNDeletes a spreadsheet.

:param str file_id: a spreadsheet ID (a.k.a file ID).
z{}/{}rB   TdeleterG   N)r~   r   r    rK   )r%   r}   rO   rH   s       r&   del_spreadsheetClient.del_spreadsheetz  s8    
 nn3W=148  3 >r)   datac                 8    U R                   R                  X5      $ )a  Imports data into the first page of the spreadsheet.

:param str file_id:
:param str data: A CSV string of data.

Example:

.. code::

    # Read CSV file contents
    content = open('file_to_import.csv', 'r').read()

    gc.import_csv(spreadsheet.id, content)

.. note::

   This method removes all other worksheets and then entirely
   replaces the contents of the first worksheet.

)r    
import_csv)r%   r}   r   s      r&   r   Client.import_csv  s    * **799r)   c                 8    U R                   R                  U5      $ )z`Retrieve a list of permissions for a file.

:param str file_id: a spreadsheet ID (aka file ID).
)r    r   )r%   r}   s     r&   r   Client.list_permissions  s    
 0099r)   valuer   r   r   email_message	with_linkc           	      >    U R                   R                  XX4XVU5      $ )ax  Creates a new permission for a file.

:param str file_id: a spreadsheet ID (aka file ID).
:param value: user or group e-mail address, domain name
    or None for 'anyone' type.
:type value: str, None
:param str perm_type: (optional) The account type.
    Allowed values are: ``user``, ``group``, ``domain``, ``anyone``
:param str role: (optional) The primary role for this user.
    Allowed values are: ``owner``, ``writer``, ``reader``
:param bool notify: (optional) Whether to send an email to the target
    user/domain.
:param str email_message: (optional) An email message to be sent
    if ``notify=True``.
:param bool with_link: (optional) Whether the link is required for this
    permission to be active.

:returns dict: the newly created permission

Examples::

    # Give write permissions to otto@example.com

    gc.insert_permission(
        '0BmgG6nO_6dprnRRUWl1UFE',
        'otto@example.org',
        perm_type='user',
        role='writer'
    )

    # Make the spreadsheet publicly readable

    gc.insert_permission(
        '0BmgG6nO_6dprnRRUWl1UFE',
        None,
        perm_type='anyone',
        role='reader'
    )

)r    insert_permission)r%   r}   r   r   r   r   r   r   s           r&   r   Client.insert_permission  s'    d 11IVI
 	
r)   permission_idc                 :    U R                   R                  X5        g)zDeletes a permission from a file.

:param str file_id: a spreadsheet ID (aka file ID.)
:param str permission_id: an ID for the permission.
N)r    remove_permission)r%   r}   r   s      r&   r   Client.remove_permission  s     	**7Br)   r$   r#   )NN)NFNT)NNNTNF),__name__
__module____qualname____firstlineno____doc__r   r   r   r   r   r'   propertyr   r+   r
   floatr	   r/   r   r   r3   r   r   r;   r   r8   r   r_   rh   rk   rp   r{   r   PDFbytesr   boolr   r   r   r   r   r   __static_attributes__rW   r)   r&   r   r      s   	 &*&0	66 '"6 $	6
 
6 	,* 	, 	, FJ.eU5%<-@&@ AB.	.<# <# < GKc]6>sm	d38n	& GK"c]"6>sm"	tDcN#X-	."H9# 9(3- 9; 9<s { $:s :{ :
Xc] 
d;6G 
(0C 0HSM 0[ 0: 2>1A1A Gc G3 Ge G:  $!&#'"nn }n 	n
 C=n n 
n`?s ?t ?:# :U3:-> :3 :.: :T#uS$Y?O:O5P0Q :  $#'"'+4
4
 }4
 C=	4

 sm4
 4
  }4
 4
 
4
lC CS CT Cr)   r   N)"r   r   httpr   typingr   r   r   r   r	   r
   google.auth.credentialsr   requestsr   r   
exceptionsr   r   r    r   r   r   rg   r   urlsr   r   utilsr   r   r   r   r   rW   r)   r&   <module>r      sA      : : / & 5 ? ? $ I H HGC GCr)   