-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenlib.py
280 lines (214 loc) · 7.51 KB
/
genlib.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# -*- coding: utf-8 -*-"
"""Use information in rebase database to create rebase spreadsheet
Required python modules:
google-api-python-client google-auth-httplib2 google-auth-oauthlib
The Google Sheets API needs to be enabled to run this script.
Also, you'll need to generate access credentials and store those
in credentials.json.
Disable pyline noise
pylint: disable=no-absolute-import
"""
import os
import pickle
from googleapiclient import discovery # pylint: disable=import-error
from google_auth_oauthlib.flow import InstalledAppFlow # pylint: disable=import-error
from google.auth.transport.requests import Request # pylint: disable=import-error, disable=no-name-in-module
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
# Spreadsheet functions
def getsheet():
"""Get and return reference to spreadsheet"""
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
try: # py2 or token saved with py3
creds = pickle.load(token)
except UnicodeDecodeError: # py3, token saved with py2
creds = pickle.load(token, encoding='latin-1') # pylint: disable=unexpected-keyword-arg
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = discovery.build('sheets', 'v4', credentials=creds)
# service = discovery.build('sheets', 'v4', developerKey=API_KEY)
return service.spreadsheets() # pylint: disable=no-member
def init_spreadsheet(filename, title):
"""Initialize spreadsheet"""
sheet = getsheet()
try:
with open(filename, 'r') as f:
ssid = f.read().strip('\n')
request = sheet.get(
spreadsheetId=ssid, ranges=[], includeGridData=False)
response = request.execute()
sheets = response.get('sheets')
delete_sheets((sheet, ssid), sheets)
except IOError:
ssid = create_spreadsheet(sheet, title)
with open(filename, 'w') as f:
f.write(ssid)
return (sheet, ssid)
# Generic topic functions
def get_other_topic_id(c):
"""Calculate and return other_topic_id"""
other_topic_id = 0
c.execute('select topic, name from topics order by name')
for topic, name in c.fetchall():
if name == 'other':
return topic
if topic >= other_topic_id:
other_topic_id = topic + 1
return other_topic_id
def get_topic_name(c, topic):
"""Get topic name from topic id"""
c.execute("select name from topics where topic is '%s'" % topic)
topic = c.fetchone()
if topic:
return topic[0]
return None
# Spreadsheet manipulation functions
def doit(sheet, requests):
"""Execute a request"""
body = {'requests': requests}
request = sheet[0].batchUpdate(spreadsheetId=sheet[1], body=body)
response = request.execute()
return response
def hide_sheet(sheet, sheetId, hide):
"""Move 'Data' sheet to end of spreadsheet."""
request = []
request.append({
'updateSheetProperties': {
'properties': {
'sheetId': sheetId,
'hidden': hide,
},
'fields': 'hidden'
}
})
doit(sheet, request)
def create_spreadsheet(sheet, title):
"""Create a spreadsheet and return reference to it"""
spreadsheet = {'properties': {'title': title}}
request = sheet.create(body=spreadsheet, fields='spreadsheetId')
response = request.execute()
return response.get('spreadsheetId')
def delete_sheets(sheet, sheets):
"""Delete all sheets except sheet 0. In sheet 0, delete all values."""
# Unhide 'Data' sheet. If it is hidden we can't remove the other sheets.
hide_sheet(sheet, 0, False)
request = []
for s in sheets:
sheetId = s['properties']['sheetId']
if sheetId != 0:
request.append({'deleteSheet': {'sheetId': sheetId}})
else:
rows = s['properties']['gridProperties']['rowCount']
request.append({
'deleteRange': {
'range': {
'sheetId': sheetId,
'startRowIndex': 0,
'endRowIndex': rows,
},
'shiftDimension': 'ROWS',
}
})
# We are letting this fail if there was nothing to clean. This will
# hopefully result in re-creating the spreadsheet.
doit(sheet, request)
def resize_sheet(requests, sheetId, start, end):
"""Resize a sheet in provided range"""
requests.append({
'autoResizeDimensions': {
'dimensions': {
'sheetId': sheetId,
'dimension': 'COLUMNS',
'startIndex': start,
'endIndex': end
}
}
})
def add_sheet_header(requests, sheetId, fields):
"""Add provided header line to specified sheet.
Make it bold.
Args:
requests: Reference to list of requests to send to API.
sheetId: Sheet Id
fields: string with comma-separated list of fields
"""
# Generate header row
requests.append({
'pasteData': {
'data': fields,
'type': 'PASTE_NORMAL',
'delimiter': ',',
'coordinate': {
'sheetId': sheetId,
'rowIndex': 0
}
}
})
# Convert header row to bold and centered
requests.append({
'repeatCell': {
'range': {
'sheetId': sheetId,
'startRowIndex': 0,
'endRowIndex': 1
},
'cell': {
'userEnteredFormat': {
'horizontalAlignment': 'CENTER',
'textFormat': {
'bold': True
}
}
},
'fields': 'userEnteredFormat(textFormat,horizontalAlignment)'
}
})
def move_sheet(sheet, sheetId, to):
"""Move 'Data' sheet to end of spreadsheet."""
request = []
request.append({
'updateSheetProperties': {
'properties': {
'sheetId': sheetId,
'index': to,
},
'fields': 'index'
}
})
doit(sheet, request)
def sourceRange(sheetId, rows, column):
"""Return source range"""
return {
'sourceRange': {
'sources': [{
'sheetId': sheetId,
'startRowIndex': 0,
'endRowIndex': rows,
'startColumnIndex': column,
'endColumnIndex': column + 1
}]
}
}
def scope(name, sheetId, rows, column):
"""Return single source range"""
return {name: sourceRange(sheetId, rows, column)}
def sscope(name, sheetId, rows, start, end):
"""Return multiple source ranges"""
s = [scope(name, sheetId, rows, start)]
while start < end:
start += 1
s += [scope(name, sheetId, rows, start)]
return s