-
Notifications
You must be signed in to change notification settings - Fork 9
/
connection.py
382 lines (281 loc) · 12.6 KB
/
connection.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
# Copyright (C) 2016 Robert Scott
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
import logging
from math import pow
from werkzeug.utils import secure_filename
from threading import ThreadError
import os
CELL_REF_ERROR_STR = "Cell range is invalid."
class SpreadsheetConnection:
"""Handles connections to the spreadsheets opened by soffice (LibreOffice).
"""
def __init__(self, spreadsheet, lock, save_path):
self.spreadsheet = spreadsheet
self.lock = lock
self.save_path = save_path
def lock_spreadsheet(self):
"""Lock the spreadsheet.
The getting and setting cell functions rely on a given spreadsheet
being locked. This insures simulations requests to the same spreadsheet
do not interfere with one another.
"""
self.lock.acquire()
def unlock_spreadsheet(self):
""" Unlock the spreadsheet and return a 'success' boolean."""
try:
self.lock.release()
return True
except (RuntimeError, ThreadError):
return False
def __get_xy_index(self, cell_ref):
chars = [c for c in cell_ref if c.isalpha()]
nums = [n for n in cell_ref if n.isdigit()]
alpha_index = 0
for i, c in enumerate(chars):
# The base index for this character is in the range of 0 to 25
c_index = ord(c.upper()) - 65
if i == len(chars) - 1:
# The simple case of the least significant character.
# Eg. The 'J' in 'AMJ'.
alpha_index += c_index
else:
# The index for additional characters to the left are
# calculated c_index * 26^(n) where n is the characters
# position to the left.
# Need to increment c_index for correct multiplication
c_index += 1
alpha_index += c_index * pow(26, len(chars) - i - 1)
num_index = int("".join(nums)) - 1 # zero-based
alpha_index = int(alpha_index)
# Check max values
# Column can not be > AMJ == 1023
if alpha_index >= 1024:
raise ValueError(CELL_REF_ERROR_STR)
# Row can not be > 1048576
if num_index >= 1048576:
raise ValueError(CELL_REF_ERROR_STR)
return alpha_index, num_index
def __is_single_cell(self, cell_ref):
if len(cell_ref.split(":")) == 1:
return True
return False
def __check_single_cell(self, cell_ref):
if not self.__is_single_cell(cell_ref):
raise ValueError(
"Expected a single cell reference. A cell range was given."
)
def __cell_to_index(self, cell_ref):
"""Convert a spreadsheet style single cell or cell reference, to a zero
based numerical index.
'cell_ref' is what one would use in LibreOffice Calc. Eg. "ABC945".
Returned is: {"row_index": int, "column_index": int}.
"""
alpha_index, num_index = self.__get_xy_index(cell_ref)
return {"row_index": num_index, "column_index": alpha_index}
def __cell_range_to_index(self, cell_ref):
"""Convert a spreadsheet style range reference to zero-based numerical
indecies that describe the start and end points of the cell range.
'cell_ref' is what one would use in LibreOffice Calc. Eg. "A1" or
"A1:D6".
Returned is: {"row_start": int, "row_end": int,
"column_start": int, "column_end": int}
"""
left_ref, right_ref = cell_ref.split(":")
left_alpha_index, left_num_index = self.__get_xy_index(left_ref)
right_alpha_index, right_num_index = self.__get_xy_index(right_ref)
return {
"row_start": left_num_index,
"row_end": right_num_index,
"column_start": left_alpha_index,
"column_end": right_alpha_index,
}
def __check_for_lock(self):
if not self.lock.locked():
raise RuntimeError(
"Lock for this spreadsheet has not been aquired."
)
def __convert_to_float_if_numeric(self, value):
"""If value is a string representation of a number, convert it to a
float. Otherwise, simply return the string.
"""
try:
return float(value)
except (ValueError, TypeError):
return value
def __check_list(self, data):
if not isinstance(data, list):
raise ValueError("Expecting list type.")
def __check_1D_list(self, data):
self.__check_list(data)
if isinstance(data[0], list):
raise ValueError("Got 2D list when expecting 1D list.")
for x, cell in enumerate(data):
data[x] = self.__convert_to_float_if_numeric(cell)
return data
def set_cells(self, sheet, cell_ref, value):
"""Set the value(s) for a single cell or a cell range. This can be used
when it is not known if 'cell_ref' refers to a single cell or a range
See 'set_cell' and 'set_cell_range' for more information.
"""
self.__validate_sheet_name(sheet)
self.__validate_cell_ref(cell_ref)
if self.__is_single_cell(cell_ref):
self.set_cell(sheet, cell_ref, value)
else:
self.set_cell_range(sheet, cell_ref, value)
def set_cell(self, sheet, cell_ref, value):
"""Set the value of a single cell.
'sheet' is either a 0-based index or the string name of the sheet.
'cell_ref' is a LibreOffice style cell reference. eg. "A1".
'value' is a single string, int or float value.
"""
self.__check_single_cell(cell_ref)
self.__check_for_lock()
r = self.__cell_to_index(cell_ref)
sheet = self.spreadsheet.sheets[sheet]
if isinstance(value, list):
raise ValueError(
"Expectin a single cell. \
A list of cells was given."
)
value = self.__convert_to_float_if_numeric(value)
sheet[r["row_index"], r["column_index"]].value = value
def set_cell_range(self, sheet, cell_ref, data):
"""Set the values for a cell range.
'sheet' is either a 0-based index or the string name of the sheet.
'cell_ref' is a LibreOffice style cell reference. eg. "D7:G42".
For a one dimensional (only horizontal or only vertical) range of
cells, 'data' is a list. For a two dimensional range of cells, 'data'
is a list of lists. For example setting the 'cell_ref' "A1:C3"
requires 'data' of the format:
[[A1, B1, C1], [A2, B2, C2], [A3, B3, C3]].
"""
self.__check_for_lock()
r = self.__cell_range_to_index(cell_ref)
sheet = self.spreadsheet.sheets[sheet]
if r["row_start"] == r["row_end"]: # A row of cells
data = self.__check_1D_list(data)
sheet[
r["row_start"], r["column_start"] : r["column_end"] + 1
].values = data
elif r["column_start"] == r["column_end"]: # A column of cells
data = self.__check_1D_list(data)
sheet[
r["row_start"] : r["row_end"] + 1, r["column_start"]
].values = data
else: # A grid of cells
self.__check_list(data)
for x, row in enumerate(data):
if not isinstance(row, list):
raise ValueError("Expected a list of cells.")
for y, cell in enumerate(row):
data[x][y] = self.__convert_to_float_if_numeric(cell)
sheet[
r["row_start"] : r["row_end"] + 1,
r["column_start"] : r["column_end"] + 1,
].values = data
def get_sheet_names(self):
"""Returns a list of all sheet names in the workbook."""
return [s.name for s in self.spreadsheet.sheets]
def __validate_cell_ref(self, cell_ref):
""" A cell ref must be of the LibreOffice format
e.g. A1 or A1:ABC123."""
if type(cell_ref) is not str:
raise ValueError(CELL_REF_ERROR_STR)
if not cell_ref[0].isalpha():
raise ValueError(CELL_REF_ERROR_STR)
if not cell_ref[-1].isdigit():
raise ValueError(CELL_REF_ERROR_STR)
if ":" in cell_ref:
# Check the second alpha if it exists
if not cell_ref[cell_ref.index(":") + 1].isalpha():
raise ValueError(CELL_REF_ERROR_STR)
# Check the start of the range has a numeric component
if not cell_ref[cell_ref.index(":") - 1].isdigit():
raise ValueError(CELL_REF_ERROR_STR)
# Check for any unallowed characters
for ref in cell_ref:
if not ref.isdigit() and not ref.isalpha() and ref != ":":
raise ValueError(CELL_REF_ERROR_STR)
# TODO - Check range for sanity
# Reversed ranges should be allowed, they just need to be flipped
# e.g. "A5:A1" must become "A1:A5"
# Also need to convert "A1:A1" to "A1"
def __validate_sheet_name(self, sheet):
"""Don't want to send an invalid sheet to pyoo."""
ERROR_STR = "Sheet name is invalid."
sheet_names = self.get_sheet_names()
if type(sheet) is int:
if sheet < 0 or sheet > len(sheet_names) - 1:
raise ValueError(ERROR_STR)
elif type(sheet) is str:
if sheet not in sheet_names:
raise ValueError(ERROR_STR)
else:
raise ValueError(ERROR_STR)
def get_cells(self, sheet, cell_ref):
"""Gets the value(s) of a single cell or a cell range. This can be used
when it is not known if 'cell_ref' refers to a single cell or a range.
See 'get_cell' and 'get_cell_range' for more information.
"""
self.__validate_sheet_name(sheet)
self.__validate_cell_ref(cell_ref)
if self.__is_single_cell(cell_ref):
return self.get_cell(sheet, cell_ref)
else:
return self.get_cell_range(sheet, cell_ref)
def get_cell(self, sheet, cell_ref):
"""Returns the value of a single cell.
'sheet' is either a 0-based index or the string name of the sheet.
'cell_ref' is what one would use in LibreOffice Calc. Eg. "A3".
A single cell value is returned.
"""
self.__check_single_cell(cell_ref)
r = self.__cell_to_index(cell_ref)
sheet = self.spreadsheet.sheets[sheet]
return sheet[r["row_index"], r["column_index"]].value
def get_cell_range(self, sheet, cell_ref):
"""Returns the values of a range of cells.
'sheet' is either a 0-based index or the string name of the sheet.
'cell_ref' is what one would use in LibreOffice Calc. Eg. "A3:F75".
A list of cell values is returned for a one dimensional range of cells.
A list of lists is returned for a two dimensional range of cells.
"""
r = self.__cell_range_to_index(cell_ref)
sheet = self.spreadsheet.sheets[sheet]
logging.debug("Requested cell area: " + str(r))
# Cell ranges are requested as: [vertical area, horizontal area]
if r["row_start"] == r["row_end"]: # A row of cells was requested
return sheet[
r["row_start"], r["column_start"] : r["column_end"] + 1
].values
elif r["column_start"] == r["column_end"]: # A column of cells
return sheet[
r["row_start"] : r["row_end"] + 1, r["column_start"]
].values
else: # A grid of cells
return sheet[
r["row_start"] : r["row_end"] + 1,
r["column_start"] : r["column_end"] + 1,
].values
def save_spreadsheet(self, filename):
"""Save the spreadsheet in it's current state.
'filename' is the name of the file.
"""
if self.lock.locked():
filename = secure_filename(filename)
self.spreadsheet.save(os.path.join(self.save_path, filename))
return True
else:
return False