Ë
    v»•ji@  ã                   óÈ   — d Z ddlmZ ddlmZ ddlmZmZmZmZm	Z	m
Z
 ddlmZ ddlmZmZ ddlmZmZ dd	lmZmZmZ dd
lmZ ddlmZmZ ddlmZmZmZm Z   G d„ d«      Z!y)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                   ó’  — e Zd ZdZdefdedee   deddfd„Z	e
dee   fd„«       Z	 d+d	eeeeeef   f      ddfd
„Zdedefd„Z	 d,dee   dee   deeeef      fd„Z	 d,dee   dee   deeeeef      ef   fd„Zd+dedee   defd„Zdedefd„Zdedefd„Zd+dee   dee   fd„Zd+dedee   defd„Zej@                  fdedede!fd„Z"	 	 	 	 d-dedee   de#dee   de#defd„Z$deddfd„Z%dedeee!f   defd „Z&dedeeeeee#f   f      fd!„Z'	 	 	 	 	 	 d.ded"ee   d#ee   d$ee   d%e#d&ee   d'e#defd(„Z(ded)eddfd*„Z)y)/ÚClienta7  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                 ó    —  |||«      | _         y ©N)r   )Úselfr   r   r   s       úA/var/www/html/venv/lib/python3.12/site-packages/gspread/client.pyÚ__init__zClient.__init__#   s   € ñ ' t¨WÓ5ˆÕó    c                 óB   — | j                   j                  j                  S )zÜReturns 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(   zClient.expiry+   s   € ð ×Ñ×$Ñ$×+Ñ+Ð+r&   Útimeoutc                 ó:   — | j                   j                  |«       y)züHow 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+   zClient.set_timeout7   s   € ð 	×Ñ×$Ñ$ WÕ-r&   Úidc                 ó8   — | j                   j                  |«      S )zÑGet 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.   zClient.get_file_drive_metadataD   s   € ð
 ×Ñ×7Ñ7¸Ó;Ð;r&   ÚtitleÚ	folder_idc                 ó2   — | j                  ||¬«      \  }}|S )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/   r0   )Ú_list_spreadsheet_files)r#   r/   r0   ÚfilesÚ_s        r$   Úlist_spreadsheet_fileszClient.list_spreadsheet_filesK   s"   € ð ×/Ñ/°eÀyÐ/ÓQ‰ˆˆqØˆr&   c                 óB  — g }d}t         }dt        j                  › d�}|r	|d|› d�z  }|r	|d|› d�z  }|ddddd	œ}	 |r||d
<   | j                  j	                  d||¬«      }|j                  «       }	|j                  |	d   «       |	j                  dd «      }|€	 ||fS Œc)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©Úparamsr3   ÚnextPageToken)r   r   Úgoogle_sheetsr   ÚrequestÚjsonÚextendr?   )
r#   r/   r0   r3   Ú
page_tokenÚurlÚqueryrA   ÚresponseÚresponse_jsons
             r$   r2   zClient._list_spreadsheet_files]   sâ   € ð ˆØˆ
