Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remplace named tuples with enums #1250

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions docs/api/utils.rst
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
Utils
=====

.. autonamedtuple:: gspread.utils.Dimension
.. autonamedtuple:: gspread.utils.DateTimeOption
.. autonamedtuple:: gspread.utils.ExportFormat
.. autonamedtuple:: gspread.utils.MimeType
.. autonamedtuple:: gspread.utils.PasteOrientation
.. autonamedtuple:: gspread.utils.PasteType
.. autonamedtuple:: gspread.utils.ValueInputOption
.. autonamedtuple:: gspread.utils.ValueRenderOption

.. automodule:: gspread.utils
:members:
:undoc-members:
2 changes: 1 addition & 1 deletion gspread/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ def export(self, file_id: str, format: str = ExportFormat.PDF) -> bytes:

See `ExportFormat`_ in the Drive API.

:type format: :namedtuple:`~gspread.utils.ExportFormat`
:type format: :class:`~gspread.utils.ExportFormat`

:returns bytes: The content of the exported file.

Expand Down
2 changes: 1 addition & 1 deletion gspread/spreadsheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ def export(self, format=ExportFormat.PDF):

See `ExportFormat`_ in the Drive API.
Default value is ``ExportFormat.PDF``.
:type format: :namedtuple:`~gspread.utils.ExportFormat`
:type format: :class:`~gspread.utils.ExportFormat`

:returns bytes: The content of the exported file.

Expand Down
114 changes: 53 additions & 61 deletions gspread/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""

import re
from collections import defaultdict, namedtuple
from collections import defaultdict
from collections.abc import Sequence
from functools import wraps
from itertools import chain
Expand All @@ -29,6 +29,7 @@
from google.auth.credentials import Credentials as Credentials
from google.oauth2.credentials import Credentials as UserCredentials
from google.oauth2.service_account import Credentials as ServiceAccountCredentials
from strenum import StrEnum

from .exceptions import IncorrectCellLabel, InvalidInputValue, NoValidUrlKeyFound

Expand All @@ -43,69 +44,60 @@
URL_KEY_V1_RE = re.compile(r"key=([^&#]+)")
URL_KEY_V2_RE = re.compile(r"/spreadsheets/d/([a-zA-Z0-9-_]+)")

Dimension = namedtuple("Dimension", ["rows", "cols"])("ROWS", "COLUMNS")
ValueRenderOption = namedtuple(
"ValueRenderOption", ["formatted", "unformatted", "formula"]
)("FORMATTED_VALUE", "UNFORMATTED_VALUE", "FORMULA")
ValueInputOption = namedtuple("ValueInputOption", ["raw", "user_entered"])(
"RAW", "USER_ENTERED"
)
DateTimeOption = namedtuple(
"DateTimeOption", ["serial_number", "formatted_string", "formated_string"]
)("SERIAL_NUMBER", "FORMATTED_STRING", "FORMATTED_STRING")
MimeTypeType = namedtuple(
"MimeType",
["google_sheets", "pdf", "excel", "csv", "open_office_sheet", "tsv", "zip"],
)
MimeType = MimeTypeType(
"application/vnd.google-apps.spreadsheet",
"application/pdf",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
"text/csv",
"application/vnd.oasis.opendocument.spreadsheet",
"text/tab-separated-values",
"application/zip",
)
ExportFormatType = namedtuple(
"ExportFormat", ["PDF", "EXCEL", "CSV", "OPEN_OFFICE_SHEET", "TSV", "ZIPPED_HTML"]
)
ExportFormat = ExportFormatType(
MimeType.pdf,
MimeType.excel,
MimeType.csv,
MimeType.open_office_sheet,
MimeType.tsv,
MimeType.zip,
)

PasteType = namedtuple(
"PasteType",
[
"normal",
"values",
"format",
"no_borders",
"formula",
"data_validation",
"conditional_formating",
],
)(
"PASTE_NORMAL",
"PASTE_VALUES",
"PASTE_FORMAT",
"PASTE_NO_BORDERS",
"PASTE_FORMULA",
"PASTE_DATA_VALIDATION",
"PASTE_CONDITIONAL_FORMATTING",
)
class Dimension(StrEnum):
rows = "ROWS"
cols = "COLUMNS"

PasteOrientation = namedtuple("PasteOrientation", ["normal", "transpose"])(
"NORMAL", "TRANSPOSE"
)

DEPRECATION_WARNING_TEMPLATE = (
"[Deprecated][in version {v_deprecated}]: {msg_deprecated}"
)
class ValueRenderOption(StrEnum):
formatted = "FORMATTED_VALUE"
unformatted = "UNFORMATTED_VALUE"
formula = "FORMULA"


class ValueInputOption(StrEnum):
raw = "RAW"
user_entered = "USER_ENTERED"


class DateTimeOption(StrEnum):
serial_number = "SERIAL_NUMBER"
formatted_string = "FORMATTED_STRING"
alifeee marked this conversation as resolved.
Show resolved Hide resolved


class MimeType(StrEnum):
google_sheets = "application/vnd.google-apps.spreadsheet"
pdf = "application/pdf"
excel = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
csv = "text/csv"
open_office_sheet = "application/vnd.oasis.opendocument.spreadsheet"
tsv = "text/tab-separated-values"
zip = "application/zip"


class ExportFormat(StrEnum):
PDF = MimeType.pdf
EXCEL = MimeType.excel
CSV = MimeType.csv
OPEN_OFFICE_SHEET = MimeType.open_office_sheet
TSV = MimeType.tsv
ZIPPED_HTML = MimeType.zip


class PasteType(StrEnum):
normal = "PASTE_NORMAL"
values = "PASTE_VALUES"
format = "PASTE_FORMAT"
no_borders = "PASTE_NO_BORDERS"
formula = "PASTE_NO_BORDERS"
data_validation = "PASTE_DATA_VALIDATION"
conditional_formating = "PASTE_CONDITIONAL_FORMATTING"


class PasteOrientation(StrEnum):
normal = "NORMAL"
transpose = "TRANSPOSE"


def convert_credentials(credentials: Credentials) -> Credentials:
Expand Down
Loading