/usr/share/tpclient-pywx/windows/xrc/generate.py is in tpclient-pywx 0.3.1.1-3.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | # This script generates boilerplate classes out of an XRC resource file.
# For each top-level windows (Panel, Frame or Dialog) a class is generated
# and all the controls become attributes of the class.
import sys
import re
import os
from xml.dom import minidom
# --------------------------- Template definitions --------------------------
fileHeaderTemplate = """\
# This file has been automatically generated.
# Please do not edit it manually.
# Python Imports
import os.path
# wxPython imports
import wx
from wx.xrc import XRCCTRL, XmlResourceWithHandlers
# Local imports
from requirements import location
"""
frameTemplate = """\
class %(windowName)sBase:
\"\"\"\\
Unlike a normal XRC generated class, this is a not a full class but a MixIn.
Any class which uses this as a base must also inherit from a proper wx object
such as the wx.Frame class.
This is so that a the same XRC can be used for both MDI and non-MDI frames.
\"\"\"
xrc = os.path.join(location(), "windows", "xrc", '%(fileName)s')
def PreCreate(self, pre):
\"\"\" This function is called during the class's initialization.
Override it for custom setup before the window is created usually to
set additional window styles using SetWindowStyle() and SetExtraStyle().\"\"\"
pass
def __init__(self, parent, *args, **kw):
\"\"\" Pass an initialized wx.xrc.XmlResource into res \"\"\"
f = os.path.join(os.path.dirname(__file__), self.xrc)
res = XmlResourceWithHandlers(f)
# Figure out what Frame class (MDI, MiniFrame, etc) is actually our base...
bases = set()
def findbases(klass, set):
for base in klass.__bases__:
set.add(base)
findbases(base, set)
findbases(self.__class__, bases)
for base in bases:
if base.__name__.endswith("Frame"):
break
# Two stage creation (see http://wiki.wxpython.org/index.cgi/TwoStageCreation)
pre = getattr(wx, "Pre%%s" %% base.__name__)()
res.LoadOnFrame(pre, parent, "%(windowName)s")
self.PreCreate(pre)
self.PostCreate(pre)
# Define variables for the controls"""
panelTemplate = """\
class %(windowName)sBase(wx.Panel):
xrc = os.path.join(location(), "windows", "xrc", '%(fileName)s')
def PreCreate(self, pre):
\"\"\" This function is called during the class's initialization.
Override it for custom setup before the window is created usually to
set additional window styles using SetWindowStyle() and SetExtraStyle().\"\"\"
pass
def __init__(self, parent, *args, **kw):
\"\"\" Pass an initialized wx.xrc.XmlResource into res \"\"\"
f = os.path.join(os.path.dirname(__file__), self.xrc)
res = XmlResourceWithHandlers(f)
# Two stage creation (see http://wiki.wxpython.org/index.cgi/TwoStageCreation)
pre = wx.PrePanel()
res.LoadOnPanel(pre, parent, "%(windowName)s")
self.PreCreate(pre)
self.PostCreate(pre)
# Define variables for the controls"""
IDmap = {
"wxID_CANCEL": "Cancel",
"wxID_CLOSE": "Close",
"wxID_SAVE": "Save",
"wxID_SAVEAS": "SaveAs",
"wxID_NEW": "New",
"wxID_OK": "Okay",
"wxID_FIND": "Find",
"wxID_REFRESH": "Refresh",
"wxID_PREFERENCES": "Config",
}
Template_Default = """\
self.%(controlName)s = XRCCTRL(self, "%(controlID)s")"""
Template_Button = """\
self.%(controlName)s = XRCCTRL(self, "%(controlID)s")
if hasattr(self, "On%(controlName)s"):
self.Bind(wx.EVT_BUTTON, self.On%(controlName)s, self.%(controlName)s)
"""
Template_CheckBox = """\
self.%(controlName)s = XRCCTRL(self, "%(controlID)s")
if hasattr(self, "On%(controlName)s"):
self.Bind(wx.EVT_CHECKBOX, self.On%(controlName)s, self.%(controlName)s)
"""
Template_ToggleButton = """\
self.%(controlName)s = XRCCTRL(self, "%(controlID)s")
if hasattr(self, "On%(controlName)s"):
self.Bind(wx.EVT_TOGGLEBUTTON, self.On%(controlName)s, self.%(controlName)s)
"""
Template_BitmapButton = Template_Button
Template_ComboBox = """\
self.%(controlName)s = XRCCTRL(self, "%(controlID)s")
if hasattr(self, "On%(controlName)s"):
self.Bind(wx.EVT_COMBOBOX, self.On%(controlName)s, self.%(controlName)s)
self.Bind(wx.EVT_TEXT_ENTER, self.On%(controlName)s, self.%(controlName)s)
if hasattr(self, "OnDirty%(controlName)s"):
self.Bind(wx.EVT_TEXT, self.On%(controlName)s, self.%(controlName)s)
"""
Template_Choice = """\
self.%(controlName)s = XRCCTRL(self, "%(controlID)s")
if hasattr(self, "On%(controlName)s"):
self.%(controlName)s.Bind(wx.EVT_CHOICE, self.On%(controlName)s)
"""
def Generate_wxFrame(xrcFile, topWindow, outFile):
fileName = os.path.basename(xrcFile.name)
windowClass = topWindow.getAttribute("subclass")
if len(windowClass) == 0:
windowClass = topWindow.getAttribute("class")
windowClass = re.sub("^wx", "wx.", windowClass)
windowName = topWindow.getAttribute("name")
print "'%s' is a '%s'"% (windowName, windowClass)
print >> outFile, frameTemplate % locals()
eventFunctions = [] # a list to store the code for the event functions
# Generate a variable for each control, and standard event handlers
# for standard controls.
for control in topWindow.getElementsByTagName("object"):
controlClass = control.getAttribute("class")
controlClass = re.sub("^wx", "wx.", controlClass)
controlName = control.getAttribute("name")
# Ignore anything which is still got a wx name...
if controlName in IDmap:
controlID = controlName
controlName = IDmap[controlName]
else:
controlID = controlName
if "wx" in controlName:
continue
if controlName != "" and controlClass != "":
print '\t', controlName, (3-(len(controlName)+1)/8)*'\t', "is a", controlClass
try:
template = globals()["Template_%s" % controlClass.replace('wx.', '')]
except KeyError:
template = globals()["Template_Default"]
print >> outFile, template % locals()
print >> outFile
print >> outFile, "\n".join(eventFunctions)
def Generate_wxPanel(xrcFile, topWindow, outFile):
fileName = os.path.basename(xrcFile.name)
windowClass = topWindow.getAttribute("subclass")
if len(windowClass) == 0:
windowClass = topWindow.getAttribute("class")
windowClass = re.sub("^wx", "wx.", windowClass)
windowName = topWindow.getAttribute("name")
print "'%s' is a '%s'"% (windowName, windowClass)
print >> outFile, panelTemplate % locals()
eventFunctions = [] # a list to store the code for the event functions
# Generate a variable for each control, and standard event handlers
# for standard controls.
for control in topWindow.getElementsByTagName("object"):
controlClass = control.getAttribute("class")
controlClass = re.sub("^wx", "wx.", controlClass)
controlName = control.getAttribute("name")
# Ignore anything which is still got a wx name...
if controlName in IDmap:
controlID = controlName
controlName = IDmap[controlName]
else:
controlID = controlName
if "wx" in controlName:
continue
if controlName != "" and controlClass != "":
print '\t', controlName, (3-(len(controlName)+1)/8)*'\t', "is a", controlClass
try:
template = globals()["Template_%s" % controlClass.replace('wx.', '')]
except KeyError:
template = globals()["Template_Default"]
print >> outFile, template % locals()
print >> outFile
print >> outFile, "\n".join(eventFunctions)
#Generate_wxWizard = Generate_wxDialog
# ------------------------- GeneratePythonForXRC ----------------------------
def GeneratePython(xrcFile, outFile):
xmldoc = minidom.parse(xrcFile)
resource = xmldoc.childNodes[0]
topWindows = [e for e in resource.childNodes
if e.nodeType == e.ELEMENT_NODE and e.tagName == "object"]
print >> outFile, fileHeaderTemplate
# Generate a class for each top-window object (Frame, Panel, Dialog, etc.)
for topWindow in topWindows:
# Figure out if this is a Panel, Frame or Wizard
windowClass = topWindow.getAttribute("class")
windowName = topWindow.getAttribute("name")
globals()["Generate_"+windowClass](xrcFile, topWindow, outFile)
# --------------------- Main ----------------
def Usage():
print """
xrcpy -- Python boilerplate code generator for XRC resources.
Usage : python pyxrc.py <resource.xrc>
The Python code is printed to the standard output.
"""
from wx.tools.pywxrc import XmlResourceCompiler
def main():
if len(sys.argv) != 2:
Usage()
else:
inFilename = sys.argv[1]
outFilename = os.path.splitext(inFilename)[0] + ".py"
try:
inStream = file(inFilename)
try:
outStream = file(outFilename, "w")
except IOError:
print >> sys.stderr, "Can't open '%s'!" % outFilename
else:
GeneratePython(inStream, outStream)
comp = XmlResourceCompiler()
comp.MakeGetTextOutput([inFilename], '.translation')
transStream = file('.translation', 'r')
outStream.write('def strings():\n')
outStream.write('\tpass\n')
outStream.write(transStream.read().replace('_(', '\t_('))
transStream.close()
os.unlink('.translation')
print "Result stored in %s." % outFilename
except IOError:
print >> sys.stderr, "Can't open '%s'!" % inFilename
except KeyError, e:
print >> sys.stderr, "There was an error outputting .py file for '%s'" % inFilename
print >> sys.stderr, "No such key %s" % e
os.unlink(outFilename)
print >> sys.stderr
if __name__ == "__main__":
main()
|