-
Notifications
You must be signed in to change notification settings - Fork 0
/
google_sheets.py
103 lines (88 loc) · 2.93 KB
/
google_sheets.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import os
import gspread
class SheetsApiClient:
"""interface for all functionality with google sheets
enables connection, append, and notification
"""
SPREADSHEET_NAME = "PDB-DEV_ChatGPT"
SCHEMA = [
"DOI",
"Title",
"date of publishing",
"date of analysis",
"authors",
"classification",
"methods used",
"software",
]
def __init__(self):
self.client = self.connect()
self.spreadsheet = self.client.open(type(self).SPREADSHEET_NAME)
self.worksheet = self.spreadsheet.get_worksheet(0)
@staticmethod
def connect():
"""connects to Google Sheets API service using private key file
"""
try:
secret_file = os.path.join(os.getcwd(), "google_sheets_credentials.json")
return gspread.service_account(secret_file)
except OSError as e:
print(e)
def append_row(self, row: [str]):
"""
Adds a row to the spreadsheet, must follow SCHEMA:
"""
self._check_row(row)
self.worksheet.append_row(row)
def append_rows(self, rows: [[str]]):
"""
Adds a list of rows to the spreadsheet, each row must follow SCHEMA:
WARNING: Assumes that the [rows] list will never exceed the maximum throughput of one api call
"""
for row in rows:
self._check_row(row)
self.worksheet.append_rows(rows)
def email(self, message: str, email_addresses: [str]):
"""Shares the spreadsheet with arthur, along with the message in an email
Args:
message (str): message to be sent
email_addresses ([str]): recipients of notification
"""
for email_address in email_addresses:
self.spreadsheet.share(
email_address,
perm_type="user",
role="reader",
notify=True,
email_message=message,
)
@staticmethod
def _check_row(row: []):
"""Checks row
Args:
row ([]): row of values to be added to worksheet
Raises:
ValueError: number of values in rows doesn't match schema
"""
if len(row) != len(SheetsApiClient.SCHEMA):
raise ValueError(
f"Row must have {len(SheetsApiClient.SCHEMA)} fields in the order specified\n{SheetsApiClient.SCHEMA}"
)
def main():
# some test code which initializes the client, then appends rows to the worksheet, then pings arthur
spread = SheetsApiClient()
dummy_row = [
"DOI",
"Title",
"date of publishing",
"date of analysis",
"authors",
"classification",
"methods used",
"software",
]
spread.append_row(dummy_row)
spread.append_rows([dummy_row, dummy_row, dummy_row])
# spread.notify_arthur("testing out the code")
if __name__ == "__main__":
main()