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 InputsHandler(Handler.Handler):
    _adminPage = 'inputs.xsl'
    _roPage = 'roinputs.xsl'
    
#    def apply(self, req):
#        muxconfig = libxml2.parseFile(PATH_XML_DOCS + 'asimux.xml')
##        if form.has_key('mode'):
##            print form['mode'].value
##        if form.has_key('port_index'):
##            print form['port_index'].value
#        options = req.params.getlist('current_programs')
#        add_list = []
#
#        # parse new items for new adds: program number, provider, name
#        for opt in options:
#            idx1 = opt.find(',')
#            idx2 = opt.find(',', idx1+1)
#            program_number = opt[:idx1]
#            #print 'program_number =', program_number, '<br>'
#            provider = opt[idx1+1:idx2]
#            #print 'provider =', provider, '<br>'
#            name = opt[idx2+1:]
#            #print 'name =', name, '<br>'
#            #  store options as tuples
#            add_list.append((program_number, provider, name))
#
#        # ignore current programs if port(@index==n)/mode == 'Auto' or 'Disabled'
#        # set content of mode to req.params['mode'].value
#        ignore_current_programs = 0
#        for port in muxconfig.xpathEval2('configuration/port'):
#            if (port.prop('index') == req.params['port_index'].value):
#                if req.params.has_key('mode'):
#                    mode = port.xpathEval2('mode')[0]
#                    ignore_current_programs = ((mode.content == 'Auto') or (mode.content == 'Disabled'))
#                    mode.setContent(req.params['mode'].value)
#                if req.params.has_key('redundancy_group'):
#                    redundancy = port.xpathEval2('redundancy/group')[0]
#                    redundancy.setContent(req.params['redundancy_group'].value)
#                if req.params.has_key('clsm_group'):
#                    clsm_group = port.xpathEval2('clsm/group')[0]
#                    clsm_group.setContent(req.params['clsm_group'].value)
#                if req.params.has_key('clsm_encoder_inet_addr'):
#                    encoder_inet_addr = port.xpathEval2('clsm/encoder_inet_addr')[0]
#                    encoder_inet_addr.setContent(req.params['clsm_encoder_inet_addr'].value)
#                if (ignore_current_programs == 0):
#                    # remove all port(@index == n)/manual_programs/add
#                    for add in port.xpathEval2('manual_programs/add'):
#                        add.unlinkNode()
#                        add.freeNode()
#                    ret = port.xpathEval2('manual_programs')
#                    if len(ret) > 0:
#                        man_progs = ret[0]
#                    else:
#                        man_progs = port.newChild(None, 'manual_programs', None)
#                    # add new items as .../manual_programs/add
#                    for item in add_list:
#                        temp = man_progs.newChild(None, 'add', '\n')
#                        temp.newProp('program_number_in', item[0])
#                        #temp.newProp('program_number_out', item[0])
#                        #temp.newProp('provider_out', item[1])
#                        #temp.newProp('name_out', item[2])
#                        #temp.shellPrintNode()
#                # found the proper port, so stop searching
#                break
#        # save config
#        muxconfig.saveFile(PATH_XML_DOCS + 'asimux.xml')
#        muxconfig.freeDoc()
#        muxconfig = None
#        libxml2.cleanupParser()
#        link = '../cgi-bin/input%sview.py' % (req.params['port_index'].value)
#        print MESSAGE_FORMAT_PAGE % (link,'Success')


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