import libxml2 #;libxml2.debugMemory(1)
import libxslt

import jon.cgi as cgi

import Handler
from Handler import PATH_XML_DOCS
#from Handler import PATH_XSL_DOCS
import jon.modpy as modpy


class HostHandler(Handler.Handler):
    
    _adminPage = 'host.xsl'
    _roPage = 'rohost.xsl'
    
    def setParam(self, req, key):
        nvDict = {}
        temp = key[len('xpath:'):].split('?')
        xpath = temp[0]
        if len(temp) == 2:
            args = temp[1].split('+')
            for nvstr in args:
                nv = nvstr.split('=')
                nvDict[nv[0]] = nv[1]
            #for k, v in nvDict.items():
            #    self.htmlComment(req, 'arg %s has value %s' % (k, v))
        val = req.params[key]

        nodes = self._muxconfig.xpathEval2(xpath);
        if len(nodes) >= 1:
            nodes[0].setContent(val)
        elif nvDict.has_key('create') and nvDict['create'] == '1':
            nodes = [self.xpathCreate(xpath)]
            if len(nodes) >= 1:
                nodes[0].setContent(val)
        else:
            req.error('xpath does not exist: %s\n' % (xpath))
    def apply(self, req):
        if req.params.has_key('xpath:/configuration/network/gateway?create=1'):
            # remove any old gateway nodes
            for node in self._muxconfig.xpathEval2('configuration/network/interface/gateway'):
                node.unlinkNode()
                node.freeNode()
        for key in req.params:
            if key[:len('xpath:')] == 'xpath:':
                self.setParam(req, key)

        if req.params.has_key('unit_name'):
            for node in self._muxconfig.xpathEval2('configuration/user_data/unit_name'):
                node.setContent(req.params['unit_name'])
        if req.params.has_key('use_dhcp'):
            if (req.params['use_dhcp'] == 'No'):
                for node in self._muxconfig.xpathEval2('configuration/network/interface/use_dhcp'):
                    node.setContent('No')
            else:
                for node in self._muxconfig.xpathEval2('configuration/network/interface/use_dhcp'):
                    node.setContent('Yes')
#        if req.params.has_key('inet_addr'):
#            for node in self._muxconfig.xpathEval2('configuration/network/interface/inet_addr'):
#                node.setContent(req.params['inet_addr'])
#        if req.params.has_key('mask'):
#            for node in self._muxconfig.xpathEval2('configuration/network/interface/mask'):
#                node.setContent(req.params['mask'])
#        if req.params.has_key('gateway'):
#            for node in self._muxconfig.xpathEval2('configuration/network/gateway'):
#                node.setContent(req.params['gateway'])
        if (req.params.has_key('enable_logging')):
            if (req.params['enable_logging'] == 'Yes'):
                for node in self._muxconfig.xpathEval2('configuration/network/ip_logging/enable_logging'):
                    node.setContent('Yes')
            else:
                for node in self._muxconfig.xpathEval2('configuration/network/ip_logging/enable_logging'):
                    node.setContent('No')
        if req.params.has_key('remote_host'):
            for node in self._muxconfig.xpathEval2('configuration/network/ip_logging/remote_host'):
                node.setContent(req.params['remote_host'])
        if req.params.has_key('server_user'):
            for node in self._muxconfig.xpathEval2('configuration/network/server_user'):
                node.setContent(req.params['server_user'])
        if req.params.has_key('server_pw'):
            for node in self._muxconfig.xpathEval2('configuration/network/server_pw'):
                node.setContent(req.params['server_pw'])
        self._muxconfig.saveFormatFile(PATH_XML_DOCS + self._configFile, 1)
        self._muxconfig.saveFormatFile(PATH_XML_DOCS + 'webref.xml', 1)
        #self._muxconfig.saveFile(PATH_XML_DOCS + self._configFile)
        self._message = 'Apply successful'
        self.view(req)

      
#cgi.CGIRequest(HostHandler).process()
def handler(modpy_request):
    return modpy.Request(HostHandler).process(modpy_request)
