#!/usr/bin/env jython

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

from pslogger import PSLogger


'''
A class encapsulating a ippToPsps configuration. This information is stored in the 'config' table
of the ipptopsps database, but there are some higher level config details in the 'settings.xml' file
'''
class Config(object):


    '''
    Constructor

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

        self.programName = programName
        self.name = name
        self.settingsPath = "../config/settings.xml" # TODO
        self.logger = None

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

        # this is the border (in degrees) that we place around any loading box of 
        # PS1 pointings to ensure we pull a large enough area out of DVO
        self.BORDER = 1.65
        self.isLoaded = False

    '''
    Prints everything for this config
    '''
    def printAll(self):

        self.logger.infoTitle("Config")

        try:
            self.logger.infoSeparator()
            self.logger.infoPair("Config name", self.name)
            self.logger.infoPair("Survey", self.survey)
            self.logger.infoPair("Publishing to PSPS as survey", self.pspsSurvey)
            self.logger.infoPair("Loading epoch", self.epoch)
            self.logger.infoPair("Data release", "%d" % self.dataRelease)
            for batchType in self.batchTypes: self.logger.infoPair("Queuing batch type", batchType)
        except:
            pass

        self.printDvoInfo()
        self.printDatastoreInfo()
        self.printBoxCoords()
        self.printDeletionPolicy()
        try: self.logger.infoSeparator()
        except: pass

    '''
    Queuing this batch type?
    '''
    def queuingThisBatchType(self, batchType):
       if batchType in self.batchTypes: return 1
       return 0

    '''
    Creates a logger object and returns it
    '''
    def getLogger(self, host, pid, stdout=1, sendToFile=0):
        
        logging.setLoggerClass(PSLogger)
        self.logger = logging.getLogger(self.programName)
        self.logger.setup(self.programName, self.logPath, self.name, host, pid, stdout, sendToFile)

        return self.logger

    '''
    Prints the currently set DVO info
    '''
    def printDvoInfo(self):

        try:
            self.logger.infoPair("DVO label", self.dvoLabel)
            self.logger.infoPair("DVO location", self.dvoLocation)
        except:
            pass
   
    '''
    Prints datastore info
    '''
    def printDatastoreInfo(self):

        try:
            self.logger.infoBool("Datastore publishing?", self.datastorePublishing)
            if self.datastorePublishing:
                self.logger.infoPair("Datastore type", self.datastoreProduct)
                self.logger.infoPair("Datastore product", self.datastoreType)
        except:
            pass
   
    '''
    Prints the current deletion policy
    '''
    def printDeletionPolicy(self):

        try:
            self.logger.infoBool("Deleting local?", self.deleteLocal)
            self.logger.infoBool("Deleting datastore?", self.deleteDatastore)
            self.logger.infoBool("Deleting DXLayer?", self.deleteDxLayer)
        except:
            pass
   
    '''
    Prints the currently set RA/Dec bounding box
    '''
    def printBoxCoords(self):

        try:
            self.logger.infoPair("RA limits", "%.1f -> %.1f" % (self.minRa, self.maxRa))
            self.logger.infoPair("Dec limits", "%.1f -> %.1f" % (self.minDec, self.maxDec))
            self.logger.infoPair("Loading box size", "%.1f degrees" % self.boxSize)
        except:
            pass
   

    '''
    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

