diff options
Diffstat (limited to 'backends/leap_backend.py')
-rw-r--r-- | backends/leap_backend.py | 57 |
1 files changed, 20 insertions, 37 deletions
diff --git a/backends/leap_backend.py b/backends/leap_backend.py index f73698f2..c3c52ee6 100644 --- a/backends/leap_backend.py +++ b/backends/leap_backend.py @@ -26,6 +26,8 @@ class DocumentEncryptionFailed(Exception): class LeapDocument(Document): """ + Encryptable and syncable document. + LEAP Documents are standard u1db documents with cabability of returning an encrypted version of the document json string as well as setting document content based on an encrypted version of json string. @@ -41,7 +43,7 @@ class LeapDocument(Document): def get_encrypted_json(self): """ - Returns document's json serialization encrypted with user's public key. + Return document's json serialization encrypted with user's public key. """ if not self._soledad: raise NoSoledadInstance() @@ -71,47 +73,28 @@ class LeapDocument(Document): doc="Determine if document should be synced with server." ) + # Returning the revision as string solves the following exception in + # Twisted web: + # exceptions.TypeError: Can only pass-through bytes on Python 2 + def _get_rev(self): + if self._rev is None: + return None + return str(self._rev) -class LeapDatabase(HTTPDatabase): - """Implement the HTTP remote database API to a Leap server.""" + def _set_rev(self, rev): + self._rev = rev - def __init__(self, url, document_factory=None, creds=None, soledad=None): - super(LeapDatabase, self).__init__(url, creds=creds) - self._soledad = soledad - self._factory = LeapDocument - - @staticmethod - def open_database(url, create): - db = LeapDatabase(url) - db.open(create) - return db - - @staticmethod - def delete_database(url): - db = LeapDatabase(url) - db._delete() - db.close() - - def _allocate_doc_id(self): - """Generate a unique identifier for this document.""" - return 'D-' + uuid.uuid4().hex # 'D-' stands for document - - def get_sync_target(self): - st = LeapSyncTarget(self._url.geturl()) - st._creds = self._creds - return st - - def create_doc_from_json(self, content, doc_id=None): - if doc_id is None: - doc_id = self._allocate_doc_id() - res, headers = self._request_json('PUT', ['doc', doc_id], {}, - content, 'application/json') - new_doc = self._factory(doc_id, res['rev'], content, - soledad=self._soledad) - return new_doc + rev = property( + _get_rev, + _set_rev, + doc="Wrapper to ensure `doc.rev` is always returned as bytes.") class LeapSyncTarget(HTTPSyncTarget): + """ + A SyncTarget that encrypts data before sending and decrypts data after + receiving. + """ def __init__(self, url, creds=None, soledad=None): super(LeapSyncTarget, self).__init__(url, creds) |