Make it possible for pywxrc to generate just the gettext _() strings
with no other code, and also make it possible to process more than one XRC file at once into a single output module. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@39308 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -427,7 +427,7 @@ class Frame(wx.Frame):
|
|||||||
try:
|
try:
|
||||||
import wx.tools.pywxrc
|
import wx.tools.pywxrc
|
||||||
rescomp = wx.tools.pywxrc.XmlResourceCompiler()
|
rescomp = wx.tools.pywxrc.XmlResourceCompiler()
|
||||||
rescomp.MakePythonModule(dataFile, pypath, embed, genGettext)
|
rescomp.MakePythonModule([dataFile], pypath, embed, genGettext)
|
||||||
except:
|
except:
|
||||||
inf = sys.exc_info()
|
inf = sys.exc_info()
|
||||||
wx.LogError(traceback.format_exception(inf[0], inf[1], None)[-1])
|
wx.LogError(traceback.format_exception(inf[0], inf[1], None)[-1])
|
||||||
|
@@ -19,12 +19,13 @@ pywxrc -- Python XML resource compiler
|
|||||||
(see http://wiki.wxpython.org/index.cgi/pywxrc for more info)
|
(see http://wiki.wxpython.org/index.cgi/pywxrc for more info)
|
||||||
|
|
||||||
Usage: python pywxrc.py -h
|
Usage: python pywxrc.py -h
|
||||||
python pywxrc.py <resource.xrc> [-e] [-g] [-o filename]
|
python pywxrc.py [-p] [-g] [-e] [-o filename] xrc input files...
|
||||||
|
|
||||||
-h, --help show help message
|
-h, --help show help message
|
||||||
-e, --embed embed resources in the output file
|
-p, --python generate python module
|
||||||
-g, --gettext embed list of translatable strings in the output file
|
-g, --gettext output list of translatable strings (may be combined with -p)
|
||||||
-o, --output output filename, or - for stdout
|
-e, --embed embed XRC resources in the output file
|
||||||
|
-o, --output output filename, or - for stdout
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import sys, os, getopt, glob, re
|
import sys, os, getopt, glob, re
|
||||||
@@ -79,23 +80,20 @@ class xrc%(windowName)s(wx.%(windowClass)s):
|
|||||||
# ------------------------ Resource data ----------------------
|
# ------------------------ Resource data ----------------------
|
||||||
|
|
||||||
def __init_resources():
|
def __init_resources():
|
||||||
|
global __res
|
||||||
|
__res = xrc.EmptyXmlResource()
|
||||||
"""
|
"""
|
||||||
|
|
||||||
LOAD_RES_FILE = """\
|
LOAD_RES_FILE = """\
|
||||||
global __res
|
__res.Load('%(resourceFilename)s')"""
|
||||||
__res = xrc.XmlResource('%(resourceFilename)s')
|
|
||||||
"""
|
|
||||||
|
|
||||||
FILE_AS_STRING = """\
|
FILE_AS_STRING = """\
|
||||||
%(filename)s = '''\\
|
%(filename)s = '''\\
|
||||||
%(fileData)s'''
|
%(fileData)s'''
|
||||||
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
PREPARE_MEMFS = """\
|
PREPARE_MEMFS = """\
|
||||||
# Load all the strings as memory files
|
|
||||||
|
|
||||||
wx.FileSystem.AddHandler(wx.MemoryFSHandler())
|
wx.FileSystem.AddHandler(wx.MemoryFSHandler())
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@@ -104,12 +102,10 @@ def __init_resources():
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
LOAD_RES_MEMFS = """\
|
LOAD_RES_MEMFS = """\
|
||||||
global __res
|
|
||||||
__res = xrc.EmptyXmlResource()
|
|
||||||
__res.Load('memory:XRC/%(memoryPath)s/%(resourceFilename)s')
|
__res.Load('memory:XRC/%(memoryPath)s/%(resourceFilename)s')
|
||||||
"""
|
"""
|
||||||
|
|
||||||
GETTEXT_DUMMY_FUNC = """\
|
GETTEXT_DUMMY_FUNC = """
|
||||||
# ----------------------- Gettext strings ---------------------
|
# ----------------------- Gettext strings ---------------------
|
||||||
|
|
||||||
def __gettext_strings():
|
def __gettext_strings():
|
||||||
@@ -121,7 +117,7 @@ def __gettext_strings():
|
|||||||
|
|
||||||
def _(str): pass
|
def _(str): pass
|
||||||
|
|
||||||
%(gettextStrings)s
|
%s
|
||||||
"""
|
"""
|
||||||
|
|
||||||
#----------------------------------------------------------------------
|
#----------------------------------------------------------------------
|
||||||
@@ -132,27 +128,57 @@ class XmlResourceCompiler:
|
|||||||
|
|
||||||
"""This class generates Python code from XML resource files (XRC)."""
|
"""This class generates Python code from XML resource files (XRC)."""
|
||||||
|
|
||||||
def MakePythonModule(self, resourceFilename, outputFilename,
|
def MakePythonModule(self, inputFiles, outputFilename,
|
||||||
embedResources=False, generateGetText=False):
|
embedResources=False, generateGetText=False):
|
||||||
if outputFilename == "-":
|
|
||||||
outputFile = sys.stdout
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
outputFile = open(outputFilename, "wt")
|
|
||||||
except IOError:
|
|
||||||
raise IOError("Can't write output to '%s'" % outputFilename)
|
|
||||||
|
|
||||||
resourceDocument = minidom.parse(resourceFilename)
|
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
|
||||||
print >>outputFile, self.templates.FILE_HEADER
|
print >>outputFile, self.templates.FILE_HEADER
|
||||||
print >>outputFile, self.GenerateClasses(resourceDocument)
|
print >>outputFile, "\n".join(classes)
|
||||||
|
|
||||||
|
print >>outputFile, self.templates.INIT_RESOURE_HEADER
|
||||||
if embedResources:
|
if embedResources:
|
||||||
print >>outputFile, self.GenerateInitResourcesEmbedded(resourceFilename, resourceDocument)
|
print >>outputFile, self.templates.PREPARE_MEMFS
|
||||||
else:
|
print >>outputFile, "\n".join(resources)
|
||||||
print >>outputFile, self.GenerateInitResourcesFile(resourceFilename, resourceDocument)
|
|
||||||
|
|
||||||
if generateGetText:
|
if generateGetText:
|
||||||
print >>outputFile, self.GenerateGetText(resourceDocument)
|
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)
|
||||||
|
|
||||||
#-------------------------------------------------------------------
|
#-------------------------------------------------------------------
|
||||||
|
|
||||||
@@ -187,20 +213,8 @@ class XmlResourceCompiler:
|
|||||||
|
|
||||||
#-------------------------------------------------------------------
|
#-------------------------------------------------------------------
|
||||||
|
|
||||||
def GenerateGetText(self, resourceDocument):
|
|
||||||
resource = resourceDocument.firstChild
|
|
||||||
strings = self.FindStringsInNode(resource)
|
|
||||||
strings = [' _("%s")\n' % s for s in strings]
|
|
||||||
gettextStrings = "".join(strings)
|
|
||||||
return self.templates.GETTEXT_DUMMY_FUNC % locals()
|
|
||||||
|
|
||||||
#-------------------------------------------------------------------
|
|
||||||
|
|
||||||
def GenerateInitResourcesEmbedded(self, resourceFilename, resourceDocument):
|
def GenerateInitResourcesEmbedded(self, resourceFilename, resourceDocument):
|
||||||
outputList = []
|
outputList = []
|
||||||
|
|
||||||
outputList.append(self.templates.INIT_RESOURE_HEADER)
|
|
||||||
|
|
||||||
files = []
|
files = []
|
||||||
|
|
||||||
resourcePath = os.path.split(resourceFilename)[0]
|
resourcePath = os.path.split(resourceFilename)[0]
|
||||||
@@ -218,8 +232,6 @@ class XmlResourceCompiler:
|
|||||||
fileData = self.FileToString(os.path.join(resourcePath, f))
|
fileData = self.FileToString(os.path.join(resourcePath, f))
|
||||||
outputList.append(self.templates.FILE_AS_STRING % locals())
|
outputList.append(self.templates.FILE_AS_STRING % locals())
|
||||||
|
|
||||||
outputList.append(self.templates.PREPARE_MEMFS % locals())
|
|
||||||
|
|
||||||
for f in [resourceFilename] + files:
|
for f in [resourceFilename] + files:
|
||||||
filename = self.GetMemoryFilename(f)
|
filename = self.GetMemoryFilename(f)
|
||||||
outputList.append(self.templates.ADD_FILE_TO_MEMFS % locals())
|
outputList.append(self.templates.ADD_FILE_TO_MEMFS % locals())
|
||||||
@@ -234,7 +246,6 @@ class XmlResourceCompiler:
|
|||||||
# take only the filename portion out of resourceFilename
|
# take only the filename portion out of resourceFilename
|
||||||
resourceFilename = os.path.split(resourceFilename)[1]
|
resourceFilename = os.path.split(resourceFilename)[1]
|
||||||
outputList = []
|
outputList = []
|
||||||
outputList.append(self.templates.INIT_RESOURE_HEADER)
|
|
||||||
outputList.append(self.templates.LOAD_RES_FILE % locals())
|
outputList.append(self.templates.LOAD_RES_FILE % locals())
|
||||||
return "".join(outputList)
|
return "".join(outputList)
|
||||||
|
|
||||||
@@ -416,27 +427,44 @@ class XmlResourceCompiler:
|
|||||||
return st2
|
return st2
|
||||||
|
|
||||||
|
|
||||||
|
#-------------------------------------------------------------------
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#---------------------------------------------------------------------------
|
#---------------------------------------------------------------------------
|
||||||
|
|
||||||
def main(args):
|
def main(args):
|
||||||
resourceFilename = ""
|
resourceFilename = ""
|
||||||
outputFilename = ""
|
outputFilename = None
|
||||||
embedResources = False
|
embedResources = False
|
||||||
generateGetText = False
|
generateGetText = False
|
||||||
|
generatePython = False
|
||||||
|
|
||||||
try:
|
try:
|
||||||
opts, args = getopt.gnu_getopt(args, "hego:", "help embed gettext output=".split())
|
opts, args = getopt.gnu_getopt(args,
|
||||||
|
"hpgeo:",
|
||||||
|
"help python gettext embed output=".split())
|
||||||
except getopt.GetoptError, e:
|
except getopt.GetoptError, e:
|
||||||
print "\nError : %s\n" % str(e)
|
print "\nError : %s\n" % str(e)
|
||||||
print __doc__
|
print __doc__
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# If there is no input file argument, show help and exit
|
# If there is no input file argument, show help and exit
|
||||||
if args:
|
if not args:
|
||||||
resourceFilename = args[0]
|
|
||||||
else:
|
|
||||||
print __doc__
|
print __doc__
|
||||||
|
print "No xrc input file was specified."
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
# Parse options and arguments
|
# Parse options and arguments
|
||||||
@@ -445,6 +473,9 @@ def main(args):
|
|||||||
print __doc__
|
print __doc__
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
if opt in ["-p", "--python"]:
|
||||||
|
generatePython = True
|
||||||
|
|
||||||
if opt in ["-o", "--output"]:
|
if opt in ["-o", "--output"]:
|
||||||
outputFilename = val
|
outputFilename = val
|
||||||
|
|
||||||
@@ -454,13 +485,33 @@ def main(args):
|
|||||||
if opt in ["-g", "--gettext"]:
|
if opt in ["-g", "--gettext"]:
|
||||||
generateGetText = True
|
generateGetText = True
|
||||||
|
|
||||||
if outputFilename is None or outputFilename == "":
|
|
||||||
outputFilename = os.path.splitext(resourceFilename)[0] + "_xrc.py"
|
# check for and expand any wildcards in the list of input files
|
||||||
|
inputFiles = []
|
||||||
|
for arg in args:
|
||||||
|
inputFiles += glob.glob(arg)
|
||||||
|
|
||||||
|
|
||||||
comp = XmlResourceCompiler()
|
comp = XmlResourceCompiler()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
comp.MakePythonModule(resourceFilename, outputFilename, embedResources, generateGetText)
|
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)
|
||||||
|
|
||||||
|
|
||||||
except IOError, e:
|
except IOError, e:
|
||||||
print >>sys.stderr, "%s." % str(e)
|
print >>sys.stderr, "%s." % str(e)
|
||||||
else:
|
else:
|
||||||
|
Reference in New Issue
Block a user