Convert to getopt, improved documentation, change the script to require

2007-07-01  Johan Dahlin  <jdahlin@async.com.br>

    * gtk/gtk-builder-convert (GtkBuilderConverter): 
    Convert to getopt, improved documentation, change
    the script to require two arguments


svn path=/trunk/; revision=18325
This commit is contained in:
Johan Dahlin 2007-07-01 15:58:24 +00:00 committed by Johan Dahlin
parent abd27f39d2
commit 0c993121b1
2 changed files with 76 additions and 10 deletions

View File

@ -1,3 +1,9 @@
2007-07-01 Johan Dahlin <jdahlin@async.com.br>
* gtk/gtk-builder-convert (GtkBuilderConverter):
Convert to getopt, improved documentation, change
the script to require two arguments
2007-06-30 Richard Hult <richard@imendio.com>
Continue the event handling cleanup:

80
gtk/gtk-builder-convert Normal file → Executable file
View File

@ -18,18 +18,36 @@
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# This is a script that can convert libglade files to the new gtkbuilder format
#
# TODO:
# GtkComboBox.items -> GtkListStore
# GtkTextView.text -> GtkTextBuffer
# Toolbars
"""Usage: gtk-builder-convert [OPTION] [INPUT] [OUTPUT]
Converts Glade files into XML files which can be loaded with GtkBuilder.
The [INPUT] file is
-w, --skip-windows Convert everything bug GtkWindow subclasses.
-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
import os
import sys
from xml.dom import minidom, Node
WINDOWS = ['GtkWindow',
'GtkDialog',
'GtkFileChooserDialog',
'GtkMessageDialog']
# The subprocess is only available in Python 2.4+
try:
import subprocess
@ -71,6 +89,9 @@ def get_object_node(child_node):
class GtkBuilderConverter(object):
def __init__(self, skip_windows):
self.skip_windows = skip_windows
#
# Public API
#
@ -166,6 +187,8 @@ class GtkBuilderConverter(object):
uimgr.setAttribute('id', 'uimanager1')
self._interface.childNodes.insert(0, uimgr)
self._convert_menubar(uimgr, node)
elif klass in WINDOWS and self.skip_windows:
self._remove_window(node)
self._default_widget_converter(node)
@ -180,11 +203,21 @@ class GtkBuilderConverter(object):
elif prop_name == "tooltip" and klass != "GtkAction":
prop.setAttribute("name", "tooltip-text")
elif prop_name in ["response_id", 'response-id']:
# It does not make sense to convert responses when
# we're not going to output dialogs
if self.skip_windows:
continue
object_id = node.getAttribute('id')
response = prop.childNodes[0].data
self._convert_dialog_response(node, object_id, response)
prop.parentNode.removeChild(prop)
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)
def _convert_menubar(self, uimgr, node):
menubar = self._dom.createElement('menubar')
menubar.setAttribute('name', node.getAttribute('id'))
@ -380,16 +413,43 @@ def _indent(output):
s.stdin.close()
return s.stdout.read()
def usage():
print __doc__
def main():
if len(sys.argv) < 2:
raise SystemExit("Usage: %s filename.glade" % (sys.argv[0],))
def main(args):
try:
opts, args = getopt.getopt(args[1:], "hw",
["help", "skip-windows"])
except getopt.GetoptError:
usage()
return 2
conv = GtkBuilderConverter()
conv.parse_file(sys.argv[1])
if len(args) != 2:
usage()
return 2
xml = conv.to_xml()
print _indent(xml)
input_filename, output_filename = args
skip_windows = False
split = False
for o, a in opts:
if o in ("-h", "--help"):
usage()
sys.exit()
elif o in ("-w", "--skip-windows"):
skip_windows = True
conv = GtkBuilderConverter(skip_windows=skip_windows)
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
return 0
if __name__ == "__main__":
main()
sys.exit(main(sys.argv))