summaryrefslogtreecommitdiff
path: root/tools
diff options
context:
space:
mode:
authorEitan Isaacson <eitan@monotonous.org>2011-04-29 01:59:17 -0700
committerEitan Isaacson <eitan@monotonous.org>2011-05-02 10:18:49 -0700
commitf195b7aafa878568e7aae8ccc27e3716ae7c21cc (patch)
tree41a9868f025dedf323ad6b87bfcaf8a263d00601 /tools
parent6294dab42aec83a4e9edd314544903a67152b7fb (diff)
downloadcaribou-f195b7aafa878568e7aae8ccc27e3716ae7c21cc.tar.gz
libcaribou: Added a tool to ammend valac's gir.
I filed a bug for this: https://bugzilla.gnome.org/show_bug.cgi?id=648957
Diffstat (limited to 'tools')
-rwxr-xr-xtools/fix_gir.py63
1 files changed, 63 insertions, 0 deletions
diff --git a/tools/fix_gir.py b/tools/fix_gir.py
new file mode 100755
index 0000000..31190e3
--- /dev/null
+++ b/tools/fix_gir.py
@@ -0,0 +1,63 @@
+#!/usr/bin/python
+
+from xml.dom import minidom
+
+def purge_white_space_and_fix_namespace(node, indent=0):
+ if getattr(node, "tagName", None) == "namespace":
+ name = node.getAttribute("name")
+ node.setAttribute("name", name.lstrip('_'))
+ for child in [c for c in node.childNodes]:
+ if child.nodeType == node.TEXT_NODE or \
+ getattr(child, "tagName", None) == "annotation":
+ node.removeChild(child)
+ continue
+ purge_white_space_and_fix_namespace(child, indent+1)
+
+def find_ancestor(node, name):
+ if getattr(node, "tagName") == name:
+ return node
+ parent = getattr(node, "parentNode", None)
+ if not parent:
+ return None
+ return find_ancestor(parent, name)
+
+def fix_vfuncs(dom):
+ for f in dom.getElementsByTagName("callback"):
+ record = find_ancestor(f, "record")
+ if not record:
+ continue
+
+ name = record.getAttribute("name")
+ cname = record.getAttribute("c:type")
+
+ assert(name.endswith("Class"))
+ assert(cname.endswith("Class"))
+
+ params = (f.getElementsByTagName("parameters") or [None])[0]
+
+ if not params:
+ params = dom.createElement("parameters")
+ f.insertBefore(params, f.firstChild)
+
+ param = dom.createElement("parameter")
+ param.setAttribute("name", "self")
+ param.setAttribute("transfer-ownership", "none")
+ ptype = dom.createElement("type")
+ ptype.setAttribute("name", name[:-5])
+ ptype.setAttribute("c:type", cname[:-5])
+ param.appendChild(ptype)
+ params.insertBefore(param, params.firstChild)
+
+if __name__ == "__main__":
+ import sys
+
+ if len(sys.argv) != 2:
+ print "supply a gir file"
+ sys.exit(1)
+
+ dom = minidom.parse(sys.argv[-1])
+
+ purge_white_space_and_fix_namespace(dom)
+ fix_vfuncs(dom)
+
+ print dom.toprettyxml(indent=" ", newl="\n")