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

import jon.cgi as cgi
import jon.modpy as modpy

import Handler
from Handler import PATH_XML_DOCS
from Handler import PATH_XSL_DOCS
from Handler import REFRESH_MESSAGE


class AdvCaHandler(Handler.Handler):

    _adminPage = 'ca.xsl'
    _roPage = 'roca.xsl'

    """
    If new fields are added the the HTML form, just add an entry in 
    the _ApplyMap with the format - FieldName: (xpathStr, createBool), -
    """
    #_ApplyMap = {
    #    # FieldName: (xpathStr, createBool);
    #    'configuration/ca[@index=1]/enable':('/configuration/ca[@index=1]/enable',0),
    #    'configuration/ca[@index=2]/enable':('/configuration/ca[@index=2]/enable',0),
    #    'configuration/ca[@index=3]/enable':('/configuration/ca[@index=3]/enable',0),
    #    'configuration/ca[@index=4]/enable':('/configuration/ca[@index=4]/enable',0),
    #    'configuration/ca[@index=5]/enable':('/configuration/ca[@index=5]/enable',0),
    #    }
        
    #def setParam(self, req, xpath):
    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':
            #self.htmlComment(req, 'creating %s, %s (%g)' % (xpath, val, time.time() - ts))
            nodes = [self.xpathCreate(xpath)]
            if len(nodes) >= 1:
                nodes[0].setContent(val)
        #elif create:
        #    nodes = [self.createXpath(req, xpath)]
        #    if len(nodes) >= 1:
        #        nodes[0].setContent(req.params[key])
        else:
            req.error('xpath does not exist: %s\n' % (xpath))
            
    def apply(self, req):
#        del req.params['submit.x']
#        del req.params['submit.y']
#        del req.params['action']
        for key in req.params:
            if key[:len('xpath:')] == 'xpath:':
                self.setParam(req, key)
            #xpath, val, kwargs = self.processParam(req, key)
            #self.xpathSet(req, xpath, val, **kwargs)

        #if req.params.has_key('configuration/ca/enable'):
        #    for node in self._muxconfig.xpathEval2('configuration/ca/enable'):
        #        node.setContent(req.params['configuration/ca/enable'])
        #if req.params.has_key('configuration/ca/ecmg/inet_addr'):
        #    for node in self._muxconfig.xpathEval2('configuration/ca/ecmg/inet_addr'):
        #        node.setContent(req.params['configuration/ca/ecmg/inet_addr'])
        #if req.params.has_key('configuration/ca/emmg/inet_addr'):
        #    for node in self._muxconfig.xpathEval2('configuration/ca/emmg/inet_addr'):
        #        node.setContent(req.params['configuration/ca/emmg/inet_addr'])

        self._muxconfig.saveFormatFile(PATH_XML_DOCS + self._configFile, 1)
        self._muxconfig.saveFormatFile(PATH_XML_DOCS + 'webref.xml', 1)
        #self._muxconfig.saveFile(PATH_XML_DOCS + 'asimux.xml')
        self._message = 'Apply successful'
        self.view(req)       

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