1 |
3107
|
perry
|
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 |
|
|
#########################################################################################
|