diff options
-rw-r--r-- | backends/couch.py | 48 | ||||
-rw-r--r-- | backends/leap_backend.py | 23 | ||||
-rw-r--r-- | server.py | 10 |
3 files changed, 27 insertions, 54 deletions
diff --git a/backends/couch.py b/backends/couch.py index 56b12574..11122aa8 100644 --- a/backends/couch.py +++ b/backends/couch.py @@ -7,6 +7,7 @@ from u1db import errors from u1db.sync import LocalSyncTarget from u1db.backends.inmemory import InMemoryIndex from u1db.remote.server_state import ServerState +from u1db.errors import DatabaseDoesNotExist # couchdb from couchdb.client import Server, Document as CouchDocument from couchdb.http import ResourceNotFound @@ -30,7 +31,7 @@ class CouchDatabase(ObjectStore): @classmethod def open_database(cls, url, create): # get database from url - m = re.match('(.*)/([^/]+)$', url) + m = re.match('(^https?://[^/]+)/(.+)$', url) if not m: raise InvalidURLError url = m.group(1) @@ -40,8 +41,8 @@ class CouchDatabase(ObjectStore): server[dbname] except ResourceNotFound: if not create: - raise - return cls(url, dbname) + raise DatabaseDoesNotExist() + return cls(url, dbname) def __init__(self, url, database, replica_uid=None, full_commit=True, session=None): @@ -242,29 +243,26 @@ class CouchSyncTarget(LocalSyncTarget): source_replica_uid, source_replica_generation, source_replica_transaction_id) - class CouchServerState(ServerState): - - def open_database(self, path): - """ - Open a database at the given location. - """ - return CouchDatabase.open_database(path, create=False) - - def check_database(self, path): - """ - Check if the database at the given location exists. - """ - db = self.open_database(path) - db.close() - - def ensure_database(self, path): - """Ensure database at the given location.""" - db = CouchDatabase.open_database(path, + """ + Inteface of the WSGI server with the CouchDB backend. + """ + + def __init__(self, couch_url): + self.couch_url = couch_url + + def open_database(self, dbname): + # TODO: open couch + from leap.soledad.backends.couch import CouchDatabase + return CouchDatabase.open_database(self.couch_url + '/' + dbname, + create=False) + + def ensure_database(self, dbname): + from leap.soledad.backends.couch import CouchDatabase + db = CouchDatabase.open_database(self.couch_url + '/' + dbname, create=True) return db, db._replica_uid - def delete_database(self, path): - """Delete database at the given location.""" - db = CouchDatabase.open_database(path) - db.delete_database() + def delete_database(self, dbname): + from leap.soledad.backends.couch import CouchDatabase + CouchDatabase.delete_database(self.couch_url + '/' + dbname) diff --git a/backends/leap_backend.py b/backends/leap_backend.py index e1acabec..f9d37e19 100644 --- a/backends/leap_backend.py +++ b/backends/leap_backend.py @@ -7,7 +7,6 @@ from u1db import Document from u1db.remote import utils from u1db.remote.http_target import HTTPSyncTarget from u1db.remote.http_database import HTTPDatabase -from u1db.remote.server_state import ServerState from u1db.errors import BrokenSyncStream import uuid @@ -202,25 +201,3 @@ class LeapSyncTarget(HTTPSyncTarget): data = None return res['new_generation'], res['new_transaction_id'] - -class LeapServerState(ServerState): - """ - Inteface of the WSGI server with the CouchDB backend. - """ - - def __init__(self): - pass - - def open_database(self, url): - # TODO: open couch - from leap.soledad.backends.couch import CouchDatabase - return CouchDatabase(url, create=False) - - def ensure_database(self, url): - from leap.soledad.backends.couch import CouchDatabase - db = CouchDatabase(url, create=True) - return db, db._replica_uid - - def delete_database(self, url): - from leap.soledad.backends.couch import CouchDatabase - CouchDatabase.delete_database(url) @@ -7,13 +7,11 @@ This should be run with: from twisted.web.wsgi import WSGIResource from twisted.internet import reactor +from u1db.remote import http_app +from leap.soledad.backends.couch import CouchServerState -from u1db.remote import ( - http_app, - server_state, -) - -state = server_state.ServerState() +couch_url = 'http://localhost:5984' +state = CouchServerState(couch_url) # TODO: change working dir to something meaningful state.set_workingdir('/tmp') # TODO: write a LeapHTTPApp that will use Couch as backend instead of SQLite |