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 1 commit
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:
10 changes: 5 additions & 5 deletions gspread/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def list_spreadsheet_files(
page_token = ""
url = DRIVE_FILES_API_V3_URL

q = 'mimeType="{}"'.format(MimeType.google_sheets)
q = 'mimeType="{}"'.format(MimeType.google_sheets.value)
if title:
q += ' and name = "{}"'.format(title)
if folder_id:
Expand Down Expand Up @@ -166,7 +166,7 @@ def create(self, title: str, folder_id: Optional[str] = None) -> Spreadsheet:
"""
payload = {
"name": title,
"mimeType": MimeType.google_sheets,
"mimeType": MimeType.google_sheets.value,
}

params: ParamsType = {
Expand Down 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 All @@ -211,7 +211,7 @@ def export(self, file_id: str, format: str = ExportFormat.PDF) -> bytes:

url = "{}/{}/export".format(DRIVE_FILES_API_V3_URL, file_id)

params: ParamsType = {"mimeType": format}
params: ParamsType = {"mimeType": format.value}

r = self.http_client.request("get", url, params=params)
return r.content
Expand Down Expand Up @@ -263,7 +263,7 @@ def copy(

payload = {
"name": title,
"mimeType": MimeType.google_sheets,
"mimeType": MimeType.google_sheets.value,
}

if folder_id is not None:
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
121 changes: 60 additions & 61 deletions gspread/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
"""

import re
from collections import defaultdict, namedtuple
from collections import defaultdict
from collections.abc import Sequence
from enum import Enum
from functools import wraps
from itertools import chain
from typing import (
Expand All @@ -19,6 +20,7 @@
Dict,
Iterable,
List,
MutableMapping,
Optional,
Tuple,
TypeVar,
Expand All @@ -43,69 +45,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(Enum):
rows = "ROWS"
cols = "COLUMNS"

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

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


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


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


class MimeType(Enum):
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(Enum):
PDF = MimeType.pdf.value
EXCEL = MimeType.excel.value
CSV = MimeType.csv.value
OPEN_OFFICE_SHEET = MimeType.open_office_sheet.value
TSV = MimeType.tsv.value
ZIPPED_HTML = MimeType.zip.value


class PasteType(Enum):
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(Enum):
normal = "NORMAL"
transpose = "TRANSPOSE"


def convert_credentials(credentials: Credentials) -> Credentials:
Expand Down Expand Up @@ -153,6 +146,12 @@ def _convert_service_account(credentials: Any) -> Credentials:
T = TypeVar("T")


def extract_enum_values(
params: MutableMapping[str, Union[None, Enum]]
) -> Dict[str, str]:
return {k: v.value for k, v in params.items() if v is not None}


def finditem(func: Callable[[T], bool], seq: Iterable[T]) -> T:
"""Finds and returns first item in iterable for which func(item) is True."""
return next(item for item in seq if func(item))
Expand Down
Loading