2007-06-26 02:42:41 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2006-2007 Async Open Source
|
|
|
|
# Henrique Romano <henrique@async.com.br>
|
|
|
|
# Johan Dahlin <jdahlin@async.com.br>
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
#
|
|
|
|
# TODO:
|
|
|
|
# Toolbars
|
|
|
|
|
2007-07-01 15:58:24 +00:00
|
|
|
"""Usage: gtk-builder-convert [OPTION] [INPUT] [OUTPUT]
|
|
|
|
Converts Glade files into XML files which can be loaded with GtkBuilder.
|
|
|
|
The [INPUT] file is
|
|
|
|
|
2007-07-10 05:00:24 +00:00
|
|
|
-w, --skip-windows Convert everything but GtkWindow subclasses.
|
2007-07-03 03:28:39 +00:00
|
|
|
-r, --root Convert only widget named root and its children
|
2007-07-01 15:58:24 +00:00
|
|
|
-h, --help display this help and exit
|
|
|
|
|
|
|
|
When OUTPUT is -, write to standard input.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
gtk-builder-convert preference.glade preferences.ui
|
|
|
|
|
|
|
|
Report bugs to http://bugzilla.gnome.org/."""
|
|
|
|
|
|
|
|
import getopt
|
2007-06-28 15:15:02 +00:00
|
|
|
import os
|
2007-06-26 02:42:41 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
from xml.dom import minidom, Node
|
|
|
|
|
2007-07-01 15:58:24 +00:00
|
|
|
WINDOWS = ['GtkWindow',
|
|
|
|
'GtkDialog',
|
|
|
|
'GtkFileChooserDialog',
|
|
|
|
'GtkMessageDialog']
|
|
|
|
|
2007-06-28 15:15:02 +00:00
|
|
|
# The subprocess is only available in Python 2.4+
|
|
|
|
try:
|
|
|
|
import subprocess
|
|
|
|
subprocess # pyflakes
|
|
|
|
except ImportError:
|
|
|
|
subprocess = None
|
|
|
|
|
2007-06-26 02:42:41 +00:00
|
|
|
def get_child_nodes(node):
|
2007-07-07 15:59:53 +00:00
|
|
|
assert node.tagName == 'object'
|
2007-06-26 02:42:41 +00:00
|
|
|
nodes = []
|
|
|
|
for child in node.childNodes:
|
|
|
|
if child.nodeType == Node.TEXT_NODE:
|
|
|
|
continue
|
|
|
|
if child.tagName != 'child':
|
|
|
|
continue
|
|
|
|
nodes.append(child)
|
|
|
|
return nodes
|
|
|
|
|
2007-07-08 14:00:21 +00:00
|
|
|
def get_properties(node):
|
2007-07-07 15:59:53 +00:00
|
|
|
assert node.tagName == 'object'
|
2007-06-26 02:42:41 +00:00
|
|
|
properties = {}
|
|
|
|
for child in node.childNodes:
|
|
|
|
if child.nodeType == Node.TEXT_NODE:
|
|
|
|
continue
|
|
|
|
if child.tagName != 'property':
|
|
|
|
continue
|
|
|
|
value = child.childNodes[0].data
|
|
|
|
properties[child.getAttribute('name')] = value
|
|
|
|
return properties
|
|
|
|
|
2007-07-07 15:59:53 +00:00
|
|
|
def get_property(node, property_name):
|
|
|
|
assert node.tagName == 'object'
|
2007-07-08 14:00:21 +00:00
|
|
|
properties = get_properties(node)
|
2007-07-07 15:59:53 +00:00
|
|
|
return properties.get(property_name)
|
|
|
|
|
2007-07-08 14:00:21 +00:00
|
|
|
def get_signal_nodes(node):
|
|
|
|
assert node.tagName == 'object'
|
|
|
|
signals = []
|
|
|
|
for child in node.childNodes:
|
|
|
|
if child.nodeType == Node.TEXT_NODE:
|
|
|
|
continue
|
|
|
|
if child.tagName == 'signal':
|
|
|
|
signals.append(child)
|
|
|
|
return signals
|
|
|
|
|
2007-09-04 12:19:37 +00:00
|
|
|
def get_property_nodes(node):
|
|
|
|
assert node.tagName == 'object'
|
|
|
|
properties = []
|
|
|
|
for child in node.childNodes:
|
|
|
|
if child.nodeType == Node.TEXT_NODE:
|
|
|
|
continue
|
|
|
|
if child.tagName == 'property':
|
|
|
|
properties.append(child)
|
|
|
|
return properties
|
|
|
|
|
2007-07-09 13:40:10 +00:00
|
|
|
def get_accelerator_nodes(node):
|
|
|
|
assert node.tagName == 'object'
|
|
|
|
accelerators = []
|
|
|
|
for child in node.childNodes:
|
|
|
|
if child.nodeType == Node.TEXT_NODE:
|
|
|
|
continue
|
|
|
|
if child.tagName == 'accelerator':
|
|
|
|
accelerators.append(child)
|
|
|
|
return accelerators
|
|
|
|
|
2007-06-26 02:42:41 +00:00
|
|
|
def get_object_node(child_node):
|
|
|
|
assert child_node.tagName == 'child'
|
|
|
|
nodes = []
|
|
|
|
for node in child_node.childNodes:
|
|
|
|
if node.nodeType == Node.TEXT_NODE:
|
|
|
|
continue
|
|
|
|
if node.tagName == 'object':
|
|
|
|
nodes.append(node)
|
|
|
|
assert len(nodes) == 1, nodes
|
|
|
|
return nodes[0]
|
|
|
|
|
2007-07-08 14:33:20 +00:00
|
|
|
def copy_properties(node, props, prop_dict):
|
|
|
|
assert node.tagName == 'object'
|
|
|
|
for prop_name in props:
|
|
|
|
value = get_property(node, prop_name)
|
|
|
|
if value is not None:
|
|
|
|
prop_dict[prop_name] = value
|
2007-07-07 15:59:53 +00:00
|
|
|
|
2007-06-26 02:42:41 +00:00
|
|
|
class GtkBuilderConverter(object):
|
|
|
|
|
2007-07-03 03:28:39 +00:00
|
|
|
def __init__(self, skip_windows, root):
|
2007-07-01 15:58:24 +00:00
|
|
|
self.skip_windows = skip_windows
|
2007-07-03 03:28:39 +00:00
|
|
|
self.root = root
|
2007-07-07 15:44:30 +00:00
|
|
|
self.root_objects = []
|
|
|
|
self.objects = {}
|
2007-07-01 15:58:24 +00:00
|
|
|
|
2007-06-26 02:42:41 +00:00
|
|
|
#
|
|
|
|
# Public API
|
|
|
|
#
|
|
|
|
|
|
|
|
def parse_file(self, file):
|
|
|
|
self._dom = minidom.parse(file)
|
|
|
|
self._parse()
|
|
|
|
|
|
|
|
def parse_buffer(self, buffer):
|
|
|
|
self._dom = minidom.parseString(buffer)
|
|
|
|
self._parse()
|
|
|
|
|
|
|
|
def to_xml(self):
|
2007-06-26 15:21:23 +00:00
|
|
|
xml = self._dom.toprettyxml("", "")
|
|
|
|
return xml.encode('utf-8')
|
2007-06-26 02:42:41 +00:00
|
|
|
|
|
|
|
#
|
|
|
|
# Private
|
|
|
|
#
|
|
|
|
|
2007-07-07 15:44:30 +00:00
|
|
|
def _get_object(self, name):
|
|
|
|
return self.objects.get(name)
|
|
|
|
|
|
|
|
def _get_objects_by_attr(self, attribute, value):
|
2007-06-26 02:42:41 +00:00
|
|
|
return [w for w in self._dom.getElementsByTagName("object")
|
|
|
|
if w.getAttribute(attribute) == value]
|
|
|
|
|
2007-07-04 03:44:38 +00:00
|
|
|
def _create_object(self, obj_class, obj_id, template=None, **properties):
|
|
|
|
if template is not None:
|
|
|
|
count = 1
|
|
|
|
while True:
|
|
|
|
obj_id = template + str(count)
|
2007-07-07 15:44:30 +00:00
|
|
|
widget = self._get_object(obj_id)
|
2007-07-04 03:44:38 +00:00
|
|
|
if widget is None:
|
|
|
|
break
|
|
|
|
|
|
|
|
count += 1
|
|
|
|
|
2007-06-26 02:42:41 +00:00
|
|
|
obj = self._dom.createElement('object')
|
|
|
|
obj.setAttribute('class', obj_class)
|
|
|
|
obj.setAttribute('id', obj_id)
|
|
|
|
for name, value in properties.items():
|
|
|
|
prop = self._dom.createElement('property')
|
|
|
|
prop.setAttribute('name', name)
|
|
|
|
prop.appendChild(self._dom.createTextNode(value))
|
|
|
|
obj.appendChild(prop)
|
2007-07-07 15:44:30 +00:00
|
|
|
self.objects[obj_id] = obj
|
|
|
|
return obj
|
|
|
|
|
|
|
|
def _create_root_object(self, obj_class, template, **properties):
|
|
|
|
obj = self._create_object(obj_class, None, template, **properties)
|
|
|
|
self.root_objects.append(obj)
|
2007-06-26 02:42:41 +00:00
|
|
|
return obj
|
|
|
|
|
|
|
|
def _parse(self):
|
|
|
|
glade_iface = self._dom.getElementsByTagName("glade-interface")
|
|
|
|
assert glade_iface, ("Badly formed XML, there is "
|
|
|
|
"no <glade-interface> tag.")
|
2007-07-07 15:50:09 +00:00
|
|
|
# Rename glade-interface to interface
|
2007-06-26 02:42:41 +00:00
|
|
|
glade_iface[0].tagName = 'interface'
|
|
|
|
self._interface = glade_iface[0]
|
|
|
|
|
2007-06-27 23:36:47 +00:00
|
|
|
# Remove glade-interface doc type
|
|
|
|
for node in self._dom.childNodes:
|
|
|
|
if node.nodeType == Node.DOCUMENT_TYPE_NODE:
|
|
|
|
if node.name == 'glade-interface':
|
|
|
|
self._dom.removeChild(node)
|
|
|
|
|
2007-07-07 15:50:09 +00:00
|
|
|
# Strip unsupported tags
|
2007-07-08 03:22:18 +00:00
|
|
|
for tag in ['requires']:
|
2007-07-07 15:50:09 +00:00
|
|
|
for child in self._dom.getElementsByTagName(tag):
|
|
|
|
child.parentNode.removeChild(child)
|
2007-06-26 02:42:41 +00:00
|
|
|
|
2007-07-03 03:28:39 +00:00
|
|
|
if self.root:
|
|
|
|
self._strip_root(self.root)
|
|
|
|
|
2007-07-07 15:50:09 +00:00
|
|
|
# Rename widget to object
|
2007-07-07 15:44:30 +00:00
|
|
|
objects = self._dom.getElementsByTagName("widget")
|
|
|
|
for node in objects:
|
|
|
|
node.tagName = "object"
|
|
|
|
|
|
|
|
for node in objects:
|
2007-06-26 02:42:41 +00:00
|
|
|
self._convert(node.getAttribute("class"), node)
|
2007-07-07 15:44:30 +00:00
|
|
|
self.objects[node.getAttribute('id')] = node
|
2007-06-26 02:42:41 +00:00
|
|
|
|
|
|
|
# Convert Gazpachos UI tag
|
|
|
|
for node in self._dom.getElementsByTagName("ui"):
|
|
|
|
self._convert_ui(node)
|
|
|
|
|
2007-07-07 15:44:30 +00:00
|
|
|
# Output the newly created root objects and sort them
|
|
|
|
# by attribute id
|
|
|
|
for obj in sorted(self.root_objects,
|
|
|
|
key=lambda n: n.getAttribute('id'),
|
|
|
|
reverse=True):
|
|
|
|
self._interface.childNodes.insert(0, obj)
|
|
|
|
|
2007-06-26 02:42:41 +00:00
|
|
|
def _convert(self, klass, node):
|
|
|
|
if klass == 'GtkNotebook':
|
|
|
|
self._packing_prop_to_child_attr(node, "type", "tab")
|
|
|
|
elif klass in ['GtkExpander', 'GtkFrame']:
|
|
|
|
self._packing_prop_to_child_attr(
|
|
|
|
node, "type", "label_item", "label")
|
|
|
|
elif klass == "GtkMenuBar":
|
2007-07-08 14:00:21 +00:00
|
|
|
self._convert_menu(node)
|
2007-12-20 23:44:18 +00:00
|
|
|
elif klass == "GtkMenu":
|
|
|
|
# Only convert toplevel popups
|
|
|
|
if node.parentNode == self._interface:
|
|
|
|
self._convert_menu(node, popup=True)
|
2007-07-01 15:58:24 +00:00
|
|
|
elif klass in WINDOWS and self.skip_windows:
|
|
|
|
self._remove_window(node)
|
2007-06-26 02:42:41 +00:00
|
|
|
self._default_widget_converter(node)
|
|
|
|
|
|
|
|
def _default_widget_converter(self, node):
|
|
|
|
klass = node.getAttribute("class")
|
2007-09-04 12:19:37 +00:00
|
|
|
for prop in get_property_nodes(node):
|
2007-06-26 02:42:41 +00:00
|
|
|
prop_name = prop.getAttribute("name")
|
|
|
|
if prop_name == "sizegroup":
|
|
|
|
self._convert_sizegroup(node, prop)
|
|
|
|
elif prop_name == "tooltip" and klass != "GtkAction":
|
|
|
|
prop.setAttribute("name", "tooltip-text")
|
|
|
|
elif prop_name in ["response_id", 'response-id']:
|
2007-07-01 15:58:24 +00:00
|
|
|
# It does not make sense to convert responses when
|
|
|
|
# we're not going to output dialogs
|
|
|
|
if self.skip_windows:
|
|
|
|
continue
|
2007-06-26 02:42:41 +00:00
|
|
|
object_id = node.getAttribute('id')
|
|
|
|
response = prop.childNodes[0].data
|
|
|
|
self._convert_dialog_response(node, object_id, response)
|
|
|
|
prop.parentNode.removeChild(prop)
|
2007-07-03 03:28:39 +00:00
|
|
|
elif prop_name == "adjustment":
|
|
|
|
self._convert_adjustment(prop)
|
2007-07-04 03:44:38 +00:00
|
|
|
elif prop_name == "items" and klass in ['GtkComboBox',
|
|
|
|
'GtkComboBoxEntry']:
|
|
|
|
self._convert_combobox_items(node, prop)
|
2007-07-07 15:11:52 +00:00
|
|
|
elif prop_name == "text" and klass == 'GtkTextView':
|
|
|
|
self._convert_textview_text(prop)
|
2007-06-26 02:42:41 +00:00
|
|
|
|
2007-07-01 15:58:24 +00:00
|
|
|
def _remove_window(self, node):
|
|
|
|
object_node = get_object_node(get_child_nodes(node)[0])
|
|
|
|
parent = node.parentNode
|
|
|
|
parent.removeChild(node)
|
|
|
|
parent.appendChild(object_node)
|
|
|
|
|
2007-12-20 23:44:18 +00:00
|
|
|
def _convert_menu(self, node, popup=False):
|
2007-07-08 14:00:21 +00:00
|
|
|
if node.hasAttribute('constructor'):
|
|
|
|
return
|
|
|
|
|
|
|
|
uimgr = self._create_root_object('GtkUIManager',
|
|
|
|
template='uimanager')
|
|
|
|
|
2007-12-20 23:44:18 +00:00
|
|
|
if popup:
|
|
|
|
name = 'popup'
|
|
|
|
else:
|
|
|
|
name = 'menubar'
|
2007-06-26 02:42:41 +00:00
|
|
|
|
2007-12-20 23:44:18 +00:00
|
|
|
menu = self._dom.createElement(name)
|
2007-07-08 14:00:21 +00:00
|
|
|
menu.setAttribute('name', node.getAttribute('id'))
|
|
|
|
node.setAttribute('constructor', uimgr.getAttribute('id'))
|
|
|
|
|
|
|
|
for child in get_child_nodes(node):
|
|
|
|
obj_node = get_object_node(child)
|
|
|
|
item = self._convert_menuitem(uimgr, obj_node)
|
|
|
|
menu.appendChild(item)
|
|
|
|
child.removeChild(obj_node)
|
|
|
|
child.parentNode.removeChild(child)
|
|
|
|
|
|
|
|
ui = self._dom.createElement('ui')
|
|
|
|
uimgr.appendChild(ui)
|
|
|
|
|
|
|
|
ui.appendChild(menu)
|
|
|
|
|
|
|
|
def _convert_menuitem(self, uimgr, obj_node):
|
2007-06-26 02:42:41 +00:00
|
|
|
children = get_child_nodes(obj_node)
|
|
|
|
name = 'menuitem'
|
|
|
|
if children:
|
|
|
|
child_node = children[0]
|
|
|
|
menu_node = get_object_node(child_node)
|
|
|
|
# Can be GtkImage, which will take care of later.
|
|
|
|
if menu_node.getAttribute('class') == 'GtkMenu':
|
|
|
|
name = 'menu'
|
|
|
|
|
|
|
|
object_class = obj_node.getAttribute('class')
|
2007-07-07 16:14:30 +00:00
|
|
|
if object_class in ['GtkMenuItem',
|
|
|
|
'GtkImageMenuItem',
|
2007-07-08 14:33:20 +00:00
|
|
|
'GtkCheckMenuItem',
|
|
|
|
'GtkRadioMenuItem']:
|
2007-06-26 02:42:41 +00:00
|
|
|
menu = self._dom.createElement(name)
|
|
|
|
elif object_class == 'GtkSeparatorMenuItem':
|
2007-07-08 14:00:21 +00:00
|
|
|
return self._dom.createElement('separator')
|
2007-06-26 02:42:41 +00:00
|
|
|
else:
|
|
|
|
raise NotImplementedError(object_class)
|
2007-07-07 16:14:30 +00:00
|
|
|
|
2007-06-26 02:42:41 +00:00
|
|
|
menu.setAttribute('action', obj_node.getAttribute('id'))
|
|
|
|
self._add_action_from_menuitem(uimgr, obj_node)
|
|
|
|
if children:
|
|
|
|
for child in get_child_nodes(menu_node):
|
|
|
|
obj_node = get_object_node(child)
|
2007-07-08 14:00:21 +00:00
|
|
|
item = self._convert_menuitem(uimgr, obj_node)
|
|
|
|
menu.appendChild(item)
|
2007-06-26 02:42:41 +00:00
|
|
|
child.removeChild(obj_node)
|
|
|
|
child.parentNode.removeChild(child)
|
2007-07-08 14:00:21 +00:00
|
|
|
return menu
|
2007-06-26 02:42:41 +00:00
|
|
|
|
2007-07-08 14:33:20 +00:00
|
|
|
def _menuitem_to_action(self, node, properties):
|
|
|
|
copy_properties(node, ['label'], properties)
|
|
|
|
|
|
|
|
def _togglemenuitem_to_action(self, node, properties):
|
|
|
|
self._menuitem_to_action(node, properties)
|
|
|
|
copy_properties(node, ['active'], properties)
|
|
|
|
|
|
|
|
def _radiomenuitem_to_action(self, node, properties):
|
|
|
|
self._togglemenuitem_to_action(node, properties)
|
|
|
|
copy_properties(node, ['group'], properties)
|
|
|
|
|
2007-06-26 02:42:41 +00:00
|
|
|
def _add_action_from_menuitem(self, uimgr, node):
|
|
|
|
properties = {}
|
|
|
|
object_class = node.getAttribute('class')
|
|
|
|
object_id = node.getAttribute('id')
|
2007-07-08 14:33:20 +00:00
|
|
|
if object_class == 'GtkMenuItem':
|
|
|
|
name = 'GtkAction'
|
|
|
|
self._menuitem_to_action(node, properties)
|
|
|
|
elif object_class == 'GtkCheckMenuItem':
|
|
|
|
name = 'GtkToggleAction'
|
|
|
|
self._togglemenuitem_to_action(node, properties)
|
|
|
|
elif object_class == 'GtkRadioMenuItem':
|
|
|
|
name = 'GtkRadioAction'
|
|
|
|
self._radiomenuitem_to_action(node, properties)
|
|
|
|
elif object_class == 'GtkImageMenuItem':
|
2007-06-26 02:42:41 +00:00
|
|
|
name = 'GtkAction'
|
|
|
|
children = get_child_nodes(node)
|
|
|
|
if (children and
|
|
|
|
children[0].getAttribute('internal-child') == 'image'):
|
|
|
|
image = get_object_node(children[0])
|
2007-07-08 14:00:21 +00:00
|
|
|
stock_id = get_property(image, 'stock')
|
2007-07-07 16:18:38 +00:00
|
|
|
if stock_id is not None:
|
|
|
|
properties['stock_id'] = stock_id
|
2007-06-26 02:42:41 +00:00
|
|
|
elif object_class == 'GtkSeparatorMenuItem':
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
raise NotImplementedError(object_class)
|
|
|
|
|
2007-07-07 15:59:53 +00:00
|
|
|
if get_property(node, 'use_stock') == 'True':
|
2007-07-07 16:18:38 +00:00
|
|
|
stock_id = get_property(node, 'label')
|
|
|
|
if stock_id is not None:
|
|
|
|
properties['stock_id'] = stock_id
|
|
|
|
|
2007-06-26 02:42:41 +00:00
|
|
|
properties['name'] = object_id
|
|
|
|
action = self._create_object(name,
|
|
|
|
object_id,
|
|
|
|
**properties)
|
2007-07-08 14:00:21 +00:00
|
|
|
|
|
|
|
for signal in get_signal_nodes(node):
|
|
|
|
signal_name = signal.getAttribute('name')
|
|
|
|
if signal_name == 'activate':
|
|
|
|
action.appendChild(signal)
|
|
|
|
else:
|
|
|
|
print 'Unhandled signal %s::%s' % (node.getAttribute('class'),
|
|
|
|
signal_name)
|
|
|
|
|
2007-06-26 02:42:41 +00:00
|
|
|
if not uimgr.childNodes:
|
|
|
|
child = self._dom.createElement('child')
|
|
|
|
uimgr.appendChild(child)
|
|
|
|
|
2007-07-07 15:44:30 +00:00
|
|
|
group = self._create_object('GtkActionGroup', None,
|
|
|
|
template='actiongroup')
|
2007-06-26 02:42:41 +00:00
|
|
|
child.appendChild(group)
|
|
|
|
else:
|
|
|
|
group = uimgr.childNodes[0].childNodes[0]
|
|
|
|
|
|
|
|
child = self._dom.createElement('child')
|
|
|
|
group.appendChild(child)
|
|
|
|
child.appendChild(action)
|
|
|
|
|
2007-07-09 13:40:10 +00:00
|
|
|
for accelerator in get_accelerator_nodes(node):
|
2007-10-13 18:07:32 +00:00
|
|
|
signal_name = accelerator.getAttribute('signal')
|
2007-07-09 13:40:10 +00:00
|
|
|
if signal_name != 'activate':
|
|
|
|
print 'Unhandled accelerator signal for %s::%s' % (
|
|
|
|
node.getAttribute('class'), signal_name)
|
|
|
|
continue
|
|
|
|
accelerator.removeAttribute('signal')
|
|
|
|
child.appendChild(accelerator)
|
|
|
|
|
2007-06-26 02:42:41 +00:00
|
|
|
def _convert_sizegroup(self, node, prop):
|
|
|
|
# This is Gazpacho only
|
|
|
|
node.removeChild(prop)
|
2007-07-07 15:44:30 +00:00
|
|
|
obj = self._get_object(prop.childNodes[0].data)
|
2007-06-26 02:42:41 +00:00
|
|
|
if obj is None:
|
2007-07-07 15:44:30 +00:00
|
|
|
widgets = self._get_objects_by_attr("class", "GtkSizeGroup")
|
2007-06-26 02:42:41 +00:00
|
|
|
if widgets:
|
|
|
|
obj = widgets[-1]
|
|
|
|
else:
|
2007-07-07 15:44:30 +00:00
|
|
|
obj = self._create_root_object('GtkSizeGroup',
|
|
|
|
template='sizegroup')
|
2007-06-26 02:42:41 +00:00
|
|
|
|
|
|
|
widgets = obj.getElementsByTagName("widgets")
|
|
|
|
if widgets:
|
|
|
|
assert len(widgets) == 1
|
|
|
|
widgets = widgets[0]
|
|
|
|
else:
|
|
|
|
widgets = self._dom.createElement("widgets")
|
|
|
|
obj.appendChild(widgets)
|
|
|
|
|
|
|
|
member = self._dom.createElement("widget")
|
|
|
|
member.setAttribute("name", node.getAttribute("id"))
|
|
|
|
widgets.appendChild(member)
|
|
|
|
|
|
|
|
def _convert_dialog_response(self, node, object_name, response):
|
|
|
|
# 1) Get parent dialog node
|
|
|
|
while True:
|
2007-10-24 11:52:56 +00:00
|
|
|
# If we can't find the parent dialog, give up
|
|
|
|
if node == self._dom:
|
|
|
|
return
|
|
|
|
|
2007-06-26 02:42:41 +00:00
|
|
|
if (node.tagName == 'object' and
|
|
|
|
node.getAttribute('class') == 'GtkDialog'):
|
|
|
|
dialog = node
|
|
|
|
break
|
|
|
|
node = node.parentNode
|
|
|
|
assert node
|
|
|
|
|
|
|
|
# 2) Get dialogs action-widgets tag, create if not found
|
|
|
|
for child in dialog.childNodes:
|
|
|
|
if child.nodeType == Node.TEXT_NODE:
|
|
|
|
continue
|
|
|
|
if child.tagName == 'action-widgets':
|
|
|
|
actions = child
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
actions = self._dom.createElement("action-widgets")
|
|
|
|
dialog.appendChild(actions)
|
|
|
|
|
|
|
|
# 3) Add action-widget tag for the response
|
|
|
|
action = self._dom.createElement("action-widget")
|
|
|
|
action.setAttribute("response", response)
|
|
|
|
action.appendChild(self._dom.createTextNode(object_name))
|
|
|
|
actions.appendChild(action)
|
|
|
|
|
2007-07-03 03:28:39 +00:00
|
|
|
def _convert_adjustment(self, prop):
|
|
|
|
data = prop.childNodes[0].data
|
|
|
|
value, lower, upper, step, page, page_size = data.split(' ')
|
2007-07-08 14:00:21 +00:00
|
|
|
adj = self._create_root_object("GtkAdjustment",
|
2007-07-07 15:44:30 +00:00
|
|
|
template='adjustment',
|
|
|
|
value=value,
|
|
|
|
lower=lower,
|
|
|
|
upper=upper,
|
|
|
|
step_increment=step,
|
|
|
|
page_increment=page,
|
|
|
|
page_size=page_size)
|
2007-07-04 03:44:38 +00:00
|
|
|
prop.childNodes[0].data = adj.getAttribute('id')
|
2007-07-03 03:28:39 +00:00
|
|
|
|
2007-07-04 03:44:38 +00:00
|
|
|
def _convert_combobox_items(self, node, prop):
|
|
|
|
if not prop.childNodes:
|
|
|
|
return
|
|
|
|
value = prop.childNodes[0].data
|
2007-07-07 15:44:30 +00:00
|
|
|
model = self._create_root_object("GtkListStore",
|
|
|
|
template="model")
|
2007-07-04 03:44:38 +00:00
|
|
|
|
|
|
|
columns = self._dom.createElement('columns')
|
|
|
|
model.appendChild(columns)
|
|
|
|
|
|
|
|
column = self._dom.createElement('column')
|
|
|
|
column.setAttribute('type', 'gchararray')
|
|
|
|
columns.appendChild(column)
|
|
|
|
|
|
|
|
data = self._dom.createElement('data')
|
|
|
|
model.appendChild(data)
|
|
|
|
|
|
|
|
for item in value.split('\n'):
|
|
|
|
row = self._dom.createElement('row')
|
|
|
|
data.appendChild(row)
|
|
|
|
|
|
|
|
col = self._dom.createElement('col')
|
|
|
|
col.setAttribute('id', '0')
|
|
|
|
col.appendChild(self._dom.createTextNode(item))
|
|
|
|
row.appendChild(col)
|
|
|
|
|
|
|
|
parent = prop.parentNode
|
|
|
|
model_prop = self._dom.createElement('property')
|
|
|
|
model_prop.setAttribute('name', 'model')
|
|
|
|
model_prop.appendChild(
|
|
|
|
self._dom.createTextNode(model.getAttribute('id')))
|
|
|
|
parent.appendChild(model_prop)
|
|
|
|
|
|
|
|
parent.removeChild(prop)
|
|
|
|
|
|
|
|
child = self._dom.createElement('child')
|
|
|
|
node.appendChild(child)
|
|
|
|
cell_renderer = self._create_object('GtkCellRendererText', None,
|
|
|
|
template='renderer')
|
|
|
|
child.appendChild(cell_renderer)
|
|
|
|
|
|
|
|
attributes = self._dom.createElement('attributes')
|
|
|
|
child.appendChild(attributes)
|
|
|
|
|
|
|
|
attribute = self._dom.createElement('attribute')
|
|
|
|
attributes.appendChild(attribute)
|
|
|
|
attribute.setAttribute('name', 'text')
|
|
|
|
attribute.appendChild(self._dom.createTextNode('0'))
|
|
|
|
|
2007-07-07 15:11:52 +00:00
|
|
|
def _convert_textview_text(self, prop):
|
2007-07-07 15:44:30 +00:00
|
|
|
if not prop.childNodes:
|
|
|
|
prop.parentNode.removeChild(prop)
|
|
|
|
return
|
|
|
|
|
2007-07-07 15:11:52 +00:00
|
|
|
data = prop.childNodes[0].data
|
|
|
|
if prop.hasAttribute('translatable'):
|
|
|
|
prop.removeAttribute('translatable')
|
2007-07-07 15:44:30 +00:00
|
|
|
tbuffer = self._create_root_object("GtkTextBuffer",
|
|
|
|
template='textbuffer',
|
|
|
|
text=data)
|
2007-07-07 15:11:52 +00:00
|
|
|
prop.childNodes[0].data = tbuffer.getAttribute('id')
|
|
|
|
|
2007-06-26 02:42:41 +00:00
|
|
|
def _packing_prop_to_child_attr(self, node, prop_name, prop_val,
|
|
|
|
attr_val=None):
|
2007-09-04 12:19:37 +00:00
|
|
|
for child in get_child_nodes(node):
|
2007-06-26 02:42:41 +00:00
|
|
|
packing_props = [p for p in child.childNodes if p.nodeName == "packing"]
|
|
|
|
if not packing_props:
|
|
|
|
continue
|
|
|
|
assert len(packing_props) == 1
|
|
|
|
packing_prop = packing_props[0]
|
|
|
|
properties = packing_prop.getElementsByTagName("property")
|
|
|
|
for prop in properties:
|
|
|
|
if (prop.getAttribute("name") != prop_name or
|
|
|
|
prop.childNodes[0].data != prop_val):
|
|
|
|
continue
|
|
|
|
packing_prop.removeChild(prop)
|
|
|
|
child.setAttribute(prop_name, attr_val or prop_val)
|
|
|
|
if len(properties) == 1:
|
|
|
|
child.removeChild(packing_prop)
|
|
|
|
|
|
|
|
def _convert_ui(self, node):
|
|
|
|
cdata = node.childNodes[0]
|
|
|
|
data = cdata.toxml().strip()
|
|
|
|
if not data.startswith("<![CDATA[") or not data.endswith("]]>"):
|
|
|
|
return
|
|
|
|
data = data[9:-3]
|
|
|
|
child = minidom.parseString(data).childNodes[0]
|
|
|
|
nodes = child.childNodes[:]
|
|
|
|
for child_node in nodes:
|
|
|
|
node.appendChild(child_node)
|
|
|
|
node.removeChild(cdata)
|
|
|
|
if not node.hasAttribute("id"):
|
|
|
|
return
|
|
|
|
|
|
|
|
# Updating references made by widgets
|
|
|
|
parent_id = node.parentNode.getAttribute("id")
|
2007-07-07 15:44:30 +00:00
|
|
|
for widget in self._get_objects_by_attr("constructor",
|
2007-06-26 02:42:41 +00:00
|
|
|
node.getAttribute("id")):
|
|
|
|
widget.getAttributeNode("constructor").value = parent_id
|
|
|
|
node.removeAttribute("id")
|
|
|
|
|
2007-07-03 03:28:39 +00:00
|
|
|
def _strip_root(self, root_name):
|
2007-07-07 15:44:30 +00:00
|
|
|
for widget in self._dom.getElementsByTagName("widget"):
|
|
|
|
if widget.getAttribute('id') == root_name:
|
|
|
|
break
|
|
|
|
else:
|
2007-07-03 03:28:39 +00:00
|
|
|
raise SystemExit("Could not find an object called `%s'" % (
|
|
|
|
root_name))
|
|
|
|
|
|
|
|
for child in self._interface.childNodes[:]:
|
|
|
|
if child.nodeType != Node.ELEMENT_NODE:
|
|
|
|
continue
|
|
|
|
child.parentNode.removeChild(child)
|
|
|
|
|
|
|
|
self._interface.appendChild(widget)
|
|
|
|
|
|
|
|
|
2007-06-28 15:15:02 +00:00
|
|
|
def _indent(output):
|
|
|
|
if not subprocess:
|
|
|
|
return output
|
|
|
|
|
|
|
|
for directory in os.environ['PATH'].split(os.pathsep):
|
|
|
|
filename = os.path.join(directory, 'xmllint')
|
|
|
|
if os.path.exists(filename):
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
return output
|
|
|
|
|
|
|
|
s = subprocess.Popen([filename, '--format', '-'],
|
|
|
|
stdin=subprocess.PIPE,
|
|
|
|
stdout=subprocess.PIPE)
|
|
|
|
s.stdin.write(output)
|
|
|
|
s.stdin.close()
|
|
|
|
return s.stdout.read()
|
|
|
|
|
2007-07-01 15:58:24 +00:00
|
|
|
def usage():
|
|
|
|
print __doc__
|
|
|
|
|
|
|
|
def main(args):
|
|
|
|
try:
|
2007-07-03 03:28:39 +00:00
|
|
|
opts, args = getopt.getopt(args[1:], "hwr:",
|
|
|
|
["help", "skip-windows", "root="])
|
2007-07-01 15:58:24 +00:00
|
|
|
except getopt.GetoptError:
|
|
|
|
usage()
|
|
|
|
return 2
|
|
|
|
|
|
|
|
if len(args) != 2:
|
|
|
|
usage()
|
|
|
|
return 2
|
|
|
|
|
|
|
|
input_filename, output_filename = args
|
|
|
|
|
|
|
|
skip_windows = False
|
|
|
|
split = False
|
2007-07-03 03:28:39 +00:00
|
|
|
root = None
|
2007-07-01 15:58:24 +00:00
|
|
|
for o, a in opts:
|
|
|
|
if o in ("-h", "--help"):
|
|
|
|
usage()
|
|
|
|
sys.exit()
|
2007-07-03 03:28:39 +00:00
|
|
|
elif o in ("-r", "--root"):
|
|
|
|
root = a
|
2007-07-01 15:58:24 +00:00
|
|
|
elif o in ("-w", "--skip-windows"):
|
|
|
|
skip_windows = True
|
|
|
|
|
2007-07-03 03:28:39 +00:00
|
|
|
conv = GtkBuilderConverter(skip_windows=skip_windows,
|
|
|
|
root=root)
|
2007-07-01 15:58:24 +00:00
|
|
|
conv.parse_file(input_filename)
|
|
|
|
|
|
|
|
xml = _indent(conv.to_xml())
|
|
|
|
if output_filename == "-":
|
|
|
|
print xml
|
|
|
|
else:
|
|
|
|
open(output_filename, 'w').write(xml)
|
|
|
|
print "Wrote", output_filename
|
2007-06-26 02:42:41 +00:00
|
|
|
|
2007-07-01 15:58:24 +00:00
|
|
|
return 0
|
2007-06-26 02:42:41 +00:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2007-07-01 15:58:24 +00:00
|
|
|
sys.exit(main(sys.argv))
|