-
Notifications
You must be signed in to change notification settings - Fork 166
/
quip.py
819 lines (711 loc) · 31.9 KB
/
quip.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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
# Copyright 2014 Quip
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""A Quip API client library.
For full API documentation, visit https://quip.com/api/.
Typical usage:
client = quip.QuipClient(access_token=...)
user = client.get_authenticated_user()
starred = client.get_folder(user["starred_folder_id"])
print "There are", len(starred["children"]), "items in your starred folder"
In addition to standard getters and setters, we provide a few convenience
methods for document editing. For example, you can use `add_to_first_list`
to append items (in Markdown) to the first bulleted or checklist in a
given document, which is useful for automating a task list.
"""
import datetime
import json
import logging
import ssl
import sys
import time
import xml.etree.cElementTree
PY3 = sys.version_info > (3,)
if PY3:
import urllib.request
import urllib.parse
import urllib.error
Request = urllib.request.Request
urlencode = urllib.parse.urlencode
urlopen = urllib.request.urlopen
HTTPError = urllib.error.HTTPError
iteritems = dict.items
else:
import urllib
import urllib2
Request = urllib2.Request
urlencode = urllib.urlencode
urlopen = urllib2.urlopen
HTTPError = urllib2.HTTPError
iteritems = dict.iteritems
try:
reload(sys)
sys.setdefaultencoding('utf8')
except:
# Can't change default encoding usually...
pass
try:
ssl.PROTOCOL_TLSv1_1
except AttributeError:
raise Exception(
"Using the Quip API requires an SSL library that supports TLS versions "
">= 1.1; your Python + OpenSSL installation must be upgraded.")
# After 2017-02, the Quip API HTTPS endpoint requires TLS version 1.1 or later;
# TLS version 1.0 is disabled due to extensive security vulnerabilities.
#
# - macOS: At this time of this writing, macOS ships with Python 2.7 and
# OpenSSL, but the version of OpenSSL is outdated and only supports TLS 1.0.
# (This is even true of the most recent version of macOS (Sierra) with all
# security patches installed; see
# https://eclecticlight.co/2016/03/23/the-tls-mess-in-os-x-el-capitan/ .)
#
# To use this module on a macOS system, you will need to install your own
# copy of Python and OpenSSL. Simple suggestions:
#
# 1) Install Homebrew from http://brew.sh; run "brew install openssl python"
# 2) Install Miniconda from https://conda.io/miniconda.html
#
# - Google App Engine (GAE): Per App Engine's documentation, you must request
# version 2.7.11 of the "ssl" library in your app.yaml file. See:
# https://cloud.google.com/appengine/docs/python/sockets/ssl_support
class QuipError(Exception):
def __init__(self, code, message, http_error):
Exception.__init__(self, "%d: %s" % (code, message))
self.code = code
self.http_error = http_error
class QuipClient(object):
"""A Quip API client"""
# Edit operations
APPEND, \
PREPEND, \
AFTER_SECTION, \
BEFORE_SECTION, \
REPLACE_SECTION, \
DELETE_SECTION = range(6)
# Folder colors
MANILA, \
RED, \
ORANGE, \
GREEN, \
BLUE = range(5)
def __init__(self, access_token=None, client_id=None, client_secret=None,
base_url=None, request_timeout=None):
"""Constructs a Quip API client.
If `access_token` is given, all of the API methods in the client
will work to read and modify Quip documents.
Otherwise, only `get_authorization_url` and `get_access_token`
work, and we assume the client is for a server using the Quip API's
OAuth endpoint.
"""
self.access_token = access_token
self.client_id = client_id
self.client_secret = client_secret
self.base_url = base_url if base_url else "https://platform.quip.com"
self.request_timeout = request_timeout if request_timeout else 10
def get_authorization_url(self, redirect_uri, state=None):
"""Returns the URL the user should be redirected to to sign in."""
return self._url(
"oauth/login", redirect_uri=redirect_uri, state=state,
response_type="code", client_id=self.client_id)
def get_access_token(self, redirect_uri, code,
grant_type="authorization_code",
refresh_token=None):
"""Exchanges a verification code for an access_token.
Once the user is redirected back to your server from the URL
returned by `get_authorization_url`, you can exchange the `code`
argument with this method.
"""
return self._fetch_json(
"oauth/access_token", redirect_uri=redirect_uri, code=code,
grant_type=grant_type, refresh_token=refresh_token,
client_id=self.client_id, client_secret=self.client_secret)
def get_authenticated_user(self):
"""Returns the user corresponding to our access token."""
return self._fetch_json("users/current")
def get_user(self, id):
"""Returns the user with the given ID."""
return self._fetch_json("users/" + id)
def get_users(self, ids):
"""Returns a dictionary of users for the given IDs."""
return self._fetch_json("users/", post_data={"ids": ",".join(ids)})
def update_user(self, user_id, picture_url=None):
return self._fetch_json("users/update", post_data={
"user_id": user_id,
"picture_url": picture_url,
})
def get_contacts(self):
"""Returns a list of the users in the authenticated user's contacts."""
return self._fetch_json("users/contacts")
def get_folder(self, id):
"""Returns the folder with the given ID."""
return self._fetch_json("folders/" + id)
def get_folders(self, ids):
"""Returns a dictionary of folders for the given IDs."""
return self._fetch_json("folders/", post_data={"ids": ",".join(ids)})
def new_folder(self, title, parent_id=None, color=None, member_ids=[]):
return self._fetch_json("folders/new", post_data={
"title": title,
"parent_id": parent_id,
"color": color,
"member_ids": ",".join(member_ids),
})
def update_folder(self, folder_id, color=None, title=None):
return self._fetch_json("folders/update", post_data={
"folder_id": folder_id,
"color": color,
"title": title,
})
def add_folder_members(self, folder_id, member_ids):
"""Adds the given users to the given folder."""
return self._fetch_json("folders/add-members", post_data={
"folder_id": folder_id,
"member_ids": ",".join(member_ids),
})
def remove_folder_members(self, folder_id, member_ids):
"""Removes the given users from the given folder."""
return self._fetch_json("folders/remove-members", post_data={
"folder_id": folder_id,
"member_ids": ",".join(member_ids),
})
def get_teams(self):
"""Returns the teams for the user corresponding to our access token."""
return self._fetch_json("teams/current")
def get_messages(self, thread_id, max_created_usec=None, count=None):
"""Returns the most recent messages for the given thread.
To page through the messages, use max_created_usec, which is the
sort order for the returned messages.
count should be an integer indicating the number of messages you
want returned. The maximum is 100.
"""
return self._fetch_json(
"messages/" + thread_id, max_created_usec=max_created_usec,
count=count)
def new_message(self, thread_id, content=None, **kwargs):
"""Sends a message on the given thread.
`content` is plain text, not HTML.
"""
args = {
"thread_id": thread_id,
"content": content,
}
args.update(kwargs)
return self._fetch_json("messages/new", post_data=args)
def get_thread(self, id):
"""Returns the thread with the given ID."""
return self._fetch_json("threads/" + id)
def get_threads(self, ids):
"""Returns a dictionary of threads for the given IDs."""
return self._fetch_json("threads/", post_data={"ids": ",".join(ids)})
def get_recent_threads(self, max_updated_usec=None, count=None, **kwargs):
"""Returns the recently updated threads for a given user."""
return self._fetch_json(
"threads/recent", max_updated_usec=max_updated_usec,
count=count, **kwargs)
def get_matching_threads(
self, query, count=None, only_match_titles=False, **kwargs):
"""Returns the recently updated threads for a given user."""
return self._fetch_json("threads/search", query=query, count=count,
only_match_titles=only_match_titles, **kwargs)
def add_thread_members(self, thread_id, member_ids):
"""Adds the given folder or user IDs to the given thread."""
return self._fetch_json("threads/add-members", post_data={
"thread_id": thread_id,
"member_ids": ",".join(member_ids),
})
def delete_thread(self, thread_id):
"""Deletes the thread with the given thread id or secret"""
return self._fetch_json("threads/delete", post_data={
"thread_id": thread_id,
})
def remove_thread_members(self, thread_id, member_ids):
"""Removes the given folder or user IDs from the given thread."""
return self._fetch_json("threads/remove-members", post_data={
"thread_id": thread_id,
"member_ids": ",".join(member_ids),
})
def move_thread(self, thread_id, source_folder_id, destination_folder_id):
"""Moves the given thread from the source folder to the destination one.
"""
self.add_thread_members(thread_id, [destination_folder_id])
self.remove_thread_members(thread_id, [source_folder_id])
def new_chat(self, message, title=None, member_ids=[]):
"""Creates a chat with the given title and members, and send the
initial message."""
return self._fetch_json("threads/new-chat", post_data={
"message": message,
"title": title,
"member_ids": ",".join(member_ids),
})
def new_document(self, content, format="html", title=None, member_ids=[]):
"""Creates a new document from the given content.
To create a document in a folder, include the folder ID in the list
of member_ids, e.g.,
client = quip.QuipClient(...)
user = client.get_authenticated_user()
client.new_document(..., member_ids=[user["archive_folder_id"]])
"""
return self._fetch_json("threads/new-document", post_data={
"content": content,
"format": format,
"title": title,
"member_ids": ",".join(member_ids),
})
def copy_document(self, thread_id, folder_ids=None, member_ids=None,
title=None, values=None, **kwargs):
"""Copies the given document, optionally replaces template variables
in the document with values in 'values' arg. The values argument
must be a dictionary that contains string keys and values that
are either strings, numbers or dictionaries.
"""
args = {"thread_id": thread_id}
if folder_ids:
args["folder_ids"] = ",".join(folder_ids)
if member_ids:
args["member_ids"] = ",".join(member_ids)
if title:
args["title"] = title
if values:
args["values"] = json.dumps(values)
args.update(kwargs)
return self._fetch_json("threads/copy-document", post_data=args)
def merge_comments(self, original_id, children_ids, ignore_user_ids=[]):
"""Given an original document and a set of exact duplicates, copies
all comments and messages on the duplicates to the original.
Impersonates the commentors if the access token used has
permission, but does not add them to the thread.
"""
import re
threads = self.get_threads(children_ids + [original_id])
original_section_ids = re.findall(r" id='([a-zA-Z0-9]{11})'",
threads[original_id]["html"])
for thread_id in children_ids:
thread = threads[thread_id]
child_section_ids = re.findall(r" id='([a-zA-Z0-9]{11})'",
thread["html"])
parent_map = dict(zip(child_section_ids, original_section_ids))
messages = self.get_messages(thread_id)
for message in reversed(messages):
if message["author_id"] in ignore_user_ids:
continue
kwargs = {
"user_id": message["author_id"],
"frame": "bubble",
"service_id": message["id"],
}
if "parts" in message:
kwargs["parts"] = json.dumps(message["parts"])
else:
kwargs["content"] = message["text"]
if "annotation" in message:
section_id = None
if "highlight_section_ids" in message["annotation"]:
section_id = message["annotation"][
"highlight_section_ids"][0]
else:
anno_loc = thread["html"].find(
'<annotation id="%s"' % message["annotation"]["id"])
loc = thread["html"].rfind("id=", 0, anno_loc)
if anno_loc >= 0 and loc >= 0:
section_id = thread["html"][loc + 4:loc + 15]
if section_id and section_id in parent_map:
kwargs["section_id"] = parent_map[section_id]
if "files" in message:
attachments = []
for blob_info in message["files"]:
blob = self.get_blob(thread_id, blob_info["hash"])
new_blob = self.put_blob(
original_id, blob, name=blob_info["name"])
attachments.append(new_blob["id"])
if attachments:
kwargs["attachments"] = ",".join(attachments)
self.new_message(original_id, **kwargs)
def edit_document(self, thread_id, content, operation=APPEND, format="html",
section_id=None, **kwargs):
"""Edits the given document, adding the given content.
`operation` should be one of the constants described above. If
`operation` is relative to another section of the document, you must
also specify the `section_id`.
"""
# Since our cell ids in 10x contain ';', which is a valid cgi
# parameter separator, we are replacing them with '_' in 10x cell
# sections. This should be no op for all other sections.
section_id = None if not section_id else section_id.replace(";", "_")
args = {
"thread_id": thread_id,
"content": content,
"location": operation,
"format": format,
"section_id": section_id
}
args.update(kwargs)
return self._fetch_json("threads/edit-document", post_data=args)
def add_to_first_list(self, thread_id, *items, **kwargs):
"""Adds the given items to the first list in the given document.
client = quip.QuipClient(...)
client.add_to_first_list(thread_id, "Try the Quip API")
"""
items = [item.replace("\n", " ") for item in items]
args = {
"thread_id": thread_id,
"content": "\n\n".join(items),
"format": "markdown",
"operation": self.AFTER_SECTION
}
args.update(kwargs)
if "section_id" not in args:
first_list = self.get_first_list(
thread_id, kwargs.pop("document_html", None))
if first_list:
args["section_id"] = self.get_last_list_item_id(first_list)
if not args.get("section_id"):
args["operation"] = self.APPEND
args["content"] = "\n\n".join([" * %s" % i for i in items])
return self.edit_document(**args)
def add_to_spreadsheet(self, thread_id, *rows, **kwargs):
"""Adds the given rows to the named (or first) spreadsheet in the
given document.
client = quip.QuipClient(...)
client.add_to_spreadsheet(thread_id, ["5/1/2014", 2.24])
"""
content = "".join(["<tr>%s</tr>" % "".join(
["<td>%s</td>" % cell for cell in row]) for row in rows])
if kwargs.get("name"):
spreadsheet = self.get_named_spreadsheet(kwargs["name"], thread_id)
else:
spreadsheet = self.get_first_spreadsheet(thread_id)
if kwargs.get("add_to_top"):
section_id = self.get_first_row_item_id(spreadsheet)
operation = self.BEFORE_SECTION
else:
section_id = self.get_last_row_item_id(spreadsheet)
operation = self.AFTER_SECTION
return self.edit_document(
thread_id=thread_id,
content=content,
section_id=section_id,
operation=operation)
def update_spreadsheet_row(self, thread_id, header, value, updates, **args):
"""Finds the row where the given header column is the given value, and
applies the given updates. Updates is a dict from header to
new value. In both cases headers can either be a string that matches, or
"A", "B", "C", 1, 2, 3 etc. If no row is found, adds a new one.
client = quip.QuipClient(...)
client.update_spreadsheet_row(
thread_id, "customer", "Acme", {"Billed": "6/24/2015"})
"""
response = None
if args.get("name"):
spreadsheet = self.get_named_spreadsheet(args["name"], thread_id)
else:
spreadsheet = self.get_first_spreadsheet(thread_id)
headers = self.get_spreadsheet_header_items(spreadsheet)
row = self.find_row_from_header(spreadsheet, header, value)
if row:
ids = self.get_row_ids(row)
for head, val in iteritems(updates):
index = self.get_index_of_header(headers, head)
if not index or index >= len(ids) or not ids[index]:
continue
response = self.edit_document(
thread_id=thread_id,
content=val,
format="markdown",
section_id=ids[index],
operation=self.REPLACE_SECTION,
**args)
else:
updates[header] = value
response = self.add_spreadsheet_row(
thread_id, spreadsheet, updates, headers=headers, **args)
return response
def add_spreadsheet_row(
self, thread_id, spreadsheet, updates, headers=None, **args):
if not headers:
headers = self.get_spreadsheet_header_items(spreadsheet)
indexed_items = {}
extra_items = []
for head, val in iteritems(updates):
index = self.get_index_of_header(
headers, head, default=None)
if index is None or index in indexed_items:
extra_items.append(val)
else:
indexed_items[index] = val
cells = []
if indexed_items:
for i in range(max(indexed_items.keys()) + 1):
if i in indexed_items:
cells.append(indexed_items[i])
elif len(extra_items):
cells.append(extra_items.pop(0))
else:
cells.append("")
cells.extend(extra_items)
content = "<tr>%s</tr>" % "".join(
["<td>%s</td>" % cell for cell in cells])
section_id = self.get_last_row_item_id(spreadsheet)
response = self.edit_document(
thread_id=thread_id,
content=content,
section_id=section_id,
operation=self.AFTER_SECTION,
**args)
return response
def toggle_checkmark(self, thread_id, item, checked=True):
"""Sets the checked state of the given list item to the given state.
client = quip.QuipClient(...)
list = client.get_first_list(thread_id)
client.toggle_checkmark(thread_id, list[0])
"""
if checked:
item.attrib["class"] = "checked"
else:
item.attrib["class"] = ""
return self.edit_document(thread_id=thread_id,
content=xml.etree.cElementTree.tostring(item),
section_id=item.attrib["id"],
operation=self.REPLACE_SECTION)
def get_first_list(self, thread_id=None, document_html=None):
"""Returns the `ElementTree` of the first list in the document.
The list can be any type (bulleted, numbered, or checklist).
If `thread_id` is given, we download the document. If you have
already downloaded the document, you can specify `document_html`
directly.
"""
return self._get_container(thread_id, document_html, "ul", 0)
def get_last_list(self, thread_id=None, document_html=None):
"""Like `get_first_list`, but the last list in the document."""
return self._get_container(thread_id, document_html, "ul", -1)
def get_section(self, section_id, thread_id=None, document_html=None):
if not document_html:
document_html = self.get_thread(thread_id).get("html")
if not document_html:
return None
tree = self.parse_document_html(document_html)
element = list(tree.iterfind(".//*[@id='%s']" % section_id))
if not element:
return None
return element[0]
def get_named_spreadsheet(self, name, thread_id=None, document_html=None):
if not document_html:
document_html = self.get_thread(thread_id).get("html")
if not document_html:
return None
tree = self.parse_document_html(document_html)
element = list(tree.iterfind(".//*[@title='%s']" % name))
if not element:
return None
return element[0]
def _get_container(self, thread_id, document_html, container, index):
if not document_html:
document_html = self.get_thread(thread_id).get("html")
if not document_html:
return None
tree = self.parse_document_html(document_html)
lists = list(tree.iter(container))
if not lists:
return None
try:
return lists[index]
except IndexError:
return None
def get_last_list_item_id(self, list_tree):
"""Returns the last item in the given list `ElementTree`."""
items = list(list_tree.iter("li"))
return items[-1].attrib["id"] if items else None
def get_first_list_item_id(self, list_tree):
"""Like `get_last_list_item_id`, but the first item in the list."""
for item in list_tree.iter("li"):
return item.attrib["id"]
return None
def get_first_spreadsheet(self, thread_id=None, document_html=None):
"""Returns the `ElementTree` of the first spreadsheet in the document.
If `thread_id` is given, we download the document. If you have
already downloaded the document, you can specify `document_html`
directly.
"""
return self._get_container(thread_id, document_html, "table", 0)
def get_last_spreadsheet(self, thread_id=None, document_html=None):
"""Like `get_first_spreadsheet`, but the last spreadsheet."""
return self._get_container(thread_id, document_html, "table", -1)
def get_last_row_item_id(self, spreadsheet_tree):
"""Returns the last row in the given spreadsheet `ElementTree`."""
items = list(spreadsheet_tree.iter("tr"))
return items[-1].attrib["id"] if items else None
def get_first_row_item_id(self, spreadsheet_tree):
"""Returns the last row in the given spreadsheet `ElementTree`."""
items = list(spreadsheet_tree.iter("tr"))
return items[1].attrib["id"] if items else None
def get_row_items(self, row_tree):
"""Returns the text of items in the given row `ElementTree`."""
return [(list(x.itertext()) or [None])[0] for x in row_tree]
def get_row_ids(self, row_tree):
"""Returns the ids of items in the given row `ElementTree`."""
return [x.attrib["id"] for x in row_tree]
def get_spreadsheet_header_items(self, spreadsheet_tree):
"""Returns the header row in the given spreadsheet `ElementTree`."""
return self.get_row_items(list(spreadsheet_tree.iterfind(".//tr"))[0])
def get_index_of_header(self, header_items, header, default=0):
"""Find the index of the given header in the items"""
if header:
header = str(header)
lower_headers = [str(h).lower() for h in header_items]
if header in header_items:
return header_items.index(header)
elif header.lower() in lower_headers:
return lower_headers.index(header.lower())
elif header.isdigit():
return int(header)
elif len(header) == 1:
char = ord(header.upper())
if ord('A') < char < ord('Z'):
return char - ord('A') + 1
else:
pass
return default
def find_row_from_header(self, spreadsheet_tree, header, value):
"""Find the row in the given spreadsheet `ElementTree` where header is
value.
"""
headers = self.get_spreadsheet_header_items(spreadsheet_tree)
index = self.get_index_of_header(headers, header)
for row in spreadsheet_tree.iterfind(".//tr"):
if len(row) <= index:
continue
cell = row[index]
if cell.tag != "td":
continue
if list(cell.itertext())[0].lower() == value.lower():
return row
def parse_spreadsheet_contents(self, spreadsheet_tree):
"""Returns a python-friendly representation of the given spreadsheet
`ElementTree`
"""
import collections
spreadsheet = {
"id": spreadsheet_tree.attrib.get("id"),
"headers": self.get_spreadsheet_header_items(spreadsheet_tree),
"rows": [],
}
for row in spreadsheet_tree.iterfind(".//tr"):
value = {
"id": row.attrib.get("id"),
"cells": collections.OrderedDict(),
}
for i, cell in enumerate(row):
if cell.tag != "td":
continue
data = {
"id": cell.attrib.get("id"),
}
images = list(cell.iter("img"))
if images:
data["content"] = images[0].attrib.get("src")
else:
data["content"] = list(cell.itertext())[0].replace(
u"\u200b", "")
style = cell.attrib.get("style")
if style and "background-color:#" in style:
sharp = style.find("#")
data["color"] = style[sharp + 1:sharp + 7]
value["cells"][spreadsheet["headers"][i]] = data
if len(value["cells"]):
spreadsheet["rows"].append(value)
return spreadsheet
def parse_document_html(self, document_html):
"""Returns an `ElementTree` for the given Quip document HTML"""
document_xml = "<html>" + document_html + "</html>"
return xml.etree.cElementTree.fromstring(document_xml.encode("utf-8"))
def parse_micros(self, usec):
"""Returns a `datetime` for the given microsecond string"""
return datetime.datetime.utcfromtimestamp(usec / 1000000.0)
def get_blob(self, thread_id, blob_id):
"""Returns a file-like object with the contents of the given blob from
the given thread.
The object is described in detail here:
https://docs.python.org/2/library/urllib2.html#urllib2.urlopen
"""
request = Request(
url=self._url("blob/%s/%s" % (thread_id, blob_id)))
if self.access_token:
request.add_header("Authorization", "Bearer " + self.access_token)
try:
return urlopen(request, timeout=self.request_timeout)
except HTTPError as error:
try:
# Extract the developer-friendly error message from the response
message = json.loads(error.read().decode())["error_description"]
except Exception:
raise error
raise QuipError(error.code, message, error)
def put_blob(self, thread_id, blob, name=None):
"""Uploads an image or other blob to the given Quip thread. Returns an
ID that can be used to add the image to the document of the thread.
blob can be any file-like object. Requires the 'requests' module.
"""
import requests
url = "blob/" + thread_id
headers = None
if self.access_token:
headers = {"Authorization": "Bearer " + self.access_token}
if name:
blob = (name, blob)
try:
response = requests.request(
"post", self._url(url), timeout=self.request_timeout,
files={"blob": blob}, headers=headers)
response.raise_for_status()
return response.json()
except requests.RequestException as error:
try:
# Extract the developer-friendly error message from the response
message = error.response.json()["error_description"]
except Exception:
raise error
raise QuipError(error.response.status_code, message, error)
def new_websocket(self, **kwargs):
"""Gets a websocket URL to connect to.
"""
return self._fetch_json("websockets/new", **kwargs)
def _fetch_json(self, path, post_data=None, **args):
request = Request(url=self._url(path, **args))
if post_data:
post_data = dict((k, v) for k, v in post_data.items()
if v or isinstance(v, int))
request_data = urlencode(self._clean(**post_data))
if PY3:
request.data = request_data.encode()
else:
request.data = request_data
if self.access_token:
request.add_header("Authorization", "Bearer " + self.access_token)
try:
return json.loads(
urlopen(
request, timeout=self.request_timeout).read().decode())
except HTTPError as error:
try:
# Extract the developer-friendly error message from the response
message = json.loads(error.read().decode())["error_description"]
except Exception:
raise error
raise QuipError(error.code, message, error)
def _clean(self, **args):
return dict((k, str(v) if isinstance(v, int) else v.encode("utf-8"))
for k, v in args.items() if v or isinstance(v, int))
def _url(self, path, **args):
url = self.base_url + "/1/" + path
args = self._clean(**args)
if args:
url += "?" + urlencode(args)
return url