2004-10-06 14:32:25 -04:00
|
|
|
#----------------------------------------------------------------------
|
|
|
|
# Name: wx.tools.pywxrc
|
|
|
|
# Purpose: XML resource compiler
|
|
|
|
#
|
|
|
|
# Author: Robin Dunn
|
|
|
|
# Based on wxrc.cpp by Vaclav Slavik, Eduardo Marques
|
|
|
|
# Ported to Python in order to not require yet another
|
|
|
|
# binary in wxPython distributions
|
|
|
|
#
|
2006-04-08 02:17:11 -04:00
|
|
|
# Massive rework by Eli Golovinsky
|
|
|
|
#
|
2004-10-06 14:32:25 -04:00
|
|
|
# RCS-ID: $Id$
|
|
|
|
# Copyright: (c) 2004 by Total Control Software, 2000 Vaclav Slavik
|
|
|
|
# Licence: wxWindows license
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
|
|
|
"""
|
2006-04-08 02:17:11 -04:00
|
|
|
pywxrc -- Python XML resource compiler
|
2006-05-02 21:47:57 -04:00
|
|
|
(see http://wiki.wxpython.org/index.cgi/pywxrc for more info)
|
2006-04-08 02:17:11 -04:00
|
|
|
|
|
|
|
Usage: python pywxrc.py -h
|
2006-05-24 00:49:07 -04:00
|
|
|
python pywxrc.py [-p] [-g] [-e] [-o filename] xrc input files...
|
2006-04-08 02:17:11 -04:00
|
|
|
|
2006-05-24 00:49:07 -04:00
|
|
|
-h, --help show help message
|
|
|
|
-p, --python generate python module
|
|
|
|
-g, --gettext output list of translatable strings (may be combined with -p)
|
|
|
|
-e, --embed embed XRC resources in the output file
|
|
|
|
-o, --output output filename, or - for stdout
|
2004-10-06 14:32:25 -04:00
|
|
|
"""
|
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
import sys, os, getopt, glob, re
|
|
|
|
import xml.dom.minidom as minidom
|
2004-10-06 14:32:25 -04:00
|
|
|
import wx
|
|
|
|
import wx.xrc
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
class PythonTemplates:
|
|
|
|
FILE_HEADER = """\
|
|
|
|
# This file was automatically generated by pywxrc, do not edit by hand.
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
import wx
|
|
|
|
import wx.xrc as xrc
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
__res = None
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
def get_resources():
|
|
|
|
\"\"\" This function provides access to the XML resources in this module.\"\"\"
|
|
|
|
global __res
|
|
|
|
if __res == None:
|
|
|
|
__init_resources()
|
|
|
|
return __res
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
"""
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
CLASS_HEADER = """\
|
2006-05-02 21:47:57 -04:00
|
|
|
class xrc%(windowName)s(wx.%(windowClass)s):
|
2006-04-08 02:17:11 -04:00
|
|
|
def PreCreate(self):
|
|
|
|
\"\"\" This function is called during the class's initialization.
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
Override it for custom setup before the window is created usually to
|
|
|
|
set additional window styles using SetWindowStyle() and SetExtraStyle().\"\"\"
|
|
|
|
pass
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
def __init__(self, parent):
|
|
|
|
# Two stage creation (see http://wiki.wxpython.org/index.cgi/TwoStageCreation)
|
|
|
|
pre = wx.Pre%(windowClass)s()
|
|
|
|
get_resources().LoadOn%(windowClass)s(pre, parent, "%(windowName)s")
|
|
|
|
self.PreCreate()
|
|
|
|
self.PostCreate(pre)
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
# Define variables for the controls
|
|
|
|
"""
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
CREATE_WIDGET_VAR = """\
|
|
|
|
self.%(widgetName)s = xrc.XRCCTRL(self, \"%(widgetName)s\")
|
|
|
|
"""
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
INIT_RESOURE_HEADER = """\
|
|
|
|
# ------------------------ Resource data ----------------------
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
def __init_resources():
|
2006-05-24 00:49:07 -04:00
|
|
|
global __res
|
|
|
|
__res = xrc.EmptyXmlResource()
|
2006-04-08 02:17:11 -04:00
|
|
|
"""
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
LOAD_RES_FILE = """\
|
2006-05-24 00:49:07 -04:00
|
|
|
__res.Load('%(resourceFilename)s')"""
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
FILE_AS_STRING = """\
|
|
|
|
%(filename)s = '''\\
|
|
|
|
%(fileData)s'''
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
"""
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
PREPARE_MEMFS = """\
|
|
|
|
wx.FileSystem.AddHandler(wx.MemoryFSHandler())
|
|
|
|
"""
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
ADD_FILE_TO_MEMFS = """\
|
|
|
|
wx.MemoryFSHandler.AddFile('XRC/%(memoryPath)s/%(filename)s', %(filename)s)
|
|
|
|
"""
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
LOAD_RES_MEMFS = """\
|
|
|
|
__res.Load('memory:XRC/%(memoryPath)s/%(resourceFilename)s')
|
|
|
|
"""
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-05-24 00:49:07 -04:00
|
|
|
GETTEXT_DUMMY_FUNC = """
|
2006-05-02 21:47:57 -04:00
|
|
|
# ----------------------- Gettext strings ---------------------
|
|
|
|
|
|
|
|
def __gettext_strings():
|
|
|
|
# This is a dummy function that lists all the strings that are used in
|
|
|
|
# the XRC file in the _("a string") format to be recognized by GNU
|
|
|
|
# gettext utilities (specificaly the xgettext utility) and the
|
|
|
|
# mki18n.py script. For more information see:
|
|
|
|
# http://wiki.wxpython.org/index.cgi/Internationalization
|
|
|
|
|
|
|
|
def _(str): pass
|
|
|
|
|
2006-05-24 00:49:07 -04:00
|
|
|
%s
|
2006-05-02 21:47:57 -04:00
|
|
|
"""
|
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
#----------------------------------------------------------------------
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
class XmlResourceCompiler:
|
|
|
|
|
|
|
|
templates = PythonTemplates()
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
"""This class generates Python code from XML resource files (XRC)."""
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-05-24 00:49:07 -04:00
|
|
|
def MakePythonModule(self, inputFiles, outputFilename,
|
2006-05-02 21:47:57 -04:00
|
|
|
embedResources=False, generateGetText=False):
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-05-24 00:49:07 -04:00
|
|
|
outputFile = self._OpenOutputFile(outputFilename)
|
|
|
|
|
|
|
|
classes = []
|
|
|
|
resources = []
|
|
|
|
gettextStrings = []
|
|
|
|
|
|
|
|
# process all the inputFiles, collecting the output data
|
|
|
|
for inFile in inputFiles:
|
|
|
|
resourceDocument = minidom.parse(inFile)
|
|
|
|
classes.append(self.GenerateClasses(resourceDocument))
|
|
|
|
|
|
|
|
if embedResources:
|
|
|
|
res = self.GenerateInitResourcesEmbedded(inFile, resourceDocument)
|
|
|
|
else:
|
|
|
|
res = self.GenerateInitResourcesFile(inFile, resourceDocument)
|
|
|
|
resources.append(res)
|
|
|
|
|
|
|
|
if generateGetText:
|
|
|
|
gettextStrings += self.FindStringsInNode(resourceDocument.firstChild)
|
|
|
|
|
|
|
|
# now write it all out
|
2006-04-08 02:17:11 -04:00
|
|
|
print >>outputFile, self.templates.FILE_HEADER
|
2006-05-24 00:49:07 -04:00
|
|
|
print >>outputFile, "\n".join(classes)
|
|
|
|
|
|
|
|
print >>outputFile, self.templates.INIT_RESOURE_HEADER
|
2006-04-08 02:17:11 -04:00
|
|
|
if embedResources:
|
2006-05-24 00:49:07 -04:00
|
|
|
print >>outputFile, self.templates.PREPARE_MEMFS
|
|
|
|
print >>outputFile, "\n".join(resources)
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-05-02 21:47:57 -04:00
|
|
|
if generateGetText:
|
2006-05-24 00:49:07 -04:00
|
|
|
gettextStrings = [' _("%s")' % s for s in gettextStrings]
|
|
|
|
gettextStrings = "\n".join(gettextStrings)
|
|
|
|
print >>outputFile, self.templates.GETTEXT_DUMMY_FUNC % gettextStrings
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
|
|
|
|
def MakeGetTextOutput(self, inputFiles, outputFilename):
|
|
|
|
"""
|
|
|
|
Just output the gettext strings by themselves, with no other
|
|
|
|
code generation.
|
|
|
|
"""
|
|
|
|
outputFile = self._OpenOutputFile(outputFilename)
|
|
|
|
for inFile in inputFiles:
|
|
|
|
resourceDocument = minidom.parse(inFile)
|
|
|
|
resource = resourceDocument.firstChild
|
|
|
|
strings = self.FindStringsInNode(resource)
|
|
|
|
strings = ['_("%s");' % s for s in strings]
|
|
|
|
print >>outputFile, "\n".join(strings)
|
2006-05-02 21:47:57 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
#-------------------------------------------------------------------
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
def GenerateClasses(self, resourceDocument):
|
|
|
|
outputList = []
|
|
|
|
|
|
|
|
resource = resourceDocument.firstChild
|
|
|
|
topWindows = [e for e in resource.childNodes
|
|
|
|
if e.nodeType == e.ELEMENT_NODE and e.tagName == "object"]
|
|
|
|
|
|
|
|
# Generate a class for each top-window object (Frame, Panel, Dialog, etc.)
|
|
|
|
for topWindow in topWindows:
|
|
|
|
windowClass = topWindow.getAttribute("class")
|
|
|
|
windowClass = re.sub("^wx", "", windowClass)
|
|
|
|
windowName = topWindow.getAttribute("name")
|
|
|
|
outputList.append(self.templates.CLASS_HEADER % locals())
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
# Generate a variable for each control, and standard event handlers
|
|
|
|
# for standard controls.
|
|
|
|
for widget in topWindow.getElementsByTagName("object"):
|
|
|
|
widgetClass = widget.getAttribute("class")
|
|
|
|
widgetClass = re.sub("^wx", "", widgetClass)
|
|
|
|
widgetName = widget.getAttribute("name")
|
|
|
|
if (widgetName != "" and widgetClass != "" and
|
|
|
|
widgetClass not in
|
|
|
|
['tool', 'unknown', 'notebookpage',
|
|
|
|
'separator', 'sizeritem', 'MenuItem']):
|
|
|
|
outputList.append(self.templates.CREATE_WIDGET_VAR % locals())
|
|
|
|
outputList.append('\n\n')
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
return "".join(outputList)
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
#-------------------------------------------------------------------
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
def GenerateInitResourcesEmbedded(self, resourceFilename, resourceDocument):
|
|
|
|
outputList = []
|
|
|
|
files = []
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
resourcePath = os.path.split(resourceFilename)[0]
|
|
|
|
memoryPath = self.GetMemoryFilename(os.path.splitext(os.path.split(resourceFilename)[1])[0])
|
|
|
|
resourceFilename = self.GetMemoryFilename(os.path.split(resourceFilename)[1])
|
|
|
|
|
|
|
|
self.ReplaceFilenamesInXRC(resourceDocument.firstChild, files, resourcePath)
|
|
|
|
|
|
|
|
filename = resourceFilename
|
|
|
|
fileData = resourceDocument.toxml()
|
|
|
|
outputList.append(self.templates.FILE_AS_STRING % locals())
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
for f in files:
|
|
|
|
filename = self.GetMemoryFilename(f)
|
|
|
|
fileData = self.FileToString(os.path.join(resourcePath, f))
|
|
|
|
outputList.append(self.templates.FILE_AS_STRING % locals())
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
for f in [resourceFilename] + files:
|
|
|
|
filename = self.GetMemoryFilename(f)
|
|
|
|
outputList.append(self.templates.ADD_FILE_TO_MEMFS % locals())
|
|
|
|
|
|
|
|
outputList.append(self.templates.LOAD_RES_MEMFS % locals())
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
return "".join(outputList)
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
def GenerateInitResourcesFile(self, resourceFilename, resourceDocument):
|
2006-05-02 21:47:57 -04:00
|
|
|
# take only the filename portion out of resourceFilename
|
|
|
|
resourceFilename = os.path.split(resourceFilename)[1]
|
2006-04-08 02:17:11 -04:00
|
|
|
outputList = []
|
|
|
|
outputList.append(self.templates.LOAD_RES_FILE % locals())
|
|
|
|
return "".join(outputList)
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
#-------------------------------------------------------------------
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
def GetMemoryFilename(self, filename):
|
|
|
|
# Remove special chars from the filename
|
|
|
|
return re.sub(r"[^A-Za-z0-9_]", "_", filename)
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
#-------------------------------------------------------------------
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
def FileToString(self, filename):
|
|
|
|
outputList = []
|
|
|
|
|
2004-10-06 14:32:25 -04:00
|
|
|
buffer = open(filename, "rb").read()
|
2006-04-08 02:17:11 -04:00
|
|
|
fileLen = len(buffer)
|
2004-10-06 14:32:25 -04:00
|
|
|
|
|
|
|
linelng = 0
|
2006-04-08 02:17:11 -04:00
|
|
|
for i in xrange(fileLen):
|
2004-10-06 14:32:25 -04:00
|
|
|
s = buffer[i]
|
|
|
|
c = ord(s)
|
|
|
|
if s == '\n':
|
|
|
|
tmp = s
|
|
|
|
linelng = 0
|
|
|
|
elif c < 32 or c > 127 or s == "'":
|
|
|
|
tmp = "\\x%02x" % c
|
|
|
|
elif s == "\\":
|
|
|
|
tmp = "\\\\"
|
|
|
|
else:
|
|
|
|
tmp = s
|
|
|
|
|
|
|
|
if linelng > 70:
|
|
|
|
linelng = 0
|
2006-04-08 02:17:11 -04:00
|
|
|
outputList.append("\\\n")
|
|
|
|
|
|
|
|
outputList.append(tmp)
|
2004-10-06 14:32:25 -04:00
|
|
|
linelng += len(tmp)
|
2006-04-08 02:17:11 -04:00
|
|
|
|
|
|
|
return "".join(outputList)
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
def NodeContainsFilename(self, node):
|
|
|
|
""" Does 'node' contain filename information at all? """
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
# Any bitmaps:
|
|
|
|
if node.nodeName == "bitmap":
|
|
|
|
return True
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
if node.nodeName == "icon":
|
|
|
|
return True
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
# URLs in wxHtmlWindow:
|
|
|
|
if node.nodeName == "url":
|
|
|
|
return True
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
# wxBitmapButton:
|
|
|
|
parent = node.parentNode
|
|
|
|
if parent.__class__ != minidom.Document and \
|
|
|
|
parent.getAttribute("class") == "wxBitmapButton" and \
|
|
|
|
(node.nodeName == "focus" or node.nodeName == "disabled" or
|
|
|
|
node.nodeName == "selected"):
|
|
|
|
return True
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
# wxBitmap or wxIcon toplevel resources:
|
|
|
|
if node.nodeName == "object":
|
|
|
|
klass = node.getAttribute("class")
|
|
|
|
if klass == "wxBitmap" or klass == "wxIcon":
|
|
|
|
return True
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
return False
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
#-------------------------------------------------------------------
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
def ReplaceFilenamesInXRC(self, node, files, resourcePath):
|
|
|
|
""" Finds all files mentioned in resource file, e.g. <bitmap>filename</bitmap>
|
|
|
|
and replaces them with the memory filenames.
|
|
|
|
|
|
|
|
Fills a list of the filenames found."""
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
# Is 'node' XML node element?
|
|
|
|
if node is None: return
|
|
|
|
if node.nodeType != minidom.Document.ELEMENT_NODE: return
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
containsFilename = self.NodeContainsFilename(node);
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
for n in node.childNodes:
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
if (containsFilename and
|
|
|
|
(n.nodeType == minidom.Document.TEXT_NODE or
|
|
|
|
n.nodeType == minidom.Document.CDATA_SECTION_NODE)):
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
filename = n.nodeValue
|
|
|
|
memoryFilename = self.GetMemoryFilename(filename)
|
|
|
|
n.nodeValue = memoryFilename
|
|
|
|
|
|
|
|
if filename not in files:
|
|
|
|
files.append(filename)
|
|
|
|
|
|
|
|
# Recurse into children
|
|
|
|
if n.nodeType == minidom.Document.ELEMENT_NODE:
|
|
|
|
self.ReplaceFilenamesInXRC(n, files, resourcePath);
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-05-02 21:47:57 -04:00
|
|
|
#-------------------------------------------------------------------
|
|
|
|
|
|
|
|
def FindStringsInNode(self, parent):
|
|
|
|
def is_number(st):
|
|
|
|
try:
|
|
|
|
i = int(st)
|
|
|
|
return True
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
strings = []
|
|
|
|
if parent is None:
|
|
|
|
return strings;
|
|
|
|
|
|
|
|
for child in parent.childNodes:
|
|
|
|
if ((parent.nodeType == parent.ELEMENT_NODE) and
|
|
|
|
# parent is an element, i.e. has subnodes...
|
|
|
|
(child.nodeType == child.TEXT_NODE or
|
|
|
|
child.nodeType == child.CDATA_SECTION_NODE) and
|
|
|
|
# ...it is textnode...
|
|
|
|
(
|
|
|
|
parent.tagName == "label" or
|
|
|
|
(parent.tagName == "value" and
|
|
|
|
not is_number(child.nodeValue)) or
|
|
|
|
parent.tagName == "help" or
|
|
|
|
parent.tagName == "longhelp" or
|
|
|
|
parent.tagName == "tooltip" or
|
|
|
|
parent.tagName == "htmlcode" or
|
|
|
|
parent.tagName == "title" or
|
|
|
|
parent.tagName == "item"
|
|
|
|
)):
|
|
|
|
# ...and known to contain translatable string
|
|
|
|
if (parent.getAttribute("translate") != "0"):
|
|
|
|
strings.append(self.ConvertText(child.nodeValue))
|
|
|
|
|
|
|
|
# subnodes:
|
|
|
|
if child.nodeType == child.ELEMENT_NODE:
|
|
|
|
strings += self.FindStringsInNode(child)
|
|
|
|
|
|
|
|
return strings
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------
|
|
|
|
|
|
|
|
def ConvertText(self, st):
|
|
|
|
st2 = ""
|
|
|
|
dt = list(st)
|
|
|
|
|
|
|
|
skipNext = False
|
|
|
|
for i in range(len(dt)):
|
|
|
|
if skipNext:
|
|
|
|
skipNext = False
|
|
|
|
continue
|
|
|
|
|
|
|
|
if dt[i] == '_':
|
|
|
|
if dt[i+1] == '_':
|
|
|
|
st2 += '_'
|
|
|
|
skipNext = True
|
|
|
|
else:
|
|
|
|
st2 += '&'
|
|
|
|
elif dt[i] == '\n':
|
|
|
|
st2 += '\\n'
|
|
|
|
elif dt[i] == '\t':
|
|
|
|
st2 += '\\t'
|
|
|
|
elif dt[i] == '\r':
|
|
|
|
st2 += '\\r'
|
|
|
|
elif dt[i] == '\\':
|
|
|
|
if dt[i+1] not in ['n', 't', 'r']:
|
|
|
|
st2 += '\\\\'
|
|
|
|
else:
|
|
|
|
st2 += '\\'
|
|
|
|
elif dt[i] == '"':
|
|
|
|
st2 += '\\"'
|
|
|
|
else:
|
|
|
|
st2 += dt[i]
|
|
|
|
|
|
|
|
return st2
|
|
|
|
|
|
|
|
|
2006-05-24 00:49:07 -04:00
|
|
|
#-------------------------------------------------------------------
|
|
|
|
|
|
|
|
def _OpenOutputFile(self, outputFilename):
|
|
|
|
if outputFilename == "-":
|
|
|
|
outputFile = sys.stdout
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
outputFile = open(outputFilename, "wt")
|
|
|
|
except IOError:
|
|
|
|
raise IOError("Can't write output to '%s'" % outputFilename)
|
|
|
|
return outputFile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2006-05-02 21:47:57 -04:00
|
|
|
|
2004-10-06 14:32:25 -04:00
|
|
|
#---------------------------------------------------------------------------
|
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
def main(args):
|
|
|
|
resourceFilename = ""
|
2006-05-24 00:49:07 -04:00
|
|
|
outputFilename = None
|
2006-04-08 02:17:11 -04:00
|
|
|
embedResources = False
|
2006-05-02 21:47:57 -04:00
|
|
|
generateGetText = False
|
2006-05-24 00:49:07 -04:00
|
|
|
generatePython = False
|
2006-04-08 02:17:11 -04:00
|
|
|
|
|
|
|
try:
|
2006-05-24 00:49:07 -04:00
|
|
|
opts, args = getopt.gnu_getopt(args,
|
|
|
|
"hpgeo:",
|
|
|
|
"help python gettext embed output=".split())
|
2006-05-02 21:47:57 -04:00
|
|
|
except getopt.GetoptError, e:
|
|
|
|
print "\nError : %s\n" % str(e)
|
2006-04-08 02:17:11 -04:00
|
|
|
print __doc__
|
|
|
|
sys.exit(1)
|
2004-10-06 14:32:25 -04:00
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
# If there is no input file argument, show help and exit
|
2006-05-24 00:49:07 -04:00
|
|
|
if not args:
|
2006-04-08 02:17:11 -04:00
|
|
|
print __doc__
|
2006-05-24 00:49:07 -04:00
|
|
|
print "No xrc input file was specified."
|
2006-04-08 02:17:11 -04:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Parse options and arguments
|
|
|
|
for opt, val in opts:
|
|
|
|
if opt in ["-h", "--help"]:
|
|
|
|
print __doc__
|
|
|
|
sys.exit(1)
|
|
|
|
|
2006-05-24 00:49:07 -04:00
|
|
|
if opt in ["-p", "--python"]:
|
|
|
|
generatePython = True
|
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
if opt in ["-o", "--output"]:
|
|
|
|
outputFilename = val
|
|
|
|
|
|
|
|
if opt in ["-e", "--embed"]:
|
|
|
|
embedResources = True
|
|
|
|
|
2006-05-02 21:47:57 -04:00
|
|
|
if opt in ["-g", "--gettext"]:
|
|
|
|
generateGetText = True
|
|
|
|
|
2006-05-24 00:49:07 -04:00
|
|
|
|
|
|
|
# check for and expand any wildcards in the list of input files
|
|
|
|
inputFiles = []
|
|
|
|
for arg in args:
|
|
|
|
inputFiles += glob.glob(arg)
|
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
|
|
|
|
comp = XmlResourceCompiler()
|
|
|
|
|
|
|
|
try:
|
2006-05-24 00:49:07 -04:00
|
|
|
if generatePython:
|
|
|
|
if not outputFilename:
|
|
|
|
outputFilename = os.path.splitext(args[0])[0] + "_xrc.py"
|
|
|
|
comp.MakePythonModule(inputFiles, outputFilename,
|
|
|
|
embedResources, generateGetText)
|
|
|
|
|
|
|
|
elif generateGetText:
|
|
|
|
if not outputFilename:
|
|
|
|
outputFilename = '-'
|
|
|
|
comp.MakeGetTextOutput(inputFiles, outputFilename)
|
|
|
|
|
|
|
|
else:
|
|
|
|
print __doc__
|
|
|
|
print "One or both of -p, -g must be specified."
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
|
2006-04-08 02:17:11 -04:00
|
|
|
except IOError, e:
|
|
|
|
print >>sys.stderr, "%s." % str(e)
|
|
|
|
else:
|
|
|
|
if outputFilename != "-":
|
|
|
|
print >>sys.stderr, "Resources written to %s." % outputFilename
|
2004-10-06 14:32:25 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2006-04-08 02:17:11 -04:00
|
|
|
main(sys.argv[1:])
|
2004-10-06 14:32:25 -04:00
|
|
|
|