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


class AdvMpeHandler(Handler.Handler):

    _adminPage = 'mpe.xsl'
    _roPage = 'rompe.xsl'
    
    def apply(self, req):
        #req.set_header('Content-Type', 'text/plain')
        if (req.params.has_key('route_index') and \
            req.params['route_index'] != ''): #and 
            if len(self._muxconfig.xpathEval2("/configuration/mpe/current_routes/route[@index='%s']" % (req.params['route_index']))) < 1:
                if len(self._muxconfig.xpathEval2('/configuration/mpe/current_routes')) < 1:
                    mpe = self._muxconfig.xpathEval2('/configuration/mpe')[0]
                    routes = mpe.newChild(None, 'current_routes', None)
                else:
                    routes = self._muxconfig.xpathEval2('/configuration/mpe/current_routes')[0]
                route = routes.newChild(None, 'route', None)
                route.newProp('index', req.params['route_index'])
            else:
                route = self._muxconfig.xpathEval2("/configuration/mpe/current_routes/route[@index='%s']" % (req.params['route_index']))[0]
            #req.write('route_index=' + req.params['route_index'] + '\n')
            if (req.params.has_key('name') and \
                req.params['name'] != ''): #and 
                #for node in self._muxconfig.xpathEval2('/configuration/mpe/current_routes/route[@index=%s]/name' %
                #                                       (req.params['route_index'])):
                #    #req.write('name=' + req.params['name'] + '\n')
                #    node.setContent(req.params['name'])
                if len(route.xpathEval2('name')) < 1:
                    name = route.newChild(None, 'name', req.params['name'])
                else:
                    name = route.xpathEval2('name')[0]  
                    name.setContent(req.params['name'])
            if (req.params.has_key('inet_addr') and \
                req.params['inet_addr'] != ''): #and 
                #for node in self._muxconfig.xpathEval2('/configuration/mpe/current_routes/route[@index=%s]/inet_addr' %
                #                                       (req.params['route_index'])):
                #    #req.write('inet_addr=' + req.params['inet_addr'] + '\n')
                #    node.setContent(req.params['inet_addr'])
                if len(route.xpathEval2('inet_addr')) < 1:
                    inet_addr = route.newChild(None, 'inet_addr', req.params['inet_addr'])
                else:
                    inet_addr = route.xpathEval2('inet_addr')[0]  
                    inet_addr.setContent(req.params['inet_addr'])
            if (req.params.has_key('inet_port') and \
                req.params['inet_port'] != ''): #and 
                #for node in self._muxconfig.xpathEval2('/configuration/mpe/current_routes/route[@index=%s]/port' %
                #                                       (req.params['route_index'])):
                #    #req.write('inet_port=' + req.params['inet_port'] + '\n')
                #    node.setContent(req.params['inet_port'])
                if len(route.xpathEval2('inet_port')) < 1:
                    inet_port = route.newChild(None, 'inet_port', req.params['inet_port'])
                else:
                    inet_port = route.xpathEval2('inet_port')[0]  
                    inet_port.setContent(req.params['inet_port'])
            if (req.params.has_key('pid') and \
                req.params['pid'] != ''): #and 
                #for node in self._muxconfig.xpathEval2('/configuration/mpe/current_routes/route[@index=%s]/pid' %
                #                                       (req.params['route_index'])):
                #    #req.write('pid=' + req.params['pid'] + '\n')
                #    node.setContent(req.params['pid'])
                if len(route.xpathEval2('pid')) < 1:
                    pid = route.newChild(None, 'pid', req.params['pid'])
                else:
                    pid = route.xpathEval2('pid')[0]  
                    pid.setContent(req.params['pid'])
            if (req.params.has_key('enabled') and \
                req.params['enabled'] != ''): #and 
                #for node in self._muxconfig.xpathEval2('/configuration/mpe/current_routes/route[@index=%s]/enabled' %
                #                                       (req.params['route_index'])):
                #    #req.write('enabled=' + req.params['enabled'] + '\n')
                #    node.setContent(req.params['enabled'])
                if len(route.xpathEval2('enabled')) < 1:
                    enabled = route.newChild(None, 'enabled', req.params['enabled'])
                else:
                    enabled = route.xpathEval2('enabled')[0]  
                    enabled.setContent(req.params['enabled'])
            if (req.params.has_key('max_rate') and \
                req.params['max_rate'] != ''): #and 
                #for node in self._muxconfig.xpathEval2('/configuration/mpe/current_routes/route[@index=%s]/enabled' %
                #                                       (req.params['route_index'])):
                #    #req.write('enabled=' + req.params['enabled'] + '\n')
                #    node.setContent(req.params['enabled'])
                if len(route.xpathEval2('max_rate')) < 1:
                    max_rate = route.newChild(None, 'max_rate', req.params['max_rate'])
                    max_rate.newProp('units', 'bps')
                else:
                    max_rate = route.xpathEval2('max_rate')[0]  
                    max_rate.setContent(req.params['max_rate'])
                    #max_rate.prop('units') = 'bps'
            self._muxconfig.saveFormatFile(PATH_XML_DOCS + self._configFile, 1)
            self._muxconfig.saveFormatFile(PATH_XML_DOCS + 'webref.xml', 1)
            self._message = 'Apply successful'
        self.view(req)
          
#cgi.CGIRequest(AdvMpeHandler).process()
def handler(modpy_request):
    return modpy.Request(AdvMpeHandler).process(modpy_request)
    