U
    hu                     @  sX   d Z ddlmZ ddlZddlmZ ddddd	Zdddd
dZdddddZdS )z Utilities for working with URLs.    )annotationsN)parsestrNone)partreturnc                 C  s   t d| std| d S )Nz[a-zA-Z0-9_-]*z,"{}" is outside the restricted character set)re	fullmatch
ValueErrorformat)r    r   T/tmp/pip-unpacked-wheel-doshhd5e/google/generativeai/notebook/sheets_sanitize_url.py_validate_url_part   s    r   c                 C  s6   t |  D ]"\}}t| |D ]}t| q"qd S )N)r   parse_qsitemsr   )r   keyvaluesvaluer   r   r   _validate_url_query_or_fragment   s    r   )urlr   c              
   C  s6  t | }|jdkr$td|j|jdkr>td|jz|jdD ]}t| qLW n4 tk
r } ztd|j|W 5 d}~X Y nX |j	rtd|j	zt
|j W n4 tk
r } ztd	|j|W 5 d}~X Y nX zt
|j W n6 tk
r0 } ztd
|j|W 5 d}~X Y nX | S )a  Sanitize a Sheets URL.

  Run some saftey checks to check whether `url` is a Sheets URL. This is not a
  general-purpose URL sanitizer. Rather, it makes use of the fact that we know
  the URL has to be for Sheets so we can make a few assumptions about (e.g. the
  domain).

  Args:
    url: The url to sanitize.

  Returns:
    The sanitized url.

  Raises:
    ValueError: If `url` does not match the expected restrictions for a Sheets
    URL.
  httpsz/Scheme for Sheets url must be "https", got "{}")zdocs.google.comzsheets.googleapis.comz9Domain for Sheets url must be "docs.google.com", got "{}"/z%Invalid path for Sheets url, got "{}"Nz(Params component must be empty, got "{}"z&Invalid query for Sheets url, got "{}"z)Invalid fragment for Sheets url, got "{}")r   urlparseschemer
   r   netlocpathsplitr   paramsr   queryfragment)r   Zparse_resultr   excr   r   r   sanitize_sheets_url$   sZ    





r!   )	__doc__
__future__r   r   urllibr   r   r   r!   r   r   r   r   <module>   s   