import time
import libxml2 #;libxml2.debugMemory(1)
import libxslt
import string 
import jon.cgi as cgi
#import jon.session as session
import jon.modpy as modpy
#from mod_python import apache

import Handler
from Handler import PATH_XML_DOCS
#from Handler import PATH_XSL_DOCS
from Handler import MESSAGE_FORMAT_PAGE

#PATH_XML_DOCS = '/etc/'
##PATH_XML_DOCS = '../htdocs/xmlxsldocs/'
#PATH_XSL_DOCS = '../htdocs/xmlxsldocs/'

class SystemHandler(Handler.Handler):
    _adminPage = 'security.xsl'
    _roPage = 'rosecurity.xsl'

#    def process(self, req):
#    req.set_header("Content-Type", "text/plain")    
#    req.write("hello\n")
#    self._muxconfig = libxml2.parseFile('/etc/asimux/asimux.xml')
#    #self.view(req)
#    req.write(str(self._muxconfig))
    
    def apply(self, req):
        req.set_header('Content-Type', 'text/html')
    #    req.write('Under Construction')
        Error = 0       #Error tracking variable
        PassError = 1   #pass error
        self._session.setDefaultBase(req.params["defaultBase"])
        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('time_out'):
            for node in self._muxconfig.xpathEval2('configuration/user_data/auto_logout_time'):
                node.setContent(req.params['time_out'])
            self._muxconfig.saveFormatFile(PATH_XML_DOCS + self._configFile, 1)
            self._muxconfig.saveFormatFile(PATH_XML_DOCS + 'webref.xml', 1)
            if (req.params['admin_pw1'] == req.params['admin_pw2']):
                for node in self._muxconfig.xpathEval2('configuration/user_data/admin_pw'):
                    node.setContent(req.params['admin_pw1'])
                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)
                #print MESSAGE_FORMAT_PAGE % ('system.py','Success')
            else:
                #print MESSAGE_FORMAT_PAGE % ('system.py','Passwords do not match')
                Error = PassError
            result={
                    PassError:'Passwords do not match',
                   }
            self._message = result.get(Error,'Apply Successful')
            self.view(req) 

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

