From 951ba59425a40d29cf6aea1d5ea56c92ef2404c1 Mon Sep 17 00:00:00 2001 From: drebs Date: Wed, 28 May 2014 16:00:48 -0300 Subject: Turn SQLCipher.sync_state into a ClientSyncState instance. --- client/src/leap/soledad/client/sqlcipher.py | 36 +++++++++++++++-------------- client/src/leap/soledad/client/sync.py | 20 ++++++++-------- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/client/src/leap/soledad/client/sqlcipher.py b/client/src/leap/soledad/client/sqlcipher.py index 0885a35f..74351116 100644 --- a/client/src/leap/soledad/client/sqlcipher.py +++ b/client/src/leap/soledad/client/sqlcipher.py @@ -57,7 +57,7 @@ from pysqlcipher import dbapi2 from u1db.backends import sqlite_backend from u1db import errors as u1db_errors -from leap.soledad.client.sync import Synchronizer +from leap.soledad.client.sync import Synchronizer, ClientSyncState from leap.soledad.client.target import SoledadSyncTarget from leap.soledad.common.document import SoledadDocument @@ -214,7 +214,6 @@ class SQLCipherDatabase(sqlite_backend.SQLitePartialExpandDatabase): syncable=syncable) self.set_document_factory(factory) self._syncers = {} - self._real_sync_state = None @classmethod def _open_database(cls, sqlcipher_file, password, document_factory=None, @@ -890,30 +889,29 @@ class SQLCipherDatabase(sqlite_backend.SQLitePartialExpandDatabase): if self._db_handle is not None: self._db_handle.close() - def _get_sync_state(self): + def _get_stored_sync_state(self): """ - Get the current sync state. + Retrieve the currently stored sync state. - :return: The current sync state. - :rtype: dict + :return: The current stored sync state or None if there's no stored + state. + :rtype: dict or None """ - if self._real_sync_state is not None: - return self._real_sync_state c = self._db_handle.cursor() c.execute("SELECT value FROM u1db_config" " WHERE name = 'sync_state'") val = c.fetchone() if val is None: return None - self._real_sync_state = json.loads(val[0]) - return self._real_sync_state + return json.loads(val[0]) - def _set_sync_state(self, state): + def _set_stored_sync_state(self, state): """ - Set the current sync state. + Stored the sync state. - :param state: The sync state to be set. - :type state: dict + :param state: The sync state to be stored or None to delete any stored + state. + :type state: dict or None """ c = self._db_handle.cursor() if state is None: @@ -923,9 +921,13 @@ class SQLCipherDatabase(sqlite_backend.SQLitePartialExpandDatabase): c.execute("INSERT OR REPLACE INTO u1db_config" " VALUES ('sync_state', ?)", (json.dumps(state),)) - self._real_sync_state = state - sync_state = property( - _get_sync_state, _set_sync_state, doc="The current sync state.") + stored_sync_state = property( + _get_stored_sync_state, _set_stored_sync_state, + doc="The current sync state dict.") + + @property + def sync_state(self): + return ClientSyncState(self) sqlite_backend.SQLiteDatabase.register_implementation(SQLCipherDatabase) diff --git a/client/src/leap/soledad/client/sync.py b/client/src/leap/soledad/client/sync.py index c29dd769..5285d540 100644 --- a/client/src/leap/soledad/client/sync.py +++ b/client/src/leap/soledad/client/sync.py @@ -86,29 +86,29 @@ class ClientSyncState(object): for attr in self._public_attr_keys: setattr(self, attr, self._public_attrs[attr]) # fetch info from stored sync state - sync_state = None + sync_state_dict = None if self._db is not None: - sync_state = self._db.sync_state - if sync_state is not None: + sync_state_dict = self._db.stored_sync_state + if sync_state_dict is not None: for attr in self._public_attr_keys: - setattr(self, attr, sync_state[attr]) + setattr(self, attr, sync_state_dict[attr]) def save(self): """ Save the current sync state in the database. """ - sync_state = {} + sync_state_dict = {} for attr in self._public_attr_keys: - sync_state[attr] = getattr(self, attr) + sync_state_dict[attr] = getattr(self, attr) if self._db is not None: - self._db.sync_state = sync_state + self._db.stored_sync_state = sync_state_dict def clear(self): """ Clear the sync state info data. """ if self._db is not None: - self._db.sync_state = None + self._db.stored_sync_state = None self._init_state() def has_stored_info(self): @@ -118,7 +118,7 @@ class ClientSyncState(object): :return: Whether there's any sync state info store on db. :rtype: bool """ - return self._db is not None and self._db.sync_state is not None + return self._db is not None and self._db.stored_sync_state is not None def __str__(self): return 'ClientSyncState: %s' % ', '.join( @@ -147,7 +147,7 @@ class Synchronizer(U1DBSynchronizer): sync_target = self.sync_target # recover current sync state from source database - sync_state = ClientSyncState(self.source) + sync_state = self.source.sync_state self.target_replica_uid = sync_state.target_replica_uid target_gen = sync_state.target_gen target_trans_id = sync_state.target_trans_id -- cgit v1.2.3