""" To use the Options system: 1. Create an OptionGroup: options = OptionGroup('MyProg', 'Options') You can also use the handy rox.setup_app_options() in most applications. 2. Create the options: colour = Option('colour', 'red', options) size = Option('size', 3, options) icons = ListOption('icons', ('circle', 'square', 'triangle'), options) 3. Register any callbacks (notification of options changing): def my_callback(): if colour.has_changed: print "The colour is now", colour.value options.add_notify(my_callback) 4. Notify any changes from defaults: options.notify() See OptionsBox for editing options. Do not change the value of options yourself. """ from __future__ import generators import os import rox from rox import choices, basedir from xml.dom import Node, minidom def data(node): """Return all the text directly inside this DOM Node.""" return ''.join([text.nodeValue for text in node.childNodes if text.nodeType == Node.TEXT_NODE]) class Option: """An Option stores a single value. Every option is part of exactly one OptionGroup. The read-only attributes value and int_value can be used to get the current setting for the Option. int_value will be -1 if the value is not a valid integer. The has_changed attribute is used during notify() calls to indicate whether this Option's value has changed since the last notify (or option creation). You may set has_changed = 1 right after creating an option if you want to force notification the first time even if the default is used. """ def __init__(self, name, value, group = None): """Create a new option with this name and default value. Add to 'group', or to rox.app_options if no group is given. The value cannot be used until the first notify() call to the group.""" if not group: assert rox.app_options group = rox.app_options self.name = name self.has_changed = 0 # ... since last notify/default self.default_value = str(value) self.group = group self.value = None self.int_value = None self.store = True self.group._register(self) def _set(self, value): if self.value != value: self.value = str(value) self.has_changed = 1 try: if self.value == 'True': self.int_value = 1 elif self.value == 'False': self.int_value = 0 else: self.int_value = int(float(self.value)) except: self.int_value = -1 def _to_xml(self, parent): doc = parent.ownerDocument node = doc.createElement('Option') node.setAttribute('name', self.name) node.appendChild(doc.createTextNode(self.value)) parent.appendChild(node) def __str__(self): return "