configparser.py :  » Development » Kahakai-Window-Manager » kahakai-0.6.2 » data » scripts » python » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » Kahakai Window Manager 
Kahakai Window Manager » kahakai 0.6.2 » data » scripts » python » configparser.py
# $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)
                                           
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.