69 lines
2.2 KiB
Python
69 lines
2.2 KiB
Python
#
|
|
# Copyright (c) 2023 Airbyte, Inc., all rights reserved.
|
|
#
|
|
|
|
|
|
import re
|
|
|
|
import unidecode
|
|
from requests.status_codes import codes as status_codes
|
|
|
|
TOKEN_PATTERN = re.compile(r"[A-Z]+[a-z]*|[a-z]+|\d+|(?P<NoToken>[^a-zA-Z\d]+)")
|
|
DEFAULT_SEPARATOR = "_"
|
|
|
|
|
|
def name_conversion(text: str) -> str:
|
|
"""
|
|
convert name using a set of rules, for example: '1MyName' -> '_1_my_name'
|
|
"""
|
|
text = unidecode.unidecode(text)
|
|
|
|
tokens = []
|
|
for m in TOKEN_PATTERN.finditer(text):
|
|
if m.group("NoToken") is None:
|
|
tokens.append(m.group(0))
|
|
else:
|
|
tokens.append("")
|
|
|
|
if len(tokens) >= 3:
|
|
tokens = tokens[:1] + [t for t in tokens[1:-1] if t] + tokens[-1:]
|
|
|
|
if tokens and tokens[0].isdigit():
|
|
tokens.insert(0, "")
|
|
|
|
text = DEFAULT_SEPARATOR.join(tokens)
|
|
text = text.lower()
|
|
return text
|
|
|
|
|
|
def safe_name_conversion(text: str) -> str:
|
|
if not text:
|
|
return text
|
|
new = name_conversion(text)
|
|
if not new:
|
|
raise Exception(f"initial string '{text}' converted to empty")
|
|
return new
|
|
|
|
|
|
def exception_description_by_status_code(code: int, spreadsheet_id) -> str:
|
|
if code in [status_codes.INTERNAL_SERVER_ERROR, status_codes.BAD_GATEWAY, status_codes.SERVICE_UNAVAILABLE]:
|
|
return (
|
|
"There was an issue with the Google Sheets API. This is usually a temporary issue from Google's side."
|
|
" Please try again. If this issue persists, contact support"
|
|
)
|
|
if code == status_codes.FORBIDDEN:
|
|
return (
|
|
f"The authenticated Google Sheets user does not have permissions to view the spreadsheet with id {spreadsheet_id}. "
|
|
"Please ensure the authenticated user has access to the Spreadsheet and reauthenticate. If the issue persists, contact support"
|
|
)
|
|
if code == status_codes.NOT_FOUND:
|
|
return (
|
|
f"The requested Google Sheets spreadsheet with id {spreadsheet_id} does not exist. "
|
|
f"Please ensure the Spreadsheet Link you have set is valid and the spreadsheet exists. If the issue persists, contact support"
|
|
)
|
|
|
|
if code == status_codes.TOO_MANY_REQUESTS:
|
|
return "Rate limit has been reached. Please try later or request a higher quota for your account."
|
|
|
|
return ""
|