# -*- coding: utf-8 -*-

# Copyright (c) 2003 Detlev Offenbach <detlev@die-offenbachs.de>
#

"""
Module implementing the handler class for reading an XML project file.
"""

import codecs
import sys
import os
from types import UnicodeType

from xml.sax.handler import ContentHandler

class ProjectHandler(ContentHandler):
    """
    Class implementing a sax handler to read an XML project file.
    """
    def __init__(self, project):
        self.elements = {
            'Description' : (self.defaultStartElement, self.endDescription),
            'Version' : (self.defaultStartElement, self.endVersion),
            'Author' : (self.defaultStartElement, self.endAuthor),
            'Email' : (self.defaultStartElement, self.endEmail),
            'VcsType' : (self.defaultStartElement, self.endVcsType),
            'VcsOptions' : (self.defaultStartElement, self.endVcsOptions),
            'HappyDocParams' : (self.defaultStartElement, self.endHappyDocParams),
            'Dir' : (self.defaultStartElement, self.endDir),
            'Name' : (self.defaultStartElement, self.endName),
            'Source' : (self.startSource, self.endSource),
            'Form' : (self.startForm, self.endForm),
            'Translation' : (self.startTranslation, self.endTranslation),
            'Interface' : (self.startInterface, self.endInterface),
            'Other' : (self.startOther, self.endOther),
            'MainScript' : (self.startMainScript, self.endMainScript),
        }
    
        self.project = project
        self.encoder = codecs.lookup(sys.getdefaultencoding())[0] # encode,decode,reader,writer
        
    def utf8_to_code(self, text):
        if type(text) is not UnicodeType:
            text = unicode(text, "utf-8")
        return self.encoder(text)[0] # result,size
        
    def startDocument(self):
        self.buffer = ""
        self.pathStack = []
        
    def startElement(self, name, attrs):
        try:
            self.elements[name][0](attrs)
        except KeyError:
            pass
            
    def endElement(self, name):
        try:
            self.elements[name][1]()
        except KeyError:
            pass
            
    def characters(self, chars):
        self.buffer += chars
        
    ###################################################
    ## below follow the individual handler functions
    ###################################################
    
    def defaultStartElement(self, attrs):
        self.buffer = ""
        
    def endDescription(self):
        self.buffer = self.utf8_to_code(self.buffer)
        self.project.pdata["DESCRIPTION"] = self.buffer.split(os.linesep)
        
    def endVersion(self):
        self.buffer = self.utf8_to_code(self.buffer)
        self.project.pdata["VERSION"] = [self.buffer]
        
    def endAuthor(self):
        self.buffer = self.utf8_to_code(self.buffer)
        self.project.pdata["AUTHOR"] = [self.buffer]
        
    def endEmail(self):
        self.buffer = self.utf8_to_code(self.buffer)
        self.project.pdata["EMAIL"] = [self.buffer]
        
    def endVcsType(self):
        self.buffer = self.utf8_to_code(self.buffer)
        self.project.pdata["VCS"] = [self.buffer]
        
    def endVcsOptions(self):
        self.buffer = self.utf8_to_code(self.buffer)
        self.project.pdata["VCSOPTIONS"] = [self.buffer]
        
    def endHappyDocParams(self):
        self.buffer = self.utf8_to_code(self.buffer)
        self.project.pdata["HAPPYDOCPARMS"] = [self.buffer]
        
    def endDir(self):
        self.buffer = self.utf8_to_code(self.buffer)
        self.pathStack.append(self.buffer)
        
    def endName(self):
        self.buffer = self.utf8_to_code(self.buffer)
        self.pathStack.append(self.buffer)
        
    def startSource(self, attrs):
        self.pathStack = []
        
    def endSource(self):
        path = ""
        for p in self.pathStack:
            path = os.path.join(path, p)
        self.project.pdata["SOURCES"].append(path)
        
    def startForm(self, attrs):
        self.pathStack = []
        
    def endForm(self):
        path = ""
        for p in self.pathStack:
            path = os.path.join(path, p)
        self.project.pdata["FORMS"].append(path)
        
    def startTranslation(self, attrs):
        self.pathStack = []
        
    def endTranslation(self):
        path = ""
        for p in self.pathStack:
            path = os.path.join(path, p)
        self.project.pdata["TRANSLATIONS"].append(path)
        
    def startInterface(self, attrs):
        self.pathStack = []
        
    def endInterface(self):
        path = ""
        for p in self.pathStack:
            path = os.path.join(path, p)
        self.project.pdata["INTERFACES"].append(path)
        
    def startOther(self, attrs):
        self.pathStack = []
        
    def endOther(self):
        path = ""
        for p in self.pathStack:
            path = os.path.join(path, p)
        self.project.pdata["OTHERS"].append(path)
        
    def startMainScript(self, attrs):
        self.pathStack = []
        
    def endMainScript(self):
        path = ""
        for p in self.pathStack:
            path = os.path.join(path, p)
        self.project.pdata["MAINSCRIPT"] = [path]