Ü$ˆàœX×3Ñ3Ð4°AÐ6ˆÙØ�} U G¨1Ð-Ñ-ˆEÙØÐ(¨¨°1Ð5Ñ5ˆEð ØØ!%Ø)-ØRñ
ˆð ÙØ&0��{Ñ#à×'Ñ'×/Ñ/°°sÀ6Ð/ÓJˆHØ$ŸM™M›OˆMØ�L‰L˜ wÑ/Ô0à&×*Ñ*¨?¸DÓAˆJàÐ!Øà�hˆÐð r&   c                 óÄ   ‡— | j                  ‰|«      \  }}	 t        ˆfd„|«      }|d   |d<   t	        | j
                  |«      S # t        $ r}t        |«      |‚d}~ww xY w)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                 ó   •— | d   ‰k(  S )NÚname© )Úxr/   s    €r$   ú<lambda>zClient.open.<locals>.<lambda>”   s   ø€ ˜!˜F™) uÑ,€ r&   NrN   r/   )r2   r   ÚStopIterationr   r   r   )r#   r/   r0   Úspreadsheet_filesrJ   Ú
propertiesÚexs    `     r$   ÚopenzClient.open�   su   ø€ ð  '+×&BÑ&BÀ5È)Ó&TÑ#Ð˜8ð	8Ü!Û,Ø!óˆJð )¨Ñ0ˆ
�7Ñä˜4×+Ñ+¨ZÓ8Ð8øô ò 	8Ü% hÓ/°RÐ7ûð	8ús   ˜A Á	AÁAÁAÚkeyc                 ó4  — 	 t        | j                  d|i«      }|S # t        $ rr}|j                  j                  t
        j                  k(  rt        |j                  «      |‚|j                  j                  t
        j                  k(  rt        |‚|‚d}~ww xY w)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#   rW   ÚspreadsheetrU   s       r$   Úopen_by_keyzClient.open_by_keyŸ   s…   € ð	Ü% d×&6Ñ&6¸¸s¸ÓDˆKð Ðøô ò 	Ø�{‰{×&Ñ&¬*×*>Ñ*>Ò>Ü)¨"¯+©+Ó6¸BÐ>Ø�{‰{×&Ñ&¬*×*>Ñ*>Ò>Ü%¨2Ð-ØˆHûð	ús   ‚ œ	B¥A-BÂBrH   c                 ó6   — | j                  t        |«      «      S )a¡  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')
        )r^   r   )r#   rH   s     r$   Úopen_by_urlzClient.open_by_url±   s   € ð ×ÑÔ 3°CÓ 8Ó9Ð9r&   c                 óÐ   — | j                  |«      }|r|D �cg c]  }||d   k(  sŒ|‘Œ }}|D �cg c]&  }t        | j                  t        dd|d   i|¤Ž«      ‘Œ( c}S c c}w c c}w )zãOpens 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.
        rN   r/   rO   )r5   r   r   Údict)r#   r/   rS   ÚspreadrP   s        r$   ÚopenallzClient.openall¿   sƒ   € ð !×7Ñ7¸Ó>Ðáà%6ö!Ø!¸%À6È&Á>Ó:Q’ð!Ðð !ð 'ö
àô ˜×(Ñ(¬$Ñ*D°Q°v±YÐ*DÀ!Ñ*DÕEò
ð 	
ùò	!ùò
s   ˜A¦A°+A#c                 óÎ   — |t         j                  dœ}ddi}|�|g|d<   | j                  j                  dt        ||¬«      }|j                  «       d   }| j                  |«      S )a  Creates 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.

        ©rN   ÚmimeTyper;   TÚparentsÚpost©rE   rA   r,   )r   rC   r   rD   r   rE   r^   )r#   r/   r0   ÚpayloadrA   ÚrÚspreadsheet_ids          r$   ÚcreatezClient.createÓ   s€   € ð Ü ×.Ñ.ñ#
ˆð   ð
ˆð Ð Ø"+ ˆG�IÑà×Ñ×$Ñ$ØÔ*°Àð %ó 
ˆð Ÿ™› $™ˆØ×Ñ Ó/Ð/r&   Úfile_idÚformatc                 ó<   — | j                   j                  ||¬«      S )aß  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
        )ro   rp   )r   Úexport)r#   ro   rp   s      r$   rr   zClient.exportð   s    € ð0 ×Ñ×&Ñ&¨w¸vÐ&ÓFÐFr&   Úcopy_permissionsÚcopy_commentsc           	      ó¬  — dj                  t        |«      }|t        j                  dœ}|�|g|d<   ddi}| j                  j                  d|||¬«      }	|	j                  «       d	   }
| j                  |
«      }|du r§| j                  |«      }|j                  «       }|D ]�  }|j                  d
«      rŒd}t        |d   «      dk(  rt        |d   «      }nt        |d   «      dv rt        |d   «      }|j                  |t        |d   «      t        |d   «      d¬«       Œƒ |du r¬t        |z  }d}g }ddddœ}|�Z||d<   | j                  j                  d||¬«      j                  «       }|j                  |d   «       |j                  dd«      }|�ŒZt        |j                  z  }dd	i}|D ]!  }| j                  j                  d|||¬«       Œ# |S )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
{}/{}/copyrf   Nrh   r;   Tri   rj   r,   Údeletedr7   ÚtypeÚdomain)ÚuserÚgroupÚemailAddressÚroleF)Úemail_addressÚ	perm_typer|   Únotifyz.comments/content,comments/anchor,nextPageTokenéd   )r=   ÚincludeDeletedr:   r>   r?   r@   ÚcommentsrB   r=   )rp   r   r   rC   r   rD   rE   r^   Úlist_permissionsr?   ÚstrÚsharer   rF   r,   )r#   ro   r/   rs   r0   rt   rH   rk   rA   rl   rm   Únew_spreadsheetÚoriginalÚpermissionsÚpÚemail_or_domainÚ
source_urlrG   r‚   ÚresÚdestination_urlÚcomments                         r$   ÚcopyzClient.copy
  s&  € ðV ×!Ñ!Ô"8¸'ÓBˆð Ü ×.Ñ.ñ#
ˆð
 Ð Ø"+ ˆG�IÑà1°4Ð8ˆØ×Ñ×$Ñ$ V¨S°wÀvÐ$ÓNˆØŸ™› $™ˆà×*Ñ*¨>Ó:ˆà˜tÑ#Ø×'Ñ'¨Ó0ˆHà"×3Ñ3Ó5ˆKØ ò �Ø—5‘5˜Ô#Øð #%�Ü�q˜‘y“> XÒ-Ü&)¨!¨H©+Ó&6‘OÜ˜˜6™“^Ð'8Ñ8Ü&)¨!¨NÑ*;Ó&<�Oà×%Ñ%Ø"1Ü! ! F¡)›nÜ˜Q˜v™Y›Ø ð	 &õ ðð* ˜DÑ Ü8¸GÑDˆJØˆJØˆHàJØ"'ØñˆFð Ð(Ø&0��{Ñ#Ø×&Ñ&×.Ñ.¨u°jÈÐ.ÓP×UÑUÓW�à—‘  J¡Ô0Ø ŸW™W _°dÓ;�
ð Ñ(ô >À×ASÑASÑTˆOð  Ð%ˆFØ#ò �Ø× Ñ ×(Ñ(Ø˜O°'À&ð )õ ðð
 Ðr&   c                 ót   — dj                  t        |«      }ddi}| j                  j                  d||¬«       y)z^Deletes a spreadsheet.

        :param str file_id: a spreadsheet ID (a.k.a file ID).
        z{}/{}r;   TÚdeleter@   N)rp   r   r   rD   )r#   ro   rH   rA   s       r$   Údel_spreadsheetzClient.del_spreadsheetz  s:   € ð
 �n‰nÔ3°WÓ=ˆà1°4Ð8ˆØ×Ñ× Ñ  ¨3°vÐ Õ>r&   Údatac                 ó:   — | j                   j                  ||«      S )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#   ro   r“   s      r$   r•   zClient.import_csv„  s   € ð* ×Ñ×*Ñ*¨7°DÓ9Ð9r&   c                 ó8   — | j                   j                  |«      S )zpRetrieve a list of permissions for a file.

        :param str file_id: a spreadsheet ID (aka file ID).
        )r   rƒ   )r#   ro   s     r$   rƒ   zClient.list_permissions›  s   € ð
 ×Ñ×0Ñ0°Ó9Ð9r&   Úvaluer~   r|   r   Úemail_messageÚ	with_linkc           	      óD   — | j                   j                  |||||||«      S )ap  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#   ro   r—   r~   r|   r   r˜   r™   s           r$   r›   zClient.insert_permission¢  s-   € ðd ×Ñ×1Ñ1Ø�U˜I t¨V°]ÀIó
ð 	
r&   Úpermission_idc                 ó<   — | j                   j                  ||«       y)z£Deletes 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#   ro   rœ   s      r$   rž   zClient.remove_permissionØ  s   € ð 	×Ñ×*Ñ*¨7°MÕBr&   r"   )NN)NFNT)NNNTNF)*Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r%   Úpropertyr   r(   r
   Úfloatr	   r+   r„   r   r.   r   r   r5   r   r2   r   rV   r^   r`   rd   rn   r   ÚPDFÚbytesrr   Úboolr�   r’   r•   rƒ   r›   rž   rO   r&   r$   r   r      s  „ ñ	ð &*Ø&0ñ	6àð6ð ˜'Ñ"ð6ð $ð	6ð
 
ó6ð ð	,˜ Ñ*ò 	,ó ð	,ð FJñ.Ø  e¨U°5¸%°<Ñ-@Ð&@Ñ AÑBð.à	ó.ð<¨#ð <°#ó <ð GKñØ˜c‘]ðØ6>¸s±mðà	ˆd�3˜�8‰nÑ	óð& GKñ"Ø˜c‘]ð"Ø6>¸s±mð"à	ˆt�D˜˜c˜‘NÑ# XÐ-Ñ	.ó"ñH9˜#ð 9¨(°3©-ð 9À;ó 9ð<˜sð  {ó ð$:˜sð : {ó :ñ
˜X c™]ð 
°d¸;Ñ6Gó 
ñ(0˜Cð 0¨H°S©Mð 0À[ó 0ð: 2>×1AÑ1Añ G˜cð G¨3ð GÀeó Gð:  $Ø!&Ø#'Ø"ñnàðnð ˜‰}ðnð ð	nð
 ˜C‘=ðnð ðnð 
ónð`? sð ?¨tó ?ð: #ð :¨U°3¸°:Ñ->ð :À3ó :ð.:¨ð :°°T¸#¸uÀSÈ$ÀYÑ?OÐ:OÑ5PÑ0Qó :ð  $Ø#'Ø"ØØ'+Øñ4
àð4
ð ˜‰}ð4
ð ˜C‘=ð	4
ð
 �s‰mð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   r]   r   Úurlsr   r   Úutilsr   r   r   r   r   rO   r&   r$   ú<module>r¯      sA   ðñõ Ý ß :× :å /ß &ç 5ß ?Ñ ?Ý $ß Iß HÓ H÷GCò GCr&   