#!/usr/bin/env jython

import sys
import os
import logging
from xml.etree.ElementTree import ElementTree, Element, tostring

from pslogger import PSLogger

'''
A class encapsulating the globald ippToPsps configuration information.
This is stored in the 'settings.xml' file in the config directory
'''
class Config(object):

    '''
    Constructor

    Basically reads the entire config and stores values to class variables
    '''
    def __init__(self):

        self.test = False
        for arg in sys.argv:
            if arg == "-test": 
                self.test = True
                sys.argv.remove(arg)
            if arg == "-t": 
                self.test = True
                sys.argv.remove(arg)

        ## name of the top-level jython script
        self.programName = os.path.basename(sys.argv[0])

        ## this is somewhat crude: ipptopsps programs are called like this: 
        ## ippjython foo.py [chunk] [other options]
        ## the logging code (called below by getLogger) wants to include 'name' in the 
        ## output file.  we blindly set name == argv[1] 
        ## programs which are called without argv[1] will use 'none' in the log file
        if len(sys.argv) >= 2:
            self.name = sys.argv[1]

        # XXX this probably goes in 'config.py'
        self.configDir = os.getenv("IPPTOPSPS_DATA")
        if self.configDir is None:
            self.configDir = "../config/"
        else:
            self.configDir = self.configDir + "/"

        # self.configdir is set in ipptopsps.jy on init based on env(IPPTOPSPS_DATA)
        # for test purposes, an uninstalled system may use the config information from 
        # a relative path
        self.settingsPath = self.configDir + "settings.xml"

        self.logger = None

        self.settingsDoc = ElementTree(file=self.settingsPath)
        self.logPath = self.settingsDoc.find("logPath").text

        print "config.programName: ", self.programName
        print "config.configDir: ", self.configDir
        print "config.settingsPath: ", self.settingsPath
        print "config.logPath: ", self.logPath
        print "config.test: ", self.test

    '''
    Creates a logger object and returns it
    '''
    def getLogger(self, host, pid, stdout=1, sendToFile=0):
        
        print "get Logger: ", host, pid
        logging.setLoggerClass(PSLogger)
        print "done Logger 1"
        self.logger = logging.getLogger(self.programName)
        print "done Logger 2"
        if (self.name is None):
            self.logger.setup(self.programName, self.logPath, "none", host, pid, stdout, sendToFile)
        else:
            self.logger.setup(self.programName, self.logPath, self.name, host, pid, stdout, sendToFile)
        print "done Logger 3"

        return self.logger

    '''
    Various db metadata getters
    '''
    def getDbName(self, dbType): return self.settingsDoc.find(dbType +"/name").text
    def getDbHost(self, dbType): return self.settingsDoc.find(dbType +"/host").text
    def getDbUser(self, dbType): return self.settingsDoc.find(dbType +"/user").text
    def getDbPassword(self, dbType): return self.settingsDoc.find(dbType +"/password").text
