Skip to content

Commit

Permalink
Merge pull request #1426 from burnash/regression/add_missing_import_csv
Browse files Browse the repository at this point in the history
Add missing method `import_csv()`
  • Loading branch information
lavigne958 committed Mar 14, 2024
2 parents 8d55a6b + 494c2b2 commit f34cb6a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 34 deletions.
27 changes: 3 additions & 24 deletions gspread/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
from .exceptions import APIError, SpreadsheetNotFound
from .http_client import HTTPClient, HTTPClientType, ParamsType
from .spreadsheet import Spreadsheet
from .urls import (
DRIVE_FILES_API_V3_COMMENTS_URL,
DRIVE_FILES_API_V3_URL,
DRIVE_FILES_UPLOAD_API_V2_URL,
)
from .urls import DRIVE_FILES_API_V3_COMMENTS_URL, DRIVE_FILES_API_V3_URL
from .utils import ExportFormat, MimeType, extract_id_from_url, finditem


Expand Down Expand Up @@ -364,7 +360,7 @@ def del_spreadsheet(self, file_id: str) -> None:
params: ParamsType = {"supportsAllDrives": True}
self.http_client.request("delete", url, params=params)

def import_csv(self, file_id: str, data: Union[str, bytes]) -> None:
def import_csv(self, file_id: str, data: Union[str, bytes]) -> Any:
"""Imports data into the first page of the spreadsheet.
:param str file_id:
Expand All @@ -385,24 +381,7 @@ def import_csv(self, file_id: str, data: Union[str, bytes]) -> None:
replaces the contents of the first worksheet.
"""
# Make sure we send utf-8
if type(data) is str:
payload = data.encode("utf-8")

headers = {"Content-Type": "text/csv"}
url = "{}/{}".format(DRIVE_FILES_UPLOAD_API_V2_URL, file_id)

self.http_client.request(
"put",
url,
data=bytes(payload),
params={
"uploadType": "media",
"convert": True,
"supportsAllDrives": True,
},
headers=headers,
)
return self.http_client.import_csv(file_id, data)

def list_permissions(self, file_id: str) -> List[Dict[str, Union[str, bool]]]:
"""Retrieve a list of permissions for a file.
Expand Down
42 changes: 42 additions & 0 deletions gspread/http_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from .exceptions import APIError, UnSupportedExportFormat
from .urls import (
DRIVE_FILES_API_V3_URL,
DRIVE_FILES_UPLOAD_API_V2_URL,
SPREADSHEET_BATCH_UPDATE_URL,
SPREADSHEET_SHEETS_COPY_TO_URL,
SPREADSHEET_URL,
Expand Down Expand Up @@ -468,6 +469,47 @@ def remove_permission(self, file_id: str, permission_id: str) -> None:
params: ParamsType = {"supportsAllDrives": True}
self.request("delete", url, params=params)

def import_csv(self, file_id: str, data: Union[str, bytes]) -> Any:
"""Imports data into the first page of the spreadsheet.
:param str data: A CSV string of data.
Example:
.. code::
# Read CSV file contents
content = open('file_to_import.csv', 'r').read()
gc.import_csv(spreadsheet.id, content)
.. note::
This method removes all other worksheets and then entirely
replaces the contents of the first worksheet.
"""
# Make sure we send utf-8
if isinstance(data, str):
data = data.encode("utf-8")

headers = {"Content-Type": "text/csv"}
url = "{}/{}".format(DRIVE_FILES_UPLOAD_API_V2_URL, file_id)

res = self.request(
"put",
url,
data=data,
params={
"uploadType": "media",
"convert": True,
"supportsAllDrives": True,
},
headers=headers,
)

return res.json()


class BackOffHTTPClient(HTTPClient):
"""BackoffClient is a gspread client with exponential
Expand Down
27 changes: 17 additions & 10 deletions gspread/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,6 @@ class MergeType(StrEnum):
merge_rows = "MERGE_ROWS"


SILENCE_WARNINGS_ENV_KEY = "GSPREAD_SILENCE_WARNINGS"


class ValueRenderOption(StrEnum):
formatted = "FORMATTED_VALUE"
unformatted = "UNFORMATTED_VALUE"
Expand Down Expand Up @@ -270,13 +267,15 @@ def numericise_all(
ignore = ignore or []

numericised_list = [
values[index]
if index + 1 in ignore
else numericise(
values[index],
empty2zero=empty2zero,
default_blank=default_blank,
allow_underscores_in_numeric_literals=allow_underscores_in_numeric_literals,
(
values[index]
if index + 1 in ignore
else numericise(
values[index],
empty2zero=empty2zero,
default_blank=default_blank,
allow_underscores_in_numeric_literals=allow_underscores_in_numeric_literals,
)
)
for index in range(len(values))
]
Expand Down Expand Up @@ -923,6 +922,14 @@ def to_records(


# SHOULD NOT BE NEEDED UNTIL NEXT MAJOR VERSION
# DEPRECATION_WARNING_TEMPLATE = (
# "[Deprecated][in version {v_deprecated}]: {msg_deprecated}"
# )


# SILENCE_WARNINGS_ENV_KEY = "GSPREAD_SILENCE_WARNINGS"


# def deprecation_warning(version: str, msg: str) -> None:
# """Emit a deprecation warning.

Expand Down

0 comments on commit f34cb6a

Please sign in to comment.