-
Notifications
You must be signed in to change notification settings - Fork 0
/
couch_adapter.py
1247 lines (1054 loc) · 46.2 KB
/
couch_adapter.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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# coding: utf-8
from common import *
from json import loads, dumps
from uuid import uuid4
from time import sleep
from traceback import format_exc
from httplib import IncompleteRead, CannotSendRequest
import errno
from time import time
from copy import deepcopy
from base64 import b64encode as base64_b64encode
try :
from couchdb.http import Unauthorized, ResourceNotFound as couch_ResourceNotFound, ResourceConflict as couch_ResourceConflict, ServerError as couch_ServerError
#from cookieclient import CookieServer as Server
from couchdb import Server
import couchdb.http
retriable_errors = (Unauthorized, IncompleteRead, CannotSendRequest)
bad_errnos = [errno.EPIPE, errno.ECONNRESET, None]
server_errors = [403, 500, 502]
except ImportError, e :
mdebug("couchdb not available. Probably on mobile.")
jnius_detachable = False
try :
from jnius import autoclass
String = autoclass('java.lang.String')
#from jnius import detach as jnius_detach
#jnius_detachable = True
except ImportError, e :
try :
from pyobjus import autoclass, objc_f, objc_str as String, objc_l as Long, objc_i as Integer
except ImportError, e :
mverbose("pyjnius and pyobjus not available. Probably on a server.")
def credentials(params) :
return params["couch_proto"] + "://" + params["couch_server"] + ":" + str(params["couch_port"] + (params["couch_path"] if ("couch_path" in params and params["couch_path"] != "") else ""))
class ResourceNotFound(Exception) :
def __init__(self, msg, e = False):
Exception.__init__(self)
self.msg = msg
self.e = e
def __str__(self) :
return self.msg
# Couchdb bug returning NotFound instead of Unathorized during a timeout
class PossibleResourceNotFound(Exception) :
def __init__(self, msg, e = False):
Exception.__init__(self)
self.msg = msg
self.e = e
def __str__(self) :
return self.msg
# Total failure to communicate with the database
class CommunicationError(Exception) :
def __init__(self, msg, e = False):
Exception.__init__(self)
self.msg = msg
self.e = e
def __str__(self) :
return self.msg
class ResourceConflict(Exception) :
def __init__(self, msg, e = False):
Exception.__init__(self)
self.msg = msg
self.e = e
def __str__(self) :
return self.msg
class NotImplementedError(Exception) :
def __init__(self, msg, e = False):
Exception.__init__(self)
self.msg = msg
self.e = e
def __str__(self) :
return self.msg
class repeatable(object):
def __init__(self, retries = 3):
self.retries = retries
def __call__(self, f):
def wrapped_f(*args):
mverbose("Repeating function " + f.__name__ + " " + str(self.retries) + " times.")
tries = self.retries
while True :
try :
return f(*args)
except ResourceConflict, e :
for line in format_exc().splitlines() :
mwarn(line)
tries = tries - 1
if tries == 0 :
merr("Ran out of tries =(")
raise CommunicationError("Repeat fail: " + str(e))
mwarn("atomic Tries left: " + str(tries))
return wrapped_f
limit = 20
# Should we make this repeat more than once? kind of like serialized() with a parameter?
def reauth(func):
def wrapper(self, *args, **kwargs):
giveup_error = False
final_result = False
failed_once = False
for attempt in range(0, limit) :
retry_auth = False
permanent_error = False
regular_error = False
giveup_error = False
try :
result = func(self, *args, **kwargs)
final_result = True
if failed_once :
mverbose("Call 1 success")
except PossibleResourceNotFound, e :
mverbose("First time with possible resource not found (attempt " + str(attempt) + ". Will re-auth and try one more time: " + str(e))
retry_auth = True
# This parameter should never get removed from the kwargs
# We do see cases where this exception gets thrown twice
# if the argument is removed under simultaneous I/O failures,
# in which case we're just playing cat and mouse, and we really
# just need to fail to the user.
kwargs["second_time"] = True
except retriable_errors, e :
mverbose("Retriable regular error: " + str(e))
retry_auth = True
giveup_error = e
except IOError, e:
if e.errno in bad_errnos:
mverbose("Retriable IOError: " + str(e) + ". Probably due to a timeout: " + str(e))
retry_auth = True
giveup_error = e
else :
merr("Actual error number: " + str(e.errno))
for line in format_exc().splitlines() :
merr(line)
permanent_error = e
except (CommunicationError, ResourceNotFound, ResourceConflict), e :
merr("regular error: " + str(e))
regular_error = e
except couch_ServerError, e :
((status, error),) = e.args
permanent_error = e
if int(status) == 413 :
merr("Code 413 means nginx request entity too large or couch's attachment size is too small: " + name)
elif int(status) in server_errors :
# Database failure. Retry again too.
retry_auth = True
mverbose("Retriable Server error: " + str(status) + " " + str(error))
else :
merr("Unhandled Server error: " + str(status) + " " + str(error))
except Exception, e :
for line in format_exc().splitlines() :
merr(line)
permanent_error = e
finally :
if retry_auth :
if attempt == (limit - 1) :
break
if attempt >= 2 :
mwarn("Starting to get worried after " + str(attempt) + " attempts about: " + str(giveup_error))
try :
self.reauthorize(e = e)
except CommunicationError, e :
merr("Re-authorization failed at attempt " + str(attempt) + ", but we'll keep trying.")
except Exception, e :
merr("WTF: " + str(e))
if attempt > 0 :
sleep(1)
failed_once = True
elif regular_error :
raise regular_error
elif permanent_error :
raise CommunicationError("Unauthorized: " + str(permanent_error))
else :
if final_result :
if failed_once :
mverbose("Call 2 success")
return result
if failed_once :
mverbose("Might loop")
raise CommunicationError("Ran out of couch retries on attempt: " + str(attempt) + ": " + str(giveup_error))
return wrapper
class AuthBase(object) :
def reauthorize(self, e = False) :
if e :
mwarn("Error Likely due to a timeout: " + str(e))
mdebug("Re-authenticating database.")
try :
try :
getattr(self, "server")
self.server.cookie = False
self.server.auth()
self.db.resource.headers["Cookie"] = self.server.cookie
except AttributeError, e :
merr("Re-authenticating server.")
self.cookie = False
self.auth()
except Exception, e :
raise CommunicationError("Failed to re-authenticate: " + str(e))
mdebug("Authenticated.")
def gimme_cookie(self) :
try :
try :
getattr(self, "server")
if "Cookie" in self.db.resource.headers :
return self.db.resource.headers["Cookie"]
return False
except AttributeError, e :
if "Cookie" in self.couch_server.resource.headers["Cookie"] :
return self.couch_server.resource.headers["Cookie"]
return False
except Exception, e :
raise CommunicationError("Failed to get cookie: " + str(e))
class MicaDatabase(AuthBase) :
def try_get(self, name) :
return self.__getitem__(name, false_if_not_found = True)
def check_for_unauthorized(e) :
((status, error),) = e.args
mwarn("Server error: " + str(status) + " " + str(error))
if int(status) == 413 :
mdebug("Code 413 means nginx request entity too large or couch's attachment size is too small: " + name)
# Database failure. Retry again too.
if int(status) in server_errors :
raise Unauthorized
raise CommunicationError("MLL Unvalidated: " + str(e))
class MicaDatabaseCouchDB(MicaDatabase) :
def __init__(self, db, server, dbname) :
self.db = db
self.username = False
self.password = False
self.server = server
self.dbname = dbname
# example:
#server will have: {u'update_seq': 7967, u'disk_size': 689025146, u'purge_seq': 0, u'doc_count': 6653, u'compact_running': False, u'db_name': u'mica', u'doc_del_count': 1048, u'instance_start_time': u'1472644998115128', u'committed_update_seq': 7967, u'data_size': 481650247, u'disk_format_version': 6}
#mobile will have: db_name, db_uuid, doc_count, update_seq, disk_size, instance_start_time
@reauth
def info(self, second_time = False) :
try :
return self.db.info()
except couch_ResourceNotFound, e :
# This happens during DB timeouts only for the _users database
if self.dbname.count("_users") and not second_time:
raise PossibleResourceNotFound(self.dbname)
mdebug("Get info not found error: " + self.dbname)
raise ResourceNotFound(str(e))
@reauth
def get_security(self) :
return self.db.security
@reauth
def set_security(self, doc) :
self.db.security = doc
@reauth
def __setitem__(self, name, doc, second_time = False) :
try :
self.db[name] = doc
except couch_ResourceNotFound, e :
# This happens during DB timeouts only for the _users database
if name.count("org.couchdb.user") and not second_time:
raise PossibleResourceNotFound(name)
mdebug("Set key not found error: " + name)
raise ResourceNotFound(str(e))
except couch_ResourceConflict, e :
mdebug("Set key conflict error: " + name)
setfail = True
# This sometimes happens in the middle of a database failure
# before the DB failed, then it will appear to be a conflict. Let's try to first
# verify if that was the case no not fail the application.
if self.doc_exist(name) :
testdoc = doc.copy()
olddoc = self.__getitem__(name).copy()
if "_rev" in doc :
del testdoc["_rev"]
if "_rev" in olddoc :
del olddoc["_rev"]
if "_id" not in testdoc :
testdoc["_id"] = name
if name.count("org.couchdb.user") :
for subkey in ["iterations", "password_scheme", "salt", "derived_key"] :
if subkey in olddoc :
del olddoc[subkey]
if subkey in testdoc :
del testdoc[subkey]
if "password" in testdoc :
del testdoc["password"]
if olddoc == testdoc :
setfail = False
mwarn("Recovery complete. Yay.")
else :
merr("It's not equal. That sucks: " + str(olddoc) + " != " + str(testdoc))
if setfail :
raise ResourceConflict(str(e), e)
@reauth
def __getitem__(self, name, false_if_not_found = False, second_time = False, rev = False, open_revs = False) :
try :
if rev :
return self.db.get(name, rev = rev)
elif open_revs :
return self.db.get(name, open_revs = open_revs)
else :
return self.db[name]
except couch_ResourceNotFound, e :
# This happens during DB timeouts only for the _users database
if name.count("org.couchdb.user") and not second_time :
raise PossibleResourceNotFound(name)
if false_if_not_found :
return False
else :
raise ResourceNotFound("Cannot lookup key: " + name, e)
@reauth
def delete_doc(self, doc) :
self.db.delete(doc)
@reauth
def __delitem__(self, name, second_time = False) :
revs = []
try :
all_deleted = False
count = -1
while not all_deleted :
count += 1
all_deleted = True
docs = self.__getitem__(name, open_revs = "all")
for doc in docs :
if "_deleted" in doc["ok"] :
continue
all_deleted = False
mverbose(str(count) + ") DELETE Found undeleted revision: " + name + ": " + doc["ok"]["_rev"])
olddoc = self.__getitem__(name, rev = doc["ok"]["_rev"])
if olddoc is not None :
mverbose(str(count) + ") DELETE Deleting...")
try :
self.delete_doc(olddoc)
except (CommunicationError, couch_ResourceNotFound), e :
mwarn("OK if not found. Will try again: " + str(e))
'''
doc = self.db[name]
if "_conflicts" in doc :
mdebug("FOUND conflict revisions.")
revs += doc["_conflicts"]
if "_deleted_conflicts" in doc :
mdebug("FOUND deleted conflict revisions.")
revs += doc["_deleted_conflicts"]
for rev in revs :
olddoc = self.db.get(name, rev=rev)
self.db.delete(olddoc)
#del self.db[name]
'''
except ResourceNotFound, e :
# This happens during DB timeouts only for the _users database
if name.count("org.couchdb.user") and not second_time :
raise PossibleResourceNotFound(name)
raise e
@reauth
def delete_attachment(self, doc, filename) :
self.db.delete_attachment(doc, filename)
@reauth
def purge(self, doc_list) :
self.db.purge(doc_list)
@reauth
def put_attachment(self, name, filename, contents, new_doc = False) :
if not new_doc :
mdebug("No existing doc. Will make a new one")
trydelete = True
if self.doc_exist(name) is True :
mdebug("Deleting original @ " + name)
doc = self.__getitem__(name)
self.__delitem__(name)
self.purge([doc])
trydelete = False
mdebug("Synthesize foo doc")
doc = { "foo" : "bar"}
# This 'translated_at' is because of bug: https://issues.apache.org/jira/browse/COUCHDB-1415
# Supposedly fixed in CouchDB 2.0
doc["translated_at"] = time()
if trydelete :
try :
doc["_rev"] = self.__getitem__(name)["_rev"]
mdebug("Old revision found.")
except ResourceNotFound, e :
mdebug("No old revision found.")
pass
mdebug("Going to write: " + str(doc) + " to doc id " + name + " under filename " + filename)
self.__setitem__(name, doc)
doc = self.__getitem__(name)
else :
doc = new_doc
if type(contents) != file :
mdebug("Putting attachment of length: " + str(len(contents)))
return self.db.put_attachment(doc, contents, filename)
@reauth
def get_attachment(self, name, filename) :
obj = self.db.get_attachment(name, filename)
if obj is not None :
return obj.read()
else :
raise CommunicationError("No such attachment: " + name + " => " + filename)
@reauth
def get_attachment_to_path(self, name, filename, path) :
sourcebytes = 0
obj = self.db.get_attachment(name, filename)
if obj is not None :
fh = open(path, 'wb')
while True :
byte = obj.read(4096)
if byte :
sourcebytes += len(byte)
fh.write(byte)
else :
break
fh.close()
else :
raise CommunicationError("No such attachment: " + name + " => " + filename)
return sourcebytes
def listen(self, username, password, port) :
return port
def get_attachment_meta(self, name, filename) :
return self.__getitem__(name)["_attachments"][filename]
@reauth
def doc_exist(self, name, second_time = False) :
try :
self.db[name]
except couch_ResourceNotFound, e :
# This happens during DB timeouts only for the _users database
if name.count("org.couchdb.user") and not second_time :
raise PossibleResourceNotFound(name)
((error, reason),) = e.args
mverbose("Doc exist returns not found: " + reason)
return False
return True
def iocheck(self, e) :
if e.errno in bad_errnos :
try :
self.reauthorize(e = e)
except CommunicationError, e :
mdebug("iocheck Re-authorization failed, but we'll keep trying.")
else :
mwarn("Actual error number: " + str(e.errno))
raise e
def do_check_for_unauthorized(self, e) :
try :
check_for_unauthorized(e)
raise CommunicationError("Failed to perform view: " + str(e))
except Unauthorized, e :
try :
self.reauthorize(e = e)
except CommunicationError, e :
mdebug("error check Re-authorization failed, but we'll keep trying.")
def error_check(self, errors) :
if errors["errors_left"] > 0 :
mwarn("Server errors left: " + str(errors["errors_left"]))
errors["errors_left"] -= 1
sleep(1)
else :
merr("No errors_left remaining.")
raise CommunicationError("Failed to perform view. Ran out of tries.")
def couchdb_pager(self, view_name='_all_docs',
startkey=None, startkey_docid=None,
endkey=None, endkey_docid=None, bulk=5000, stale = False):
# Request one extra row to resume the listing there later.
options = {'limit': bulk + 1}
if stale :
options["stale"] = stale
if startkey:
options['startkey'] = startkey
if startkey_docid:
options['startkey_docid'] = startkey_docid
if endkey:
options['endkey'] = endkey
if endkey_docid:
options['endkey_docid'] = endkey_docid
yielded_rows = {}
errors = {"errors_left" : limit}
while errors["errors_left"] > 0 :
try:
view = self.db.view(view_name, **options)
rows = []
done = False
# If we got a short result (< limit + 1), we know we are done.
if len(view) <= bulk:
rows = view.rows
done = True
else:
# Otherwise, continue at the new start position.
rows = view.rows[:-1]
last = view.rows[-1]
options['startkey'] = last.key
options['startkey_docid'] = last.id
for row in self.do_rows(rows, yielded_rows) :
yield row
if not done :
continue
break
except retriable_errors, e :
try :
self.reauthorize(e = e)
except CommunicationError, e :
mdebug("view 1) check Re-authorization failed, but we'll keep trying.")
except IOError, e:
self.iocheck(e)
except couch_ServerError, e :
self.do_check_for_unauthorized(e)
self.error_check(errors)
def do_rows(self, rows, yielded_rows) :
for row in rows :
if row["key"] is None or (("id" in row and row["id"] not in yielded_rows) or row["value"]["_id"] not in yielded_rows) :
if row["key"] is not None :
_id = row["id"] if "id" in row else row["value"]["_id"]
yielded_rows[_id] = True
yield row
def view(self, *args, **kwargs) :
view_name = args[0]
mverbose("Query view: " + view_name)
if "keys" in kwargs :
keylist = []
username = kwargs["username"]
for key in kwargs["keys"] :
keylist.append([username, key])
kwargs["keys"] = keylist
if "username" in kwargs :
del kwargs["username"]
if "keys" in kwargs :
yielded_rows = {}
errors = {"errors_left" : limit}
while errors["errors_left"] > 0 :
try :
for row in self.do_rows(self.db.view(*args, **kwargs), yielded_rows) :
yield row
break
except retriable_errors, e :
try :
self.reauthorize(e = e)
except CommunicationError, e :
mdebug("view 2) check Re-authorization failed, but we'll keep trying.")
except IOError, e:
self.iocheck(e)
except couch_ServerError, e :
self.do_check_for_unauthorized(e)
self.error_check(errors)
else :
kwargs["view_name"] = view_name
kwargs["bulk"] = 50
for row in self.couchdb_pager(**kwargs) :
yield row
@reauth
def compact(self, *args, **kwargs) :
self.db.compact(*args, **kwargs)
@reauth
def cleanup(self, *args, **kwargs) :
self.db.cleanup(*args, **kwargs)
def close(self) :
pass
def runloop(self) :
mdebug("Server runloop - nothing to do.")
def pull_percent(self) :
return "100.0"
def push_percent(self) :
return "100.0"
def detach_thread(self) :
pass
# FIXME: need try's here so we return our "NotFound"
# instead of our not found
class MicaServerCouchDB(AuthBase) :
def __init__(self, url = False, username = False, password = False, cookie = False, refresh = False) :
self.url = url
self.cookie = cookie
self.refresh = refresh
if refresh :
self.username = username
self.password = password
self.couch_server = Server(url)
if refresh :
assert(self.url)
assert(self.username)
assert(self.password)
self.first_auth(username, password)
@reauth
def first_auth(self, username, password) :
self.auth(username, password)
def get_cookie(self, url, username, password) :
username_unquoted = myquote(username)
password_unquoted = myquote(password)
full_url = url.replace("//", "//" + username_unquoted + ":" + password_unquoted + "@")
tmp_server = Server(full_url)
mverbose("Requesting cookie.")
try :
code, message, obj = tmp_server.resource.post('_session',headers={'Content-Type' : 'application/x-www-form-urlencoded'}, body="name=" + username_unquoted + "&password=" + password_unquoted)
except UnicodeDecodeError :
# CouchDB folks messed up badly. This is ridiculous that I have
# to do this
mwarn("Retrying another way....")
username_unquoted = username_unquoted.encode("latin1").decode("latin1")
password_unquoted = password_unquoted.encode("latin1").decode("latin1")
code, message, obj = tmp_server.resource.post('_session',headers={'Content-Type' : 'application/x-www-form-urlencoded'}, body="name=" + username_unquoted + "&password=" + password_unquoted)
except UnicodeEncodeError :
mwarn("Retrying a third way....")
save = couchdb.http.basic_auth
def basic_auth_override(credentials) :
if credentials:
token = base64_b64encode('%s:%s' % credentials)
return ('Basic %s' % token.strip()).encode('ascii')
couchdb.http.basic_auth = basic_auth_override
try :
code, message, obj = tmp_server.resource.post('_session',headers={'Content-Type' : 'application/x-www-form-urlencoded'}, body="name=" + username_unquoted + "&password=" + password_unquoted)
except Exception, e :
merr(str(e))
couchdb.http.basic_auth = save
raise e
couchdb.http.basic_auth = save
if (code != 200) :
raise CommunicationError("MLL Unauthorized: " + username)
cookie = message["Set-Cookie"].split(";", 1)[0].strip()
mverbose("Received cookie: " + cookie)
return cookie
def auth(self, username = False, password = False) :
mverbose("Reauth start")
if not username or not password :
assert(self.username)
assert(self.password)
assert(self.refresh)
username = self.username
password = self.password
if not self.cookie :
mverbose("No cookie for user: " + username)
self.cookie = self.get_cookie(self.url, username, password)
else :
mdebug("Reusing cookie: " + self.cookie)
assert(self.cookie)
self.couch_server.resource.headers["Cookie"] = self.cookie
mverbose("Reauth done")
@reauth
def __getitem__(self, dbname) :
if dbname in self.couch_server :
db = self.couch_server[dbname]
else :
db = self.couch_server.create(dbname)
return MicaDatabaseCouchDB(db, self, dbname)
@reauth
def __delitem__(self, name) :
del self.couch_server[name]
@reauth
def __contains__(self, dbname) :
return True if dbname in self.couch_server else False
class AndroidMicaDatabaseCouchbaseMobile(MicaDatabase) :
def __init__(self, db, name) :
self.db = db
self.dbname = name
mdebug("Android CouchBase Mobile python adapter initialized")
def __setitem__(self, name, doc) :
try :
err = self.db.put(self.dbname, name, String(dumps(doc)))
if err != "" :
raise CommunicationError("Error occured putting document: " + name + " " + err)
except Exception, e :
raise CommunicationError("Error occured putting document: " + str(e), e)
def __getitem__(self, name, false_if_not_found = False) :
try :
doc = self.db.get(String(self.dbname), String(name))
except Exception, e :
raise CommunicationError("Error occured getting document: " + name + " " + str(e), e)
if doc == "" :
if false_if_not_found :
return False
else :
mwarn("Cannot lookup key: " + name)
raise ResourceNotFound("Cannot lookup key: " + name)
if doc is not None :
return loads(doc)
# return was None (null)
raise CommunicationError("Bad exception occured getting document: " + name)
def info(self) :
try :
return loads(self.db.info(String(self.dbname)))
except Exception, e :
raise CommunicationError("Error occured getting database info: " + self.dbname + " " + str(e), e)
if info is None :
raise ResourceNotFound("Could not get database info: " + self.dbname)
def __delitem__(self, name) :
try :
err = self.db.delete(String(self.dbname), String(name))
except Exception, e :
raise CommunicationError("Error occured deleting document: " + name + " " + str(e), e)
if err != "" :
raise ResourceNotFound("Could not delete document: " + name + " " + err)
def put_attachment(self, name, filename, contents, doc = False) :
raise NotImplementedError("Sorry, the mobile version does not allow importing new stories, so creating new attachments is not required today.")
def get_attachment_to_path(self, name, filename, path) :
try :
attach = self.db.get_attachment_to_path(String(self.dbname), String(name), String(filename), String(path))
except Exception, e :
raise CommunicationError("Error getting attachment to path: " + name + " " + str(e), e)
if attach is None :
raise ResourceNotFound("Could not find attachment to path for document: " + name)
def get_attachment(self, name, filename) :
try :
attach = self.db.get_attachment(String(self.dbname), String(name), String(filename))
except Exception, e :
raise CommunicationError("Error getting attachment: " + name + " " + str(e), e)
if attach is None :
raise ResourceNotFound("Could not find attachment for document: " + name)
# The ByteArray pyjnius is actually a 'memoryview' from python that
# represents the native java byte[] object that was returned,
# which you can google about. MemoryViews are shared-memory versions
# of native python bytearrays which can be maped into a string,
# like this.
return "".join(map(chr, attach))
def get_attachment_meta(self, name, filename) :
try :
meta = self.db.get_attachment_meta(String(self.dbname), String(name), String(filename))
except Exception, e :
raise CommunicationError("Error getting attachment metadata: " + name + " " + str(e), e)
if meta is None :
raise ResourceNotFound("Could not find attachment meta for document: " + name)
return loads(meta)
def doc_exist(self, name) :
try :
result = self.db.doc_exist(String(self.dbname), String(name))
if result == "error" :
raise CommunicationError("Error occured checking document existence: " + name)
return True if result == "true" else False
except Exception, e :
raise CommunicationError("Could test document existence: " + name + " " + str(e), e)
def view(self, name, startkey = False, endkey = False, keys = False, stale = False, username = False) :
seed = False
uuid = False
err_msg = False
e = False
self.updateView("$('#viewstat').addClass('alert-danger');")
try :
parts = name.split("/")
assert(len(parts) == 2)
design = parts[0]
vname = parts[1]
params = {}
if startkey :
params["startkey"] = startkey
if endkey :
params["endkey"] = endkey
if keys :
uuid = str(uuid4())
for key in keys :
assert(isinstance(key, str) or isinstance(key, unicode))
self.db.view_seed(String(uuid), String(username), String(key))
seed = True
params["keys"] = uuid
if stale :
params["stale"] = stale
if len(params) == 0 :
params = ""
else :
params = dumps(params)
it = self.db.view(String(self.dbname), String(design), String(vname), String(params), String(str(username)))
if it is None :
raise CommunicationError("Error occured for view: " + name)
while True :
has_next = self.db.view_has_next(it)
if not has_next :
break
result = self.db.view_next(it)
if result is None :
raise CommunicationError("Iteration error occured for view: " + name)
j = loads(result)
yield j["result"]
except Exception, err :
err_msg = "Error getting view: " + name + " " + str(err)
except CommunicationError, e :
err_msg = str(err)
finally :
self.updateView("$('#viewstat').removeClass('alert-danger');")
if seed and uuid:
self.db.view_seed_cleanup(String(uuid))
if err_msg :
raise CommunicationError(err_msg)
def compact(self, *args, **kwargs) :
if len(args) > 0 :
mwarn("Compacting a CBL view doesn't exist. Just pass.")
return
result = self.db.compact(self.dbname)
if result != "" :
raise CommunicationError("Compaction failed: " + result)
def cleanup(self, *args, **kwargs) :
# Does couchbase mobile have this?
#self.db.cleanup(*args, **kwargs)
pass
def close(self) :
try :
self.db.close(self.dbname)
except Exception, e :
raise CommunicationError("Database close failed for: " + name)
def runloop(self) :
pass
def pull_percent(self) :
return self.db.get_pull_percent()
def push_percent(self) :
return self.db.get_push_percent()
def stop_replication(self) :
self.db.stop_replication(self.dbname)
def filters(self, localdbname, params) :
if self.db.filters(localdbname, String(params)) == -1 :
mdebug("Filters failed. Boo. =(")
return False
else :
mdebug("Filters installed. Yay.")
return True
def replicate(self, url, user, pw, dbname, localdbname) :
username_unquoted = myquote(user)
password_unquoted = myquote(pw)
full_url = url.replace("//", "//" + username_unquoted + ":" + password_unquoted + "@") + "/" + dbname
if self.db.replicate(localdbname, String(full_url), False) == -1 :
mdebug("Replication failed. Boo. =(")
return False
else :
mdebug("Replication started. Yay.")
return True
def updateView(self, js) :
self.db.updateView(String(js))
def detach_thread(self) :
if jnius_detachable :
mdebug("Trying to detach...")
# https://github.com/kivy/pyjnius/commit/9e60152dc5172cfa0c2c90dbcc9e25d5c4cb2493
jnius_detach()
mdebug("Detached.")
else :
mdebug("No detach available. Need to upgrade. Will spin instead.")
while True :
sleep(3600)
def listen(self, username, password, port) :
# Since the user/pass will be fed locally through memory,
# and then accessed through javascript, I haven't found
# a need to escape them yet
#username_unquoted = myquote(username)
#password_unquoted = myquote(password)
port = self.db.listen(String(username), String(password), port)
if port == -1 :
raise CommunicationError("We failed to start the listener service for Couch. Check log for errors.")
return port
class AndroidMicaServerCouchbaseMobile(object) :
def __init__(self, db_already_local) :
self.db = db_already_local
def __getitem__(self, dbname) :
try :
self.db.start(String(dbname))
except Exception, e :
raise CommunicationError("Database creation failed for: " + name)
return AndroidMicaDatabaseCouchbaseMobile(self.db, dbname)
def __delitem__(self, name) :
try :
self.db.drop(String(name))
except Exception, e :
raise CommunicationError("Database deletion failed for: " + name)
def __contains__(self, dbname) :
return True if self.db.exists(String(dbname)) else False
class iosMicaDatabaseCouchbaseMobile(MicaDatabase) :
def __init__(self, db, name) :
self.db = db
self.dbname = name
mdebug("ios CouchBase Mobile python adapter initialized")