
    Yia=                         S 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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

    )
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DRIVE_FILES_UPLOAD_API_V2_URL)ExportFormatMimeTypeextract_id_from_urlfinditemc                   n   \ rS rSrSrS\4S\S\\   S\	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\\\\\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\R6                  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S4S jr"S\S\\\\!\\4   4      4S jr#      S-S\S \\   S!\\   S"\\   S#\S$\\   S%\S\4S& jjr$S\S'\SS4S( jr%S)r&g).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       9/app/.venv/lib/python3.13/site-packages/gspread/client.py__init__Client.__init__&   s     't5    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%   r*   s     r&   r,   Client.get_file_drive_metadata.   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.
)r.   r/   )_list_spreadsheet_files)r%   r.   r/   files_s        r&   list_spreadsheet_filesClient.list_spreadsheet_files5   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paramsr2   nextPageToken)r   r   google_sheetsr    requestjsonextendr?   )
r%   r.   r/   r2   
page_tokenurlqueryrA   responseresponse_jsons
             r&   r1   Client._list_spreadsheet_filesG   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 )xr.   s    r&   <lambda>Client.open.<locals>.<lambda>~   s    !F)u,r)   NrO   r.   )r1   r   StopIterationr   r   r    )r%   r.   r/   spreadsheet_filesrJ   
propertiesexs    `     r&   openClient.openk   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')
r*   N)
r   r    r   rJ   status_coder   	NOT_FOUNDr   	FORBIDDENPermissionError)r%   rZ   spreadsheetrW   s       r&   open_by_keyClient.open_by_key   s    	%d&6&6sDK   	{{&&**>*>>)"++6B>{{&&**>*>>%2-H	s    
BA/BBrH   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')
)ra   r   )r%   rH   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.
rO   r.   rP   )r4   r   r    dict)r%   r.   rU   spreadrQ   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.

rO   mimeTyper;   TparentspostrE   rA   r*   )r   rC   r    rD   r   rE   ra   )r%   r.   r/   payloadrA   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
)rv   rw   )r    export)r%   rv   rw   s      r&   ry   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 H6  nUR                  S
5      (       a  M  UR                  US   US   US   SS9  M8     USL a  [        U-  nSn/ nSSSS.nUbX  UUS'   U R                  R                  SXS9R                  5       nUR                  US   5        UR                  SS5      nUb  MX  [        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
{}/{}/copyrl   Nrn   r;   Tro   rp   r*   deletedemailAddresstyperoleF)email_address	perm_typer   notifyr7   z.comments/content,comments/anchor,nextPageTokend   )r=   includeDeletedr:   r>   r?   r@   commentsrB   r=   )rw   r   r   rC   r    rD   rE   ra   list_permissionsr?   sharer   rF   r*   )r%   rv   r.   r{   r/   r|   rH   rq   rA   rr   rs   new_spreadsheetoriginalpermissionsp
source_urlrG   r   resdestination_urlcomments                        r&   copyClient.copy   s   V !!"8'B  ..#

  "+GI148$$VSv$N$**>:t#''0H"335K 55##%%"#N"3i6 	 & 	 ! 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).
{}/{}r;   Tdeleter@   N)rw   r   r    rD   )r%   rv   rH   rA   s       r&   del_spreadsheetClient.del_spreadsheetZ  s8    
 nn3W=148  3 >r)   datac           	          [        U5      [        L a  UR                  S5      nSS0nSR                  [        U5      nU R
                  R                  SU[        W5      SSSS.US	9  g
)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.

zutf-8zContent-Typeztext/csvr   putmediaT)
uploadTypeconvertr;   )r   rA   headersN)r   strencoderw   r   r    rD   bytes)r%   rv   r   rq   r   rH   s         r&   
import_csvClient.import_csvd  sr    , :kk'*G!:.nn:GD  w%%)
  	! 
	
r)   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%   rv   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%   rv   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%   rv   r   s      r&   r   Client.remove_permission  s     	**7Br)   r$   )NNr#   )NFNT)NNNTNF)'__name__
__module____qualname____firstlineno____doc__r   r
   r   r   r   r'   r   r   r,   r   r   r4   r   r   r1   r   rX   ra   rd   ri   rt   r   PDFr   ry   boolr   r   r	   r   r   r   r   __static_attributes__rP   r)   r&   r   r      s   	 &*&0	66 '"6 $	6
 
6<# <# < GKc]6>sm	d38n	& %)"	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:  $!&#'"dd }d 	d
 C=d d 
dL?s ?t ?&
# &
U3:-> &
4 &
P: :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   httpr   typingr   r   r   r   r   r	   google.auth.credentialsr
   requestsr   r   
exceptionsr   r   r    r   r   r   r`   r   urlsr   r   r   utilsr   r   r   r   r   rP   r)   r&   <module>r      sH     : : / & 5 ? ? $ 
 I HuC uCr)   