Revision 3107
Added by perry almost 18 years ago
src/python/metacat.py | ||
---|---|---|
1 |
#!/usr/bin/python |
|
2 |
# |
|
3 |
# TODO: |
|
4 |
# validate |
|
5 |
# getNextDocid |
|
6 |
# getNextRevision(docid) |
|
7 |
# getDocids(keyword=None) |
|
8 |
# query(keyword, returnfields) |
|
9 |
# queryDict => same as above but returns python dictionary data structure |
|
10 |
|
|
11 |
import httplib, urllib |
|
12 |
|
|
13 |
class MetacatClient: |
|
14 |
|
|
15 |
def __init__(self, server="pmark.msi.ucsb.edu:8180", urlPath="/knb_test/metacat"): |
|
16 |
self.metacatUrlPath = urlPath |
|
17 |
self.metacatServer = server |
|
18 |
self.sessionid = None |
|
19 |
|
|
20 |
def getMetacatUrl(self): |
|
21 |
return "http://" + self.metacatServer + self.metacatUrlPath |
|
22 |
|
|
23 |
def login(self, username, password, organization=None): |
|
24 |
|
|
25 |
if organization == 'NCEAS': |
|
26 |
uid = 'uid=%s,o=NCEAS,dc=ecoinformatics,dc=org' % username |
|
27 |
else: |
|
28 |
uid = username |
|
29 |
|
|
30 |
postdata = { 'action' : 'login', |
|
31 |
'qformat' : 'xml', |
|
32 |
'username' : uid, |
|
33 |
'password' : password } |
|
34 |
|
|
35 |
response = self.postRequest(postdata) |
|
36 |
if response.find("<login>") != -1: |
|
37 |
return True |
|
38 |
else: |
|
39 |
return False |
|
40 |
|
|
41 |
def logout(self): |
|
42 |
postdata = { 'action' : 'logout', |
|
43 |
'qformat' : 'xml'} |
|
44 |
|
|
45 |
response = self.postRequest(postdata) |
|
46 |
if response.find("<logout>") != -1: |
|
47 |
return True |
|
48 |
else: |
|
49 |
return False |
|
50 |
|
|
51 |
def read(self, docid, qformat="xml"): |
|
52 |
postdata = { 'action' : 'read', |
|
53 |
'qformat' : qformat, |
|
54 |
'docid' : docid } |
|
55 |
response = self.postRequest(postdata) |
|
56 |
# if error node returned |
|
57 |
if response.find("<error>") != -1: |
|
58 |
return False |
|
59 |
else: |
|
60 |
return response |
|
61 |
|
|
62 |
|
|
63 |
def insert(self, docid, doctext): |
|
64 |
postdata = { 'action' : 'insert', |
|
65 |
'doctext' : doctext, |
|
66 |
'docid' : docid } |
|
67 |
response = self.postRequest(postdata) |
|
68 |
# if error node returned |
|
69 |
if response.find("<error>") != -1: |
|
70 |
return False |
|
71 |
else: |
|
72 |
return response |
|
73 |
|
|
74 |
def update(self, docid, doctext): |
|
75 |
postdata = { 'action' : 'update', |
|
76 |
'doctext' : doctext, |
|
77 |
'docid' : docid } |
|
78 |
response = self.postRequest(postdata) |
|
79 |
return response |
|
80 |
|
|
81 |
def delete(self, docid): |
|
82 |
postdata = { 'action' : 'delete', |
|
83 |
'docid' : docid } |
|
84 |
response = self.postRequest(postdata) |
|
85 |
return response |
|
86 |
|
|
87 |
def squery(self, pathquery, qformat="xml"): |
|
88 |
postdata = { 'action' : 'squery', |
|
89 |
'qformat' : qformat, |
|
90 |
'query' : pathquery } |
|
91 |
response = self.postRequest(postdata) |
|
92 |
return response |
|
93 |
|
|
94 |
def postRequest(self, postdata): |
|
95 |
conn = httplib.HTTPConnection( self.metacatServer ) |
|
96 |
params = urllib.urlencode( postdata ) |
|
97 |
headers = { "Content-type" : "application/x-www-form-urlencoded", |
|
98 |
"Accept" : "*/*"} |
|
99 |
|
|
100 |
# If we have an active session, set the cookie |
|
101 |
if self.sessionid is not None: |
|
102 |
headers['Cookie'] = self.sessionid |
|
103 |
|
|
104 |
conn.request( "POST", self.metacatUrlPath, params, headers ) |
|
105 |
response = conn.getresponse() |
|
106 |
|
|
107 |
# If metacat responds with a new session id, |
|
108 |
# register it with the metacat client instance |
|
109 |
setcookie = response.getheader("set-cookie", None) |
|
110 |
if setcookie: |
|
111 |
jsid = setcookie.split(';')[0] |
|
112 |
if jsid[:11] == "JSESSIONID=": |
|
113 |
self.sessionid = jsid |
|
114 |
|
|
115 |
if response.status == 200: |
|
116 |
content = response.read() |
|
117 |
else: |
|
118 |
print " !!! SERVER DID NOT RETURN 'OK'.... STATUS is " + str(response.status) |
|
119 |
content = "" |
|
120 |
conn.close() |
|
121 |
return content |
|
122 |
|
|
0 | 123 |
src/python/transferEml.py | ||
---|---|---|
1 |
#!/usr/bin/python |
|
2 |
""" |
|
3 |
Transfers documents from one metacat server to another |
|
4 |
Author: matt perry |
|
5 |
Date: 09/28/06 |
|
6 |
""" |
|
7 |
|
|
8 |
import metacat |
|
9 |
import sys |
|
10 |
|
|
11 |
try: |
|
12 |
docidlist = sys.argv[1] |
|
13 |
except: |
|
14 |
|
|
15 |
print " usage: python transferEml.py docidlist.txt " |
|
16 |
print " where docidlist.txt is a line-delimited list of docids " |
|
17 |
|
|
18 |
sys.exit(1) |
|
19 |
|
|
20 |
print "Logging into destination server" |
|
21 |
dest = metacat.MetacatClient('yourserver:8180','/knb/metacat') |
|
22 |
dest.login('user','password','NCEAS') |
|
23 |
|
|
24 |
print "Logging into source server" |
|
25 |
src = metacat.MetacatClient('knb.ecoinformatics.org', '/knb/metacat') |
|
26 |
src.login('user','password','NCEAS') |
|
27 |
|
|
28 |
print "Open docid list and loop" |
|
29 |
for docid in open(docidlist,'r').readlines(): |
|
30 |
docid = docid.strip().replace('\n','') |
|
31 |
if docid != '' and docid is not None: |
|
32 |
print " reading docid " + docid |
|
33 |
eml = src.read(docid) |
|
34 |
if eml: |
|
35 |
print " inserting docid " + docid |
|
36 |
response = dest.insert(docid + ".1" ,eml) |
|
37 |
if not response: |
|
38 |
print " insert failed" |
|
39 |
else: |
|
40 |
print " docid " + docid + " not found" |
|
41 |
|
|
42 |
print "Logging out" |
|
43 |
dest.logout() |
|
44 |
src.logout() |
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
0 | 49 |
src/python/README | ||
---|---|---|
1 |
PyMetacat |
|
2 |
Python library to work with Metacat XML Databases. |
|
3 |
Author: Matthew Perry |
|
4 |
Date: 12/06/2006 |
|
5 |
|
|
6 |
Introduction |
|
7 |
------------- |
|
8 |
Metacat is a flexible XML database. It can store, search and rerieve XML documents of any schema and it used primarily for storing metadata documents. This library provides a python interface to any Metacat server allowing you to add, insert, update, delete and query documents. |
|
9 |
|
|
10 |
|
|
11 |
Installation |
|
12 |
------------ |
|
13 |
|
|
14 |
To install pymetcat, copy the metacat.py file into your python site-packages directory (or anywhere in $PYTHONPATH ). |
|
15 |
|
|
16 |
|
|
17 |
Examples |
|
18 |
------------ |
|
19 |
|
|
20 |
Along with transferEml.py (a full working example of pymetacat usage), here are some basic usage examples: |
|
21 |
|
|
22 |
########################################################################################## |
|
23 |
import metacat |
|
24 |
|
|
25 |
# Initialize |
|
26 |
mc = metacat.MetacatClient('www.server.net','/context/metacat') |
|
27 |
|
|
28 |
# Print the url |
|
29 |
print mc.getMetacatUrl() |
|
30 |
# http://www.server.net/context/metacat |
|
31 |
|
|
32 |
# Login |
|
33 |
response = mc.login('uid=user,o=ORG,dc=ecoinformatics,dc=org','PassW0Rd!') |
|
34 |
print "login : ", response |
|
35 |
|
|
36 |
# Read the eml |
|
37 |
docid = 'org.99.4.1' |
|
38 |
eml = mc.read(docid) |
|
39 |
#print eml |
|
40 |
|
|
41 |
# Insert the read document as a new copy w/ new docid |
|
42 |
newdocid = 'anotherorg.905' |
|
43 |
response = mc.insert(newdocid + ".1",eml) |
|
44 |
print response |
|
45 |
|
|
46 |
# Make some change to the xml text and update it |
|
47 |
eml.replace("Serengeti", "Some other") |
|
48 |
response = mc.update(newdocid + ".2",eml) |
|
49 |
print response |
|
50 |
|
|
51 |
# Delete the document |
|
52 |
response = mc.delete(newdocid + ".2") |
|
53 |
print "DELETE" |
|
54 |
print response |
|
55 |
|
|
56 |
|
|
57 |
pathquery = """ |
|
58 |
<pathquery version="1.0"> |
|
59 |
<meta_file_id>unspecified</meta_file_id> |
|
60 |
<querytitle>unspecified</querytitle> |
|
61 |
<returnfield>dataset/title</returnfield> |
|
62 |
<returndoctype></returndoctype> |
|
63 |
<querygroup operator="UNION"> |
|
64 |
<queryterm casesensitive="false" searchmode="contains"> |
|
65 |
<value>%</value> |
|
66 |
</queryterm> |
|
67 |
<queryterm casesensitive="false" searchmode="contains"> |
|
68 |
<value>Africa</value> |
|
69 |
<pathexpr>keyword</pathexpr> |
|
70 |
</queryterm> |
|
71 |
</querygroup> |
|
72 |
</pathquery> |
|
73 |
""" |
|
74 |
response = mc.squery(pathquery) |
|
75 |
print "===============" |
|
76 |
print response |
|
77 |
print "===============" |
|
78 |
|
|
79 |
|
|
80 |
response = mc.logout() |
|
81 |
print "logout : ", response |
|
82 |
######################################################################################### |
|
0 | 83 |
Also available in: Unified diff
Initial import of prototype python client library to interact with metacat