1 |
3107
|
perry
|
#!/usr/bin/python
|
2 |
|
|
#
|
3 |
3122
|
perry
|
# '$RCSfile$'
|
4 |
|
|
# Copyright: 2000 Regents of the University of California
|
5 |
|
|
#
|
6 |
|
|
# '$Author$'
|
7 |
|
|
# '$Date$'
|
8 |
|
|
# '$Revision$'
|
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 |
3107
|
perry
|
# 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 |
8265
|
leinfelder
|
def __init__(self, server="localhost:8080", urlPath="/metacat/metacat"):
|
37 |
3107
|
perry
|
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 |
|
|
return False
|
61 |
|
|
|
62 |
|
|
def logout(self):
|
63 |
|
|
postdata = { 'action' : 'logout',
|
64 |
|
|
'qformat' : 'xml'}
|
65 |
|
|
|
66 |
|
|
response = self.postRequest(postdata)
|
67 |
|
|
if response.find("<logout>") != -1:
|
68 |
|
|
return True
|
69 |
|
|
else:
|
70 |
|
|
return False
|
71 |
|
|
|
72 |
|
|
def read(self, docid, qformat="xml"):
|
73 |
|
|
postdata = { 'action' : 'read',
|
74 |
|
|
'qformat' : qformat,
|
75 |
|
|
'docid' : docid }
|
76 |
|
|
response = self.postRequest(postdata)
|
77 |
|
|
# if error node returned
|
78 |
|
|
if response.find("<error>") != -1:
|
79 |
|
|
return False
|
80 |
|
|
else:
|
81 |
|
|
return response
|
82 |
|
|
|
83 |
|
|
|
84 |
|
|
def insert(self, docid, doctext):
|
85 |
|
|
postdata = { 'action' : 'insert',
|
86 |
|
|
'doctext' : doctext,
|
87 |
|
|
'docid' : docid }
|
88 |
|
|
response = self.postRequest(postdata)
|
89 |
|
|
# if error node returned
|
90 |
|
|
if response.find("<error>") != -1:
|
91 |
|
|
return False
|
92 |
|
|
else:
|
93 |
|
|
return response
|
94 |
|
|
|
95 |
|
|
def update(self, docid, doctext):
|
96 |
|
|
postdata = { 'action' : 'update',
|
97 |
|
|
'doctext' : doctext,
|
98 |
|
|
'docid' : docid }
|
99 |
|
|
response = self.postRequest(postdata)
|
100 |
|
|
return response
|
101 |
|
|
|
102 |
|
|
def delete(self, docid):
|
103 |
|
|
postdata = { 'action' : 'delete',
|
104 |
|
|
'docid' : docid }
|
105 |
|
|
response = self.postRequest(postdata)
|
106 |
|
|
return response
|
107 |
|
|
|
108 |
|
|
def squery(self, pathquery, qformat="xml"):
|
109 |
|
|
postdata = { 'action' : 'squery',
|
110 |
|
|
'qformat' : qformat,
|
111 |
|
|
'query' : pathquery }
|
112 |
|
|
response = self.postRequest(postdata)
|
113 |
|
|
return response
|
114 |
|
|
|
115 |
|
|
def postRequest(self, postdata):
|
116 |
|
|
conn = httplib.HTTPConnection( self.metacatServer )
|
117 |
|
|
params = urllib.urlencode( postdata )
|
118 |
|
|
headers = { "Content-type" : "application/x-www-form-urlencoded",
|
119 |
|
|
"Accept" : "*/*"}
|
120 |
|
|
|
121 |
|
|
# If we have an active session, set the cookie
|
122 |
|
|
if self.sessionid is not None:
|
123 |
|
|
headers['Cookie'] = self.sessionid
|
124 |
|
|
|
125 |
|
|
conn.request( "POST", self.metacatUrlPath, params, headers )
|
126 |
|
|
response = conn.getresponse()
|
127 |
|
|
|
128 |
|
|
# If metacat responds with a new session id,
|
129 |
|
|
# register it with the metacat client instance
|
130 |
|
|
setcookie = response.getheader("set-cookie", None)
|
131 |
|
|
if setcookie:
|
132 |
|
|
jsid = setcookie.split(';')[0]
|
133 |
|
|
if jsid[:11] == "JSESSIONID=":
|
134 |
|
|
self.sessionid = jsid
|
135 |
|
|
|
136 |
|
|
if response.status == 200:
|
137 |
|
|
content = response.read()
|
138 |
|
|
else:
|
139 |
3122
|
perry
|
print " SERVER DID NOT RETURN 'OK'.... STATUS is " + str(response.status)
|
140 |
3107
|
perry
|
content = ""
|
141 |
|
|
conn.close()
|
142 |
|
|
return content
|