#!/usr/bin/env python
# Find enumerations in a gir file

import os,sys
from xml.etree.cElementTree import parse

tree = parse(sys.argv[1])

CORE_NS = "http://www.gtk.org/introspection/core/1.0"
C_NS = "http://www.gtk.org/introspection/c/1.0"
GLIB_NS = "http://www.gtk.org/introspection/glib/1.0"

def _corens(tag):
    return '{%s}%s' % (CORE_NS, tag)

def _glibns(tag):
    return '{%s}%s' % (GLIB_NS, tag)

def _cns(tag):
    return '{%s}%s' % (C_NS, tag)

def _find_first_child(node, name_or_names):
    if isinstance(name_or_names, str):
        for child in node.getchildren():
            if child.tag == name_or_names:
                return child
    else:
        for child in node.getchildren():
            if child.tag in name_or_names:
                return child
    return None

def _find_children(node, name):
    return [child for child in node.getchildren() if child.tag == name]

repo = tree.getroot()
assert repo is not None
assert repo.tag == _corens('repository')
namespace = _find_first_child(repo, _corens('namespace'))
assert namespace is not None
for node in _find_children(namespace, _corens('enumeration')):
    for member in _find_children(node, _corens('member')):
        v = int(member.attrib['value'])
        if v >= 2147483647:
            print "%s.%s.%s = %d" % (namespace.attrib['name'],
                                  node.attrib['name'],
                                  member.attrib['name'],
                                  v)

