1 |
4902
|
daigle
|
#!/usr/bin/python
|
2 |
|
|
#
|
3 |
|
|
# '$RCSfile$'
|
4 |
|
|
# Copyright: 2000 Regents of the University of California
|
5 |
|
|
#
|
6 |
|
|
# '$Author: perry $'
|
7 |
|
|
# '$Date: 2006-12-15 10:23:32 -0800 (Fri, 15 Dec 2006) $'
|
8 |
|
|
# '$Revision: 3122 $'
|
9 |
|
|
#
|
10 |
|
|
# This program is free software; you can redistribute it and/or modify
|
11 |
|
|
# it under the terms of the GNU General Public License as published by
|
12 |
|
|
# the Free Software Foundation; either version 2 of the License, or
|
13 |
|
|
# (at your option) any later version.
|
14 |
|
|
#
|
15 |
|
|
# This program is distributed in the hope that it will be useful,
|
16 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18 |
|
|
# GNU General Public License for more details.
|
19 |
|
|
#
|
20 |
|
|
# You should have received a copy of the GNU General Public License
|
21 |
|
|
# along with this program; if not, write to the Free Software
|
22 |
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
23 |
|
|
#
|
24 |
|
|
# TODO:
|
25 |
|
|
# validate
|
26 |
|
|
# getNextDocid
|
27 |
|
|
# getNextRevision(docid)
|
28 |
|
|
# getDocids(keyword=None)
|
29 |
|
|
# query(keyword, returnfields)
|
30 |
|
|
# queryDict => same as above but returns python dictionary data structure
|
31 |
|
|
|
32 |
|
|
import httplib, urllib
|
33 |
|
|
|
34 |
|
|
class MetacatClient:
|
35 |
|
|
|
36 |
|
|
def __init__(self, server="knb.ecoinformatics.org", urlPath="/knb/metacat"):
|
37 |
|
|
self.metacatUrlPath = urlPath
|
38 |
|
|
self.metacatServer = server
|
39 |
|
|
self.sessionid = None
|
40 |
|
|
|
41 |
|
|
def getMetacatUrl(self):
|
42 |
|
|
return "http://" + self.metacatServer + self.metacatUrlPath
|
43 |
|
|
|
44 |
|
|
def login(self, username, password, organization=None):
|
45 |
|
|
|
46 |
|
|
if organization == 'NCEAS':
|
47 |
|
|
uid = 'uid=%s,o=NCEAS,dc=ecoinformatics,dc=org' % username
|
48 |
|
|
else:
|
49 |
|
|
uid = username
|
50 |
|
|
|
51 |
|
|
postdata = { 'action' : 'login',
|
52 |
|
|
'qformat' : 'xml',
|
53 |
|
|
'username' : uid,
|
54 |
|
|
'password' : password }
|
55 |
|
|
|
56 |
|
|
response = self.postRequest(postdata)
|
57 |
|
|
if response.find("<login>") != -1:
|
58 |
|
|
return True
|
59 |
|
|
else:
|
60 |
|
|
print response
|
61 |
|
|
return False
|
62 |
|
|
|
63 |
|
|
def logout(self):
|
64 |
|
|
postdata = { 'action' : 'logout',
|
65 |
|
|
'qformat' : 'xml'}
|
66 |
|
|
|
67 |
|
|
response = self.postRequest(postdata)
|
68 |
|
|
if response.find("<logout>") != -1:
|
69 |
|
|
return True
|
70 |
|
|
else:
|
71 |
|
|
return False
|
72 |
|
|
|
73 |
|
|
def read(self, docid, qformat="xml"):
|
74 |
|
|
postdata = { 'action' : 'read',
|
75 |
|
|
'qformat' : qformat,
|
76 |
|
|
'docid' : docid }
|
77 |
|
|
response = self.postRequest(postdata)
|
78 |
|
|
# if error node returned
|
79 |
|
|
if response.find("<error>") != -1:
|
80 |
|
|
return False
|
81 |
|
|
else:
|
82 |
|
|
return response
|
83 |
|
|
|
84 |
|
|
|
85 |
|
|
def insert(self, docid, doctext):
|
86 |
|
|
postdata = { 'action' : 'insert',
|
87 |
|
|
'doctext' : doctext,
|
88 |
|
|
'docid' : docid }
|
89 |
|
|
response = self.postRequest(postdata)
|
90 |
|
|
# if error node returned
|
91 |
|
|
return response
|
92 |
|
|
|
93 |
|
|
def update(self, docid, doctext):
|
94 |
|
|
postdata = { 'action' : 'update',
|
95 |
|
|
'doctext' : doctext,
|
96 |
|
|
'docid' : docid }
|
97 |
|
|
response = self.postRequest(postdata)
|
98 |
|
|
return response
|
99 |
|
|
|
100 |
|
|
def delete(self, docid):
|
101 |
|
|
postdata = { 'action' : 'delete',
|
102 |
|
|
'docid' : docid }
|
103 |
|
|
response = self.postRequest(postdata)
|
104 |
|
|
return response
|
105 |
|
|
|
106 |
|
|
def squery(self, pathquery, qformat="xml"):
|
107 |
|
|
postdata = { 'action' : 'squery',
|
108 |
|
|
'qformat' : qformat,
|
109 |
|
|
'query' : pathquery }
|
110 |
|
|
response = self.postRequest(postdata)
|
111 |
|
|
return response
|
112 |
|
|
|
113 |
|
|
def postRequest(self, postdata):
|
114 |
|
|
conn = httplib.HTTPConnection( self.metacatServer )
|
115 |
|
|
params = urllib.urlencode( postdata )
|
116 |
|
|
headers = { "Content-type" : "application/x-www-form-urlencoded",
|
117 |
|
|
"Accept" : "*/*"}
|
118 |
|
|
|
119 |
|
|
# If we have an active session, set the cookie
|
120 |
|
|
if self.sessionid is not None:
|
121 |
|
|
headers['Cookie'] = self.sessionid
|
122 |
|
|
|
123 |
|
|
conn.request( "POST", self.metacatUrlPath, params, headers )
|
124 |
|
|
response = conn.getresponse()
|
125 |
|
|
|
126 |
|
|
# If metacat responds with a new session id,
|
127 |
|
|
# register it with the metacat client instance
|
128 |
|
|
setcookie = response.getheader("set-cookie", None)
|
129 |
|
|
if setcookie:
|
130 |
|
|
jsid = setcookie.split(';')[0]
|
131 |
|
|
if jsid[:11] == "JSESSIONID=":
|
132 |
|
|
self.sessionid = jsid
|
133 |
|
|
|
134 |
|
|
if response.status == 200:
|
135 |
|
|
content = response.read()
|
136 |
|
|
else:
|
137 |
|
|
print " SERVER DID NOT RETURN 'OK'.... STATUS is " + str(response.status)
|
138 |
|
|
content = ""
|
139 |
|
|
conn.close()
|
140 |
|
|
return content
|