Source

Fat x Fast / FatxFast / gui / widgetparser.py

Full commit
# This file is part of FatxFast.
#
#    FatxFast is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    FatxFast is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with FatxFast.  If not, see <http://www.gnu.org/licenses/>.

# Versioning based on: 
# http://en.wikipedia.org/wiki/Versioning#Designating_development_stage
__author__ = "dryatu (c) 2013"
__version__ = "1.2.5"


import FatxFast.utils.xmlparser as xmlparser
from FatxFast.gui import (menuwidget, boxwidget, 
    textwidget, buttonwidget, linkwidget, containerwidget)
from FatxFast.scenes.basescene import Scene

class SceneLoadError(Exception): pass

class SceneParser(xmlparser.XmlParser):
    
    def __init__(self, filename="widgets.xml"):
        super(SceneParser, self).__init__(filename)
        self.scenes = {}
        self.templates = {}
        self._parse_templates()
        self.caller = None
        self.scenemanager = None
        
    def _create_composite_hierarchy(self, parent, node):
        for element in node:
            try:
                getattr(self, "_parse_"+element.tag)(parent, element)
            except AttributeError:
                getattr(self, "_parse_widget")(parent, element)

    def get_widgets(self, caller=None, source=None, scenemanager=None):
        self.caller = caller
        self.scenemanager = scenemanager
        if source is None:
            source = caller.__class__.__name__.lower()
        if isinstance(caller, Scene):
            self.get_scene(caller, source)
        else:
            node = self.root.find(source)
            #...
            
            
    def get_scene(self, caller, scenename):
        scenes = {scene.tag: scene for scene in self.root.find("scenes")}
        try:
            scene = scenes[scenename]
        except KeyError:
            raise SceneLoadError(str(scenename))
        else:
            caller.widgets = {}
            self._create_composite_hierarchy(caller, scene)
            
    def override_attributes(self, templ_attrib, overrides):
        merged_attributes = templ_attrib.copy()
        for attribute in overrides:
            if overrides[attribute] == "none":
                del merged_attributes[attribute]
            else:
                merged_attributes[attribute] = overrides[attribute]
        """try:
            merged_attributes["text"] = overrides["text"]
        except KeyError:
            pass"""
        return merged_attributes

    def _parse_properties(self, parent, node):
        property_dict = {}
        try:
            for prop in node.findall("property"):
                property_dict[prop.attrib["key"]] = prop.attrib["value"]
        except AttributeError:
            pass
        parent.properties =  property_dict
        
    def _parse_templates(self):
        for child in self.root.find("templates"):
            self.templates[child.tag] = self._get_attributes(child)
    
    def _parse_text(self, parent, node):
        try:
            parent.text = node.text
        except AttributeError:
            parent.text =  ""
        
    def _parse_widget(self, parent, node):
        widgettypes = {"boxwidget":boxwidget.BoxWidget,
                       "menuwidget":menuwidget.MenuWidget,
                       "textwidget":textwidget.TextWidget,
                       "buttonwidget":buttonwidget.ButtonWidget,
                       "linkwidget":linkwidget.LinkWidget,
                       "scenelinkwidget":linkwidget.SceneLinkWidget,
                       "backlinkwidget":linkwidget.BackLinkWidget
                       }
        
        overrides = self._get_attributes(node)
        tempwid_attributes = self.override_attributes(
                                        self.templates[node.tag], overrides)
        
        if isinstance(parent, containerwidget.ContainerWidget):
            tempwid_attributes["container"] = parent
            
        widget = widgettypes[self.templates[node.tag]["type"]]
        tempwid = widget(**tempwid_attributes)
        
        if isinstance(tempwid, linkwidget.SceneLinkWidget): 
            tempwid.connect_obj = self.scenemanager
        else:    
            tempwid.connect_obj = self.caller

        parent.add_widget(tempwid.name, tempwid)
        self._create_composite_hierarchy(tempwid, node)