/usr/lib/python2.7/dist-packages/wxglade/codegen/xrc_codegen.py is in python-wxglade 0.6.8-2.2.
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 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 | """\
XRC code generator
Generates the xml code for the app in XRC format.
Calls the appropriate ``writers'' of the various objects. These functions
return an instance of XrcObject
@copyright: 2002-2007 Alberto Griggio <agriggio@users.sourceforge.net>
@copyright: 2012 Carsten Grohmann <mail@carstengrohmann.de>
@license: MIT (see license.txt) - THIS PROGRAM COMES WITH NO WARRANTY
"""
import cStringIO
import common
from xml.sax.saxutils import escape, quoteattr
from codegen import BaseCodeWriter, \
EventsPropertyHandler, \
ExtraPropertiesPropertyHandler
from ordereddict import OrderedDict
class FontPropertyHandler:
def __init__(self):
self.props = {'size': '', 'family': '', 'style': '', 'weight': '',
'underlined': '', 'face': ''}
self.current = None
def start_elem(self, name, attrs):
self.current = name
def end_elem(self, name, code_obj):
if name == 'font':
code_obj.properties['font'] = self.props
return True # to remove this handler
def char_data(self, data):
self.props[self.current] = str(data.strip())
# end of class FontHandler
class XRCCodeWriter(BaseCodeWriter):
"""\
Code writer class for writing XRC XML code out of the designed GUI elements
"""
default_extensions = ['xrc']
language = "XRC"
xrc_objects = None
"""\
dictionary of active L{XrcObject} instances: during the code generation
it stores all the non-sizer objects that have children (i.e. frames,
dialogs, panels, notebooks, etc.), while at the end of the code generation,
before L{finalize} is called, it contains only the true toplevel objects
(frames and dialogs), and is used to write their XML code
(see L{finalize}). The other objects are deleted when L{add_object} is
called with their corresponding code_object as argument
(see L{add_object})
"""
global_property_writers = {
'font': FontPropertyHandler,
'events': EventsPropertyHandler,
'extraproperties': ExtraPropertiesPropertyHandler,
}
"""\
Dictionary whose items are custom handlers for widget properties
"""
property_writers = {}
"""\
Dictionary of dictionaries of property handlers specific for a widget
the keys are the class names of the widgets
Example: property_writers['wxRadioBox'] = {'choices', choices_handler}
"""
obj_builders = {}
"""\
Dictionary of ``writers'' for the various objects
"""
tmpl_encoding = '<?xml version="1.0" encoding="%s"?>\n'
tmpl_generated_by = '<!-- %(generated_by)s -->'
# Nested classes
class XrcObject(object):
"""\
Class to produce the XRC code for a given widget. This is a base class
which does nothing
"""
def __init__(self):
self.properties = {}
self.children = [] # sub-objects
def write_child_prologue(self, child, out_file, ntabs):
pass
def write_child_epilogue(self, child, out_file, ntabs):
pass
def write_property(self, name, val, outfile, ntabs):
pass
def write(self, out_file, ntabs):
pass
def warning(self, msg):
"""\
Show a warning message
@param msg: Warning message
@type msg: String
@see: L{common.MessageLogger.warn()}
"""
common.message.warn(msg)
# end of class XrcObject
class SizerItemXrcObject(XrcObject):
"""\
XrcObject to handle sizer items
"""
def __init__(self, obj, option, flag, border):
XRCCodeWriter.XrcObject.__init__(self)
self.obj = obj # the XrcObject representing the widget
self.option = option
self.flag = flag
self.border = border
def write(self, out_file, ntabs):
write = out_file.write
write(self.tabs(ntabs) + '<object class="sizeritem">\n')
if self.option != '0':
write(self.tabs(ntabs + 1) + '<option>%s</option>\n' % \
self.option)
if self.flag and self.flag != '0':
write(self.tabs(ntabs + 1) + '<flag>%s</flag>\n' % self.flag)
if self.border != '0':
write(self.tabs(ntabs + 1) + '<border>%s</border>\n' % \
self.border)
# write the widget
self.obj.write(out_file, ntabs + 1)
write(self.tabs(ntabs) + '</object>\n')
# end of class SizerItemXrcObject
class SpacerXrcObject(XrcObject):
"""\
XrcObject to handle widgets
"""
def __init__(self, size_str, option, flag, border):
XRCCodeWriter.XrcObject.__init__(self)
self.size_str = size_str
self.option = option
self.flag = flag
self.border = border
def write(self, out_file, ntabs):
write = out_file.write
write(self.tabs(ntabs) + '<object class="spacer">\n')
write(self.tabs(ntabs + 1) + \
'<size>%s</size>\n' % self.size_str.strip())
if self.option != '0':
write(self.tabs(ntabs + 1) + '<option>%s</option>\n' % \
self.option)
if self.flag and self.flag != '0':
write(self.tabs(ntabs + 1) + '<flag>%s</flag>\n' % self.flag)
if self.border != '0':
write(self.tabs(ntabs + 1) + '<border>%s</border>\n' % \
self.border)
write(self.tabs(ntabs) + '</object>\n')
# end of class SpacerXrcObject
class DefaultXrcObject(XrcObject):
"""\
Standard XrcObject for every widget, used if no specific XrcObject is
available
"""
def __init__(self, code_obj):
XRCCodeWriter.XrcObject.__init__(self)
self.properties = code_obj.properties
self.code_obj = code_obj
self.name = code_obj.name
self.klass = code_obj.base # custom classes aren't allowed in XRC
self.subclass = code_obj.klass
def write_property(self, name, val, outfile, ntabs):
if val:
name = escape(name)
outfile.write(self.tabs(ntabs) + '<%s>%s</%s>\n' % \
(name, escape(val), name))
def write(self, out_file, ntabs):
write = out_file.write
if self.code_obj.in_sizers:
write(self.tabs(ntabs) + \
'<object class=%s>\n' % quoteattr(self.klass))
else:
if self.subclass and self.subclass != self.klass:
write(self.tabs(ntabs) +
'<object class=%s name=%s subclass=%s>\n' % \
(quoteattr(self.klass), quoteattr(self.name),
quoteattr(self.subclass)))
else:
write(self.tabs(ntabs) + '<object class=%s name=%s>\n' % \
(quoteattr(self.klass), quoteattr(self.name)))
tab_str = self.tabs(ntabs + 1)
# write the properties
if self.properties.has_key('foreground'):
if self.properties['foreground'].startswith('#'):
# XRC does not support colors from system settings
self.properties['fg'] = self.properties['foreground']
del self.properties['foreground']
if self.properties.has_key('background'):
if self.properties['background'].startswith('#'):
# XRC does not support colors from system settings
self.properties['bg'] = self.properties['background']
del self.properties['background']
if self.properties.has_key('font'):
font = self.properties['font']
del self.properties['font']
else:
font = None
style = str(self.properties.get('style', ''))
if style and style == '0':
del self.properties['style']
if 'id' in self.properties:
del self.properties['id'] # id has no meaning for XRC
# ALB 2004-12-05
if 'events' in self.properties:
#del self.properties['events'] # no event handling in XRC
for handler, event in self.properties['events'].iteritems():
write(tab_str + '<handler event=%s>%s</handler>\n' % \
(quoteattr(handler), escape(event)))
del self.properties['events']
# 'disabled' property is actually 'enabled' for XRC
if 'disabled' in self.properties:
try:
val = int(self.properties['disabled'])
except:
val = False
if val:
self.properties['enabled'] = '0'
del self.properties['disabled']
# ALB 2007-08-31 extracode property
if 'extracode' in self.properties:
write(self.properties['extracode'].replace('\\n', '\n'))
del self.properties['extracode']
# custom base classes are ignored for XRC...
if 'custom_base' in self.properties:
del self.properties['custom_base']
if 'extraproperties' in self.properties:
prop = self.properties['extraproperties']
del self.properties['extraproperties']
self.properties.update(prop)
for name, val in self.properties.iteritems():
self.write_property(str(name), val, out_file, ntabs + 1)
# write the font, if present
if font:
write(tab_str + '<font>\n')
tab_str = self.tabs(ntabs + 2)
for key, val in font.iteritems():
if val:
write(tab_str + '<%s>%s</%s>\n' % \
(escape(key), escape(val), escape(key)))
write(self.tabs(ntabs + 1) + '</font>\n')
# write the children
for c in self.children:
self.write_child_prologue(c, out_file, ntabs + 1)
c.write(out_file, ntabs + 1)
self.write_child_epilogue(c, out_file, ntabs + 1)
write(self.tabs(ntabs) + '</object>\n')
# end of class DefaultXrcObject
class NotImplementedXrcObject(XrcObject):
"""\
XrcObject used when no code for the widget can be generated (for
example, because XRC does not currently handle such widget)
"""
def __init__(self, code_obj):
XRCCodeWriter.XrcObject.__init__(self)
self.code_obj = code_obj
def write(self, outfile, ntabs):
m = 'code generator for %s objects not available' % \
self.code_obj.base
self.warning('%s' % m)
outfile.write(self.tabs(ntabs) + '<!-- %s -->\n' % m)
# end of class NotImplementedXrcObject
def __init__(self):
BaseCodeWriter.__init__(self)
# Inject to all classed derivated from WrcObject
XRCCodeWriter.XrcObject.tabs = self.tabs
def initialize(self, app_attrs):
# initialise parent class
BaseCodeWriter.initialize(self, app_attrs)
out_path = app_attrs['path']
if self.multiple_files:
# for now we handle only single-file code generation
raise IOError("XRC code cannot be split into multiple files")
self.output_file_name = out_path
self.out_file = cStringIO.StringIO() # open(out_path, 'w')
self.out_file.write('\n<resource version="2.3.0.1">\n')
self.curr_tab = 1
self.xrc_objects = OrderedDict()
def finalize(self):
# write the code for every toplevel object
for obj in self.xrc_objects.itervalues():
obj.write(self.out_file, 1)
self.out_file.write('</resource>\n')
# store the contents to file
self.save_file(
self.output_file_name,
self.out_file.getvalue()
)
def add_app(self, app_attrs, top_win_class):
"""\
In the case of XRC output, there's no wxApp code to generate
"""
pass
def add_object(self, unused, sub_obj):
"""\
Adds the object sub_obj to the XRC tree. The first argument is unused.
"""
# what we need in XRC is not top_obj, but sub_obj's true parent
top_obj = sub_obj.parent
builder = self.obj_builders.get(
sub_obj.base,
XRCCodeWriter.DefaultXrcObject
)
try:
# check whether we already created the xrc_obj
xrc_obj = sub_obj.xrc
except AttributeError:
xrc_obj = builder(sub_obj) # builder functions must return a
# subclass of XrcObject
sub_obj.xrc = xrc_obj
else:
# if we found it, remove it from the self.xrc_objects dictionary
# (if it was there, i.e. the object is not a sizer), because this
# isn't a true toplevel object
if sub_obj in self.xrc_objects:
del self.xrc_objects[sub_obj]
# let's see if sub_obj's parent already has an XrcObject: if so, it is
# temporairly stored in the self.xrc_objects dict...
if top_obj in self.xrc_objects:
top_xrc = self.xrc_objects[top_obj]
else:
# ...otherwise, create it and store it in the self.xrc_objects dict
top_xrc = self.obj_builders.get(
top_obj.base, XRCCodeWriter.DefaultXrcObject)(top_obj)
top_obj.xrc = top_xrc
self.xrc_objects[top_obj] = top_xrc
top_obj.xrc.children.append(xrc_obj)
def add_sizeritem(self, unused, sizer, obj, option, flag, border):
"""\
Adds a sizeritem to the XRC tree. The first argument is unused.
"""
# what we need in XRC is not toplevel, but sub_obj's true parent
toplevel = obj.parent
top_xrc = toplevel.xrc
obj_xrc = obj.xrc
try:
sizer_xrc = sizer.xrc
except AttributeError:
# if the sizer has not an XrcObject yet, create it now
sizer_xrc = self.obj_builders.get(
sizer.base, XRCCodeWriter.DefaultXrcObject)(sizer)
sizer.xrc = sizer_xrc
# we now have to move the children from 'toplevel' to 'sizer'
index = top_xrc.children.index(obj_xrc)
if obj.klass == 'spacer':
w = obj.properties.get('width', '0')
h = obj.properties.get('height', '0')
obj_xrc = XRCCodeWriter.SpacerXrcObject(
'%s, %s' % (w, h),
str(option),
str(flag),
str(border)
)
sizer.xrc.children.append(obj_xrc)
else:
sizeritem_xrc = XRCCodeWriter.SizerItemXrcObject(
obj_xrc,
str(option),
str(flag),
str(border)
)
sizer.xrc.children.append(sizeritem_xrc)
del top_xrc.children[index]
def add_class(self, code_obj):
"""\
Add class behaves very differently for XRC output than for other
lanaguages (i.e. pyhton): since custom classes are not supported in
XRC, this has effect only for true toplevel widgets, i.e. frames and
dialogs. For other kinds of widgets, this is equivalent to add_object
"""
if not self.xrc_objects.has_key(code_obj):
builder = self.obj_builders.get(
code_obj.base,
XRCCodeWriter.DefaultXrcObject
)
xrc_obj = builder(code_obj)
code_obj.xrc = xrc_obj
# add the xrc_obj to the dict of the toplevel ones
self.xrc_objects[code_obj] = xrc_obj
def _format_comment(self, msg):
return '<!-- %s -->' % escape(msg.rstrip())
# end of class XRCCodeWriter
writer = XRCCodeWriter()
"""\
The code writer is an instance of L{XRCCodeWriter}.
"""
language = writer.language
"""\
Language generated by this code generator
"""
|