Wednesday, July 19, 2006

more IronPython + MarkLogic experimenting

I've been playing more with IronPython. Specifically, experimenting with using it along with the MarkLogic's XCC API.

Pretty ugly, but working re-implementation of the sample code to run queries.

# Re-implementation of SimpleQueryRunner.cs XCC example
# using IronPython

import clr
clr.AddReferenceToFile("MarklogicXcc.dll")

import Marklogic.Xcc
import System
import os
import sys

def execute(session,query):
"""Generator that yields result strings from
execution of the query"""

req = session.NewAdhocQuery(query)
for res in session.SubmitRequest(req).AsStrings():
yield res
return

def main():
if len(sys.argv) != 3:
print usage()
sys.exit(2)

query = "'Hello world'"
try:
f = open(sys.argv[2])
query = f.read()
f.close()

cs = Marklogic.Xcc.ContentSourceFactory.NewContentSource(
System.Uri(sys.argv[1]))
session = cs.NewSession()

for result in execute(session,query):
print result

except EnvironmentError,e:
sys.stderr.write("*** Error: %s\n"%e)
sys.exit(1)
except Exception,e:
#ugh, looks like exceptions from XCC are Exception
sys.stderr.write("*** Error: %s\n"%e)
sys.exit(1)

def usage():
return "usage: \n SimpleQueryRunner.py xcc://USER:PASSWORD@HOST:PORT/MLDATABASE <queryfile>"

if __name__ == "__main__":
main()

Figuring out more ways I might infiltrate my workplace with Python makes me happy happy. Beer also makes me happy. Mmm... Murphy's Stout.

Monday, July 17, 2006

MarkLogic XCC + IronPython = Sweet!

At work we're using MarkLogic to store and transform our XML content. You use XQuery to access the data, which has been pretty fun to learn. While looking at the release notes for MarkLogic 3.1-2, I saw that they've released the .NET version of their new XCC API for connecting with the server.

I've meant to start playing with IronPython for a while. And I've also meant to start learning C#/.NET stuff. So, I thought I'd try running the MarkLogic XCC.Net examples via IronPthon.

And, it works! Woohoo! Not that it does much yet...

import clr
clr.AddReferenceToFile("MarklogicXcc.dll")

import Marklogic.Xcc

import System

import os

doc = 'hamlet.xml'

#replace connection info and marklogic db name
contentSource = 'xcc://USER:PASSWORD@HOST:PORT/MLDBNAME'

print "Loading document ..."
Marklogic.Xcc.Examples.ContentLoader.Main(
System.Array[System.String](
[contentSource ,doc]
)
)

print "Fetching document ..."
Marklogic.Xcc.Examples.ContentFetcher.Main(
System.Array[System.String](
[contentSource,os.path.abspath(doc).replace('\\','/'),'-o','hamlet_fetched.xml']
)
)
print "Done."

Our collection of XSLT/XQuery/Java applications is a pain to deploy and do quick interactive testing against. Hopefully being able to script it with IronPython -- and maybe provide a nice interface with Windows Forms -- will allow quicker turnaround times.