in google/generativeai/notebook/sheets_sanitize_url.py [0:0]
def sanitize_sheets_url(url: str) -> str:
"""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.
"""
parse_result = parse.urlparse(url)
if parse_result.scheme != "https":
raise ValueError(
'Scheme for Sheets url must be "https", got "{}"'.format(parse_result.scheme)
)
if parse_result.netloc not in ("docs.google.com", "sheets.googleapis.com"):
raise ValueError(
'Domain for Sheets url must be "docs.google.com", got "{}"'.format(parse_result.netloc)
)
# Path component.
try:
for fragment in parse_result.path.split("/"):
_validate_url_part(fragment)
except ValueError as exc:
raise ValueError('Invalid path for Sheets url, got "{}"'.format(parse_result.path)) from exc
# Params component.
if parse_result.params:
raise ValueError('Params component must be empty, got "{}"'.format(parse_result.params))
# Query component.
try:
_validate_url_query_or_fragment(parse_result.query)
except ValueError as exc:
raise ValueError(
'Invalid query for Sheets url, got "{}"'.format(parse_result.query)
) from exc
# Fragment component.
try:
_validate_url_query_or_fragment(parse_result.fragment)
except ValueError as exc:
raise ValueError(
'Invalid fragment for Sheets url, got "{}"'.format(parse_result.fragment)
) from exc
return url