1 |
3107
|
perry
|
PyMetacat
|
2 |
3108
|
perry
|
Python client library to work with Metacat XML Databases.
|
3 |
3107
|
perry
|
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 |
3108
|
perry
|
To install pymetcat, copy the metacat.py file into your python site-packages directory (eg. /usr/lib/python2.4/site-packages) or anywhere in $PYTHONPATH including the current working directory.
|
15 |
3107
|
perry
|
|
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 |
3108
|
perry
|
|
23 |
|
|
|
24 |
|
|
# Load the metacat library
|
25 |
3107
|
perry
|
import metacat
|
26 |
|
|
|
27 |
3108
|
perry
|
# Initialize a metacat client connection
|
28 |
3107
|
perry
|
mc = metacat.MetacatClient('www.server.net','/context/metacat')
|
29 |
|
|
|
30 |
3108
|
perry
|
# Inspect the methods available through the MetacatClient object
|
31 |
|
|
dir(mc)
|
32 |
|
|
|
33 |
3107
|
perry
|
# Print the url
|
34 |
|
|
print mc.getMetacatUrl()
|
35 |
|
|
|
36 |
|
|
# Login
|
37 |
|
|
response = mc.login('uid=user,o=ORG,dc=ecoinformatics,dc=org','PassW0Rd!')
|
38 |
|
|
print "login : ", response
|
39 |
|
|
|
40 |
|
|
# Read the eml
|
41 |
|
|
docid = 'org.99.4.1'
|
42 |
|
|
eml = mc.read(docid)
|
43 |
3108
|
perry
|
print eml
|
44 |
3107
|
perry
|
|
45 |
|
|
# Insert the read document as a new copy w/ new docid
|
46 |
|
|
newdocid = 'anotherorg.905'
|
47 |
|
|
response = mc.insert(newdocid + ".1",eml)
|
48 |
|
|
print response
|
49 |
|
|
|
50 |
|
|
# Make some change to the xml text and update it
|
51 |
|
|
eml.replace("Serengeti", "Some other")
|
52 |
|
|
response = mc.update(newdocid + ".2",eml)
|
53 |
|
|
print response
|
54 |
|
|
|
55 |
|
|
# Delete the document
|
56 |
|
|
response = mc.delete(newdocid + ".2")
|
57 |
|
|
print "DELETE"
|
58 |
|
|
print response
|
59 |
|
|
|
60 |
|
|
|
61 |
|
|
pathquery = """
|
62 |
|
|
<pathquery version="1.0">
|
63 |
|
|
<meta_file_id>unspecified</meta_file_id>
|
64 |
|
|
<querytitle>unspecified</querytitle>
|
65 |
|
|
<returnfield>dataset/title</returnfield>
|
66 |
|
|
<returndoctype></returndoctype>
|
67 |
|
|
<querygroup operator="UNION">
|
68 |
|
|
<queryterm casesensitive="false" searchmode="contains">
|
69 |
3108
|
perry
|
<value>%</value>
|
70 |
|
|
</queryterm>
|
71 |
|
|
<queryterm casesensitive="false" searchmode="contains">
|
72 |
3107
|
perry
|
<value>Africa</value>
|
73 |
|
|
<pathexpr>keyword</pathexpr>
|
74 |
|
|
</queryterm>
|
75 |
|
|
</querygroup>
|
76 |
|
|
</pathquery>
|
77 |
|
|
"""
|
78 |
|
|
response = mc.squery(pathquery)
|
79 |
|
|
print response
|
80 |
|
|
|
81 |
|
|
response = mc.logout()
|
82 |
|
|
print "logout : ", response
|