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.

No comments: