forked from thm-projects/arsnova-flashcards-2014
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateviews.py
102 lines (81 loc) · 2.71 KB
/
createviews.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import httplib, json
import sys
import socket
import os, io
import base64
class CouchConnection(httplib.HTTPConnection):
"""docstring for CouchConnection"""
def __init__(self, host="127.0.0.1", port=5984, username="admin", password="admin"):
httplib.HTTPConnection.__init__(self, host, port)
self.username = username
self.password = password
def request(self, method, path, body="", header={}):
if self.username != "" and self.password != "":
creds = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
header["Authorization"] = "Basic %s" % creds
httplib.HTTPConnection.request(self, method, path, body, header)
def database_exists(conn, db):
conn.request("HEAD", "/" + db)
res = conn.getresponse()
res.read()
return res.status == 200
def database_create(conn, db):
conn.request("PUT", "/" + db)
res = conn.getresponse()
res.read()
return res.status == 201
def view_exists(conn, view_url):
conn.request("HEAD", view_url)
res = conn.getresponse()
res.read()
return res.status == 200
def view_create(conn, view_url, view):
conn.request("POST", view_url, view.read(), { "Content-Type": "application/json" })
res = conn.getresponse()
res.read()
return res.status == 201
def view_read(conn, view_url):
conn.request("GET", view_url)
res = conn.getresponse()
return res.read()
def view_revision(conn, view_url):
view_document = json.loads(view_read(conn, view_url))
return view_document["_rev"]
def view_update_revision(conn, view_url, view):
view_updated = json.loads(view.read())
view_updated["_rev"] = view_revision(conn, view_url)
return json.dumps(view_updated)
def view_update(conn, view_url, view):
conn.request("PUT", view_url, view_update_revision(conn, view_url, view), { "Content-Type": "application/json" })
res = conn.getresponse()
res.read()
return res.status == 201
def view_process(conn, db, view):
view_url = "/" + db + "/_design/" + os.path.basename(view)
print view_url
if not view_exists(conn, view_url):
if not view_create(conn, "/" + db, open(view, "r")):
print "... creation FAILED!"
else:
if not view_update(conn, view_url, open(view, "r")):
print "... update FAILED!"
host = "localhost"
port = "5984"
db = "thmcards"
username = ""
password = ""
viewpath = "couchviews"
conn = CouchConnection(host, port, username, password)
try:
if not database_exists(conn, db):
print "Creating database '" + db + "'..."
if not database_create(conn, db):
print "... FAILED"
else:
print "Database '" + db + "' already exists."
for view in os.listdir(viewpath):
print "Creating view '" + view + "'..."
view_process(conn, db, viewpath + "/" + view)
except socket.error as e:
print "Could not connect to CouchDB <" + str(e) + ">! Exiting..."
sys.exit(1)