From de73fc6969433a69ec6ba12ec508c3c93b83fcc6 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Thu, 20 Aug 2015 14:47:14 -0300 Subject: [refactor] Move constructor, use isinstance isinstance is better, as kaliy pointed out, and the constructor is also in a safer place on __init__.py to be explicit. Also re-apply a change from last rebase; --- .../leap/soledad/client/http_target/__init__.py | 45 ++++++++++++++++++++++ client/src/leap/soledad/client/http_target/api.py | 4 +- .../src/leap/soledad/client/http_target/fetch.py | 4 ++ common/src/leap/soledad/common/couch.py | 2 +- 4 files changed, 52 insertions(+), 3 deletions(-) diff --git a/client/src/leap/soledad/client/http_target/__init__.py b/client/src/leap/soledad/client/http_target/__init__.py index 7fa33153..7a5cea9f 100644 --- a/client/src/leap/soledad/client/http_target/__init__.py +++ b/client/src/leap/soledad/client/http_target/__init__.py @@ -24,6 +24,7 @@ after receiving. import logging +from leap.common.http import HTTPClient from leap.soledad.client.http_target.send import HTTPDocSender from leap.soledad.client.http_target.api import SyncTargetAPI from leap.soledad.client.http_target.fetch import HTTPDocFetcher @@ -43,3 +44,47 @@ class SoledadHTTPSyncTarget(SyncTargetAPI, HTTPDocSender, HTTPDocFetcher): the parsed documents that the remote send us, before being decrypted and written to the main database. """ + def __init__(self, url, source_replica_uid, creds, crypto, cert_file, + sync_db=None, sync_enc_pool=None): + """ + Initialize the sync target. + + :param url: The server sync url. + :type url: str + :param source_replica_uid: The source replica uid which we use when + deferring decryption. + :type source_replica_uid: str + :param creds: A dictionary containing the uuid and token. + :type creds: creds + :param crypto: An instance of SoledadCrypto so we can encrypt/decrypt + document contents when syncing. + :type crypto: soledad.crypto.SoledadCrypto + :param cert_file: Path to the certificate of the ca used to validate + the SSL certificate used by the remote soledad + server. + :type cert_file: str + :param sync_db: Optional. handler for the db with the symmetric + encryption of the syncing documents. If + None, encryption will be done in-place, + instead of retreiving it from the dedicated + database. + :type sync_db: Sqlite handler + :param sync_enc_pool: The encryption pool to use to defer encryption. + If None is passed the encryption will not be + deferred. + :type sync_enc_pool: leap.soledad.client.encdecpool.SyncEncrypterPool + """ + if url.endswith("/"): + url = url[:-1] + self._url = str(url) + "/sync-from/" + str(source_replica_uid) + self.source_replica_uid = source_replica_uid + self._auth_header = None + self.set_creds(creds) + self._crypto = crypto + self._sync_db = sync_db + self._sync_enc_pool = sync_enc_pool + self._insert_doc_cb = None + # asynchronous encryption/decryption attributes + self._decryption_callback = None + self._sync_decr_pool = None + self._http = HTTPClient(cert_file) diff --git a/client/src/leap/soledad/client/http_target/api.py b/client/src/leap/soledad/client/http_target/api.py index d83250ee..98a5b47e 100644 --- a/client/src/leap/soledad/client/http_target/api.py +++ b/client/src/leap/soledad/client/http_target/api.py @@ -84,13 +84,13 @@ class SyncTargetAPI(SyncTarget): """ raw = yield self._http_request(self._url) res = json.loads(raw) - defer.returnValue([ + defer.returnValue(( res['target_replica_uid'], res['target_replica_generation'], res['target_replica_transaction_id'], res['source_replica_generation'], res['source_transaction_id'] - ]) + )) def record_sync_info( self, source_replica_uid, source_replica_generation, diff --git a/client/src/leap/soledad/client/http_target/fetch.py b/client/src/leap/soledad/client/http_target/fetch.py index a991d2a2..d38ecb19 100644 --- a/client/src/leap/soledad/client/http_target/fetch.py +++ b/client/src/leap/soledad/client/http_target/fetch.py @@ -70,6 +70,10 @@ class HTTPDocFetcher(object): self._received_docs = 0 number_of_changes, ngen, ntrans = self._insert_received_doc(doc, 1, 1) + if ngen: + new_generation = ngen + new_transaction_id = ntrans + if defer_decryption: self._sync_decr_pool.start(number_of_changes) diff --git a/common/src/leap/soledad/common/couch.py b/common/src/leap/soledad/common/couch.py index 90f1a36f..08096f86 100644 --- a/common/src/leap/soledad/common/couch.py +++ b/common/src/leap/soledad/common/couch.py @@ -1320,7 +1320,7 @@ class CouchDatabase(CommonBackend): 'converged', at_gen is the insertion/current generation. :rtype: (str, int) """ - if type(doc) is not CouchDocument: + if not isinstance(doc, CouchDocument): doc = self._factory(doc.doc_id, doc.rev, doc.get_json()) self._save_source_info(replica_uid, replica_gen, replica_trans_id, number_of_docs, -- cgit v1.2.3