#!/usr/bin/env python
#
# Copyright (C) 2004 Stefan Seefeld
# All rights reserved.
# Licensed to the public under the terms of the GNU LGPL (>= 2),
# see the file COPYING for details.
#

"""This simple tool can be used to introspect an AST as generated from the Dump.Formatter.
It requires the python binding for Daniel Veillard's libxml2 package (@see http://xmlsoft.org)"""

import sys
try:
    import libxml2
except ImportError:
    sys.stderr.write('this tool requires the libxml2 module\n')
    sys.exit(-1)

if len(sys.argv) != 3:

    print 'Usage : %s <xml> <xpath>'%sys.argv[0]
    sys.exit(-1)

doc = libxml2.parseFile(sys.argv[1])
ctxt = doc.xpathNewContext()
res = ctxt.xpathEval(sys.argv[2])

if type(res) == float:
    print res

elif type(res) == list: #node set
    if len(res) == 0: print '1 match'
    elif len(res) == 1: print '1 match :'
    else: print '%d matches :'%len(res)

    for r in res:
        print '%s (%s)'%(r.content, r.nodePath())

else:
    print 'unsupported return type', type(res)
