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 MESSAGE_FORMAT_PAGE


class OutputsHandler(Handler.Handler):

    _adminPage = 'outputs.xsl'
    _roPage = 'rooutputs.xsl'
    
    def apply(self, req):
        if req.params.has_key('egress'):
            #print form['egress'].value
            if req.params['egress'] == 'smpte':
                for node in self._muxconfig.xpathEval2('configuration/egress/smpte310/output'):
                    node.setContent('On')
            elif req.params['egress'] == 'off':
                req.params['clock_mode'] = 'Off'
                for node in self._muxconfig.xpathEval2('configuration/egress/smpte310/output'):
                    node.setContent('Off')
            else:
                if req.params['clock_mode'] == 'Off':
                    req.params['clock_mode'] = 'Burst'
                for node in self._muxconfig.xpathEval2('configuration/egress/smpte310/output'):
                    node.setContent('Off')
	if req.params.has_key('format'):
	    for node in self._muxconfig.xpathEval2('configuration/audio/format'):
		node.setContent(req.params['format'])        
	if req.params.has_key('framing_mode'):
            for node in self._muxconfig.xpathEval2('configuration/egress/asi/framing_mode'):
                node.setContent(req.params['framing_mode'])
        if req.params.has_key('clock_mode'):
            for node in self._muxconfig.xpathEval2('configuration/egress/asi/clock_mode'):
                node.setContent(req.params['clock_mode'])
        if req.params.has_key('manual_rate'):
            for node in self._muxconfig.xpathEval2('configuration/egress/asi/egress_clock_rate'):
                node.setContent(req.params['manual_rate'])
        if req.params.has_key('modulation_target'):
            for node in self._muxconfig.xpathEval2('configuration/egress/asi/modulation_target'):
                node.setContent(req.params['modulation_target'])
        if req.params.has_key('qpsk_viterbi'):
            for node in self._muxconfig.xpathEval2('configuration/egress/asi/qpsk_viterbi'):
                node.setContent(req.params['qpsk_viterbi'])
        #if req.params.has_key('qpsk_symbol_rate'):
        #    for node in self._muxconfig.xpathEval2('configuration/egress/asi/qpsk_symbol_rate'):
        #        node.setContent(req.params['qpsk_symbol_rate'])
        self._muxconfig.saveFormatFile(PATH_XML_DOCS + self._configFile, 1)
        self._muxconfig.saveFormatFile(PATH_XML_DOCS + 'webref.xml', 1)
        #req.set_header('Content-Type', 'text/html')
        #req.write(MESSAGE_FORMAT_PAGE % ('outputs.py','Success'))
        self._message = 'Apply successful'
        self.view(req)
    #    print '<body><a href="../cgi-bin/outputsview.py">Success</a></body>'

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