-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from jabardigitalservice/feature/config_conver…
…ter_spreadsheet Feature/config converter spreadsheet
- Loading branch information
Showing
13 changed files
with
233 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright (c) Free Software Foundation, Inc. All rights reserved. | ||
# Licensed under the AGPL-3.0-only License. See LICENSE in the project root | ||
# for license information. | ||
|
||
"""Google Spreadsheet library.""" | ||
|
||
from __future__ import annotations | ||
from dataclasses import dataclass | ||
import warnings | ||
|
||
from google.oauth2.service_account import Credentials | ||
import gspread | ||
from pandas import DataFrame | ||
|
||
from . import DataSource | ||
|
||
|
||
@dataclass(repr=False) | ||
class GSheet(DataSource): | ||
""" | ||
Represents a data source that connects to an Google Spreadsheet. | ||
Args: | ||
client_secret_file (str): path location credential google spreadsheet. | ||
""" | ||
|
||
client_secret_file: str | ||
|
||
@property | ||
def connection(self) -> Credentials: | ||
""" | ||
Returns a credential for the Google Spreadsheet. | ||
Returns: | ||
Credentials: Creds from googleservice account. | ||
""" | ||
return Credentials.from_service_account_file( | ||
self.client_secret_file, | ||
scopes=['https://www.googleapis.com/auth/spreadsheets'] | ||
) | ||
|
||
def __call__( | ||
self, gsheet_id: str, sheet_name: str, | ||
) -> DataFrame: | ||
""" | ||
__call__ method. | ||
Converts the data from google spreadsheet into a | ||
Pandas DataFrame. | ||
Args: | ||
gsheet_id (str): The id from url spreadsheet. | ||
sheet_name (str): The name a sheet will get data. | ||
Returns: | ||
DataFrame: A Pandas DataFrame. | ||
""" | ||
with warnings.catch_warnings(record=True): | ||
warnings.simplefilter('always') | ||
data: gspread.Worksheet = gspread.authorize( | ||
self.connection | ||
).open_by_key(gsheet_id).worksheet(sheet_name) | ||
|
||
# default index 0 jadi kolom | ||
return DataFrame(data.get_all_records()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"type": "service_account", | ||
"project_id": "project_id", | ||
"private_key_id": "private_key_id", | ||
"private_key": "-----BEGIN PRIVATE KEY----------END PRIVATE KEY-----\n", | ||
"client_email": "name@email.com", | ||
"client_id": "client_id", | ||
"auth_uri": "https://accounts.google.com/o/oauth2/auth", | ||
"token_uri": "https://oauth2.googleapis.com/token", | ||
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs", | ||
"client_x509_cert_url": "client_x509_cert_url" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright (c) Free Software Foundation, Inc. All rights reserved. | ||
# Licensed under the AGPL-3.0-only License. See LICENSE in the project root | ||
# for license information. | ||
|
||
"""test_gsheet.""" | ||
|
||
import csv | ||
from os import path | ||
from unittest.mock import patch | ||
|
||
from . import CONFIG_JSON, CONFIG_YAML, DataFrameTestCase, PATH | ||
from datasae.converter import DataSourceType | ||
|
||
|
||
class MockCreds: | ||
"""MockCreds.""" | ||
|
||
def __init__(self, filename, **kwargs): | ||
"""__init__.""" | ||
pass | ||
|
||
@property | ||
def project_id(self): | ||
"""project_id.""" | ||
pass | ||
|
||
|
||
class GSheetTest(DataFrameTestCase): | ||
"""GSheetTest.""" | ||
|
||
def __init__(self, methodName: str = 'runTest'): | ||
"""__init__.""" | ||
super().__init__(methodName) | ||
self.NAME: str = 'test_gsheet' | ||
self.gsheet = CONFIG_JSON(self.NAME) | ||
|
||
def test_config(self): | ||
"""test_config.""" | ||
for config in (CONFIG_JSON, CONFIG_YAML): | ||
gsheet = config(self.NAME) | ||
self.assertIs(gsheet.type, DataSourceType.GSHEET) | ||
self.assertEqual( | ||
gsheet.client_secret_file, path.join(PATH, 'creds.json') | ||
) | ||
|
||
@patch( | ||
'google.oauth2.service_account.Credentials.from_service_account_file', | ||
side_effect=MockCreds | ||
) | ||
def test_connection(self, _): | ||
"""test_connection.""" | ||
self.assertTrue(hasattr(self.gsheet.connection, 'project_id')) | ||
|
||
@patch('gspread.authorize') | ||
@patch( | ||
'google.oauth2.service_account.Credentials.from_service_account_file', | ||
side_effect=MockCreds | ||
) | ||
def test_convert(self, _, mock_gspread): | ||
"""test_convert.""" | ||
mock_open_by_key = mock_gspread.return_value.open_by_key.return_value | ||
mock_worksheet = mock_open_by_key.worksheet.return_value | ||
|
||
with open(path.join(PATH, 'data.csv')) as file: | ||
mock_worksheet.get_all_records.return_value = [ | ||
{ | ||
key: value | ||
for key, value in row.items() | ||
if key | ||
} | ||
for row in csv.DictReader(file) | ||
] | ||
|
||
self.assertEqual(self.DATA, self.gsheet('gsheet_id', 'sheet_name')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters