################################################################################
# File     : xmlpeek.py
# Function : xml peek library/script
################################################################################
# Newest version can be obtained at http://www.freshlime.org
# Send comments or questions to code at freshlime dot org
# $Id: xmlpeek.py 99 2008-07-04 15:21:20Z jbester $
################################################################################
# Copyright (c) 2008, J. Bester
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the distribution.
#     * The name of the authors names of its contributors may be used to 
#       endorse or promote products derived from this software without
#       specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
################################################################################
"""
XML Peek library/script.

Set value based of xpath expression in an xml file
"""

import libxml2
import sys

def xmlpeek( filename, xpath ):
    """
    Xml peek.
    Similar to xmlpeek in nant.  Returns value(s) based on xpath expression.

    filename - xml file path
    xpath - xpath expression
    value - value to set content of xpath expression to be
    """
    try:
        doc = libxml2.parseFile(filename)
        exps = doc.xpathEval( xpath )
        return [ url.content for url in exps ]
    except:
        return []

def main():
    """
    Main function:
    Called if __main__ module
    """
    prog = sys.argv[0]
    args = sys.argv[1:]
    if len( args ) != 2:
        print """\
        %s [filename] [xpath]
        Prints value in xml file based on xpath expression\
        """ % ( prog )
        return
    filename = args[ 0 ]
    xpath = args[ 1 ]
    result = xmlpeek( filename, xpath )
    if not result:
        print '%s: not successful' % ( prog )
    else:
        for item in result:
            print item
        
if __name__ == '__main__':
    main()