# $Id: configparser.py,v 1.5 2003/09/08 05:29:53 pervago Exp $
import xml.dom.minidom
import os
class ConfigurationParser(object):
def parse(self):
dom = xml.dom.minidom.parse(self.configurationFileName())
root = dom.getElementsByTagName('configuration')[0]
self._settings = {}
self._bindings = {}
self._handleSettings(dom.getElementsByTagName('settings')[0])
self._handleBindings(dom.getElementsByTagName('bindings')[0])
dom.unlink()
def configurationFileName(self):
return "%s/.kahakai/configuration.xml" % os.getenv('HOME')
def _handleSettings(self, root):
for setting in root.childNodes:
if setting.nodeType != setting.TEXT_NODE:
self._settings[setting.nodeName] = self._getText(setting)
def _handleBindings(self, root):
targets = root.getElementsByTagName('target')
for target in targets:
self._handleTarget(target)
def _handleTarget(self, target):
targetName = target.getAttribute('id')
self._bindings[targetName] = []
bindings = target.getElementsByTagName('binding')
for binding in bindings:
self._handleBinding(targetName, binding)
def _getText(self, node):
rc = ""
for node in node.childNodes:
if node.nodeType == node.TEXT_NODE:
rc = rc + node.data
return rc
def getBindings(self):
return self._bindings
def getSettings(self):
return self._settings
def _handleBinding(self, targetName, binding):
eventParent = binding.getElementsByTagName('event')[0]
event = None
for child in eventParent.childNodes:
if child.nodeType != child.TEXT_NODE:
event = child
break
action = self._getText(binding.getElementsByTagName('action')[0])
self._bindings[targetName].append(self._parseEvent(event) + (action,))
def _parseEvent(self, eventNode):
eventType = eventNode.nodeName
params = {}
for param in eventNode.childNodes:
if param.nodeType != param.TEXT_NODE:
if param.nodeName in ('modifier', 'notModifier'):
params.setdefault(param.nodeName, []).append(
self._getText(param))
else:
params[param.nodeName] = self._getText(param)
return (eventType, params)
|