#!/usr/bin/python3

#
# A mini-parser to be used where properl XPath tool is not available
#

import os
import sys
try:
    from StringIO import StringIO
except ImportError:
    from io import StringIO

import libxml2


STATUS_NOMATCH = 1
STATUS_USAGE = 2

MATCH_FMT = os.environ.get('MATCH_FMT', '%s\n')


def usage():
    sys.stderr.write("usage: ./pxpath //XPATH/EXPR"
                     " /path/to/result.xml [ns_name:ns_url]...\n")
    sys.exit(STATUS_USAGE)


def debug(*msgs):
    if os.environ.get('DEBUG') == '1':
        [sys.stderr.write('debug: %s\n' % msg) for msg in msgs]


def write(body, origin):
    debug('----- BEGIN MATCH (%s) -----' % origin.__class__,
          body,
          '----- END MATCH (%s) -----' % origin.__class__)
    sys.stdout.write(MATCH_FMT % body)


if __name__ == '__main__':

    try:
        expr = sys.argv[1]
        file = sys.argv[2]
    except IndexError:
        usage()

    namespaces = []
    for arg in sys.argv[3:]:
        name, url = arg.split(':', 1)
        namespaces.append((name, url))

    doc = libxml2.parseFile(file)
    context = doc.xpathNewContext()
    [context.xpathRegisterNs(n, u) for n, u in namespaces]
    matches = context.xpathEvalExpression(expr)
    debug('expr=%r' % expr,
          'matches=%r' % matches)

    if isinstance(matches, str):
        write(matches, matches)
    else:
        if not matches:
            sys.exit(STATUS_NOMATCH)
        for match in matches:
            if isinstance(match, libxml2.xmlAttr):
                write(match.content, match)
            else:
#               value = match.serialize()
#               debug('value=%r' % value)
                write(match.serialize(), match)
