Synchronization of couchdb-views from the file system for Svarga.

 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
import os
import sys
from urlparse import urlsplit
from svarga.core.env import env
from svarga.core.settings import init_environment
from svarga.core.dispatch import command
from couchdbkit import Server
from couchdbkit.loaders import FileSystemDocLoader
from restkit.httpc import HttpClient, BasicAuth
class SvargaDocsLoaderHandler(object):
def __init__(self):
self.transport = HttpClient()
url = urlsplit(env.settings.COUCHDB_URL)
if url.scheme not in ['http', 'https']:
raise Exception
url.username and self.transport.add_authorization(
BasicAuth([url.username, url.password]))
server_uri = '%s://%s' % (url.scheme, url.netloc)
server = Server(server_uri, self.transport)
db_name = url.path[1:]
self.db = db_name in server and server[db_name] \
or server.create_db(db_name)
def sync(self, app_path, doc_dir, app_label):
loader = FileSystemDocLoader(app_path, doc_dir, design_name=app_label)
loader.sync(self.db)
@command(usage='[-d DOC_DIR_NAME]')
def sync_couchdb(ui, doc_dir=('d', '_design', 'Name of dir with couchdb design')):
init_environment(None)
app_labels = filter(lambda app: not app.startswith('svarga'),
env.settings.LOCAL_APPS)
couchdb_handler = SvargaDocsLoaderHandler()
for app_label in app_labels:
app_path = os.path.abspath(os.path.join(
sys.modules[app_label].__file__, "..")
)
if os.path.isdir(os.path.join(app_path, doc_dir)):
print 'sync "%s" in CouchDB' % app_label
couchdb_handler.sync(app_path, doc_dir, app_label)