#
# -*- coding: utf-8 -*-

'''
$RCSfile: datasources.py,v $
$Revision: 898 $
$Author: markus $
$Date: 2008-04-29 18:28:55 +0200 (Di, 29. Apr 2008) $
General filesystem methods all webapps need.
'''

############################################################################################################
#
#   IMPORTS
#
#===========================================================================================================

import os, string, shutil
from types import *
from biocase.cmfinfo            import CMFListClass
from biocase.wrapper.psf        import ProviderSetup
from biocase.tools.caching      import getCachedDsaFile, getCachedFilename

import biocase.configuration
cfg = biocase.configuration.Cfg()

import logging
log=logging.getLogger("lib.datasource")

############################################################################################################
#
#   CLASSES
#
#===========================================================================================================
 
class Datasource:
    # ------------------------------------------------------------------------------------
    def __init__(self, name):
        self.name     = name
        self.psfObj   = None
        self.dbmodObj = None
        # temp references only created when needed at all
        self._cmfListObj   = None
        
    # ------------------------------------------------------------------------------------
    def __repr__(self):
        return "DSAobj:%s"%self.name
        
    # ------------------------------------------------------------------------------------
    def getDsaDir(self):
        return os.path.join(cfg.dsaLocator, self.name)

    # ------------------------------------------------------------------------------------
    def getSchemaListObj(self):
        if self._cmfListObj is None:
            self._cmfListObj = CMFListClass()
            self._cmfListObj.addSchemasFromDir( self.getDsaDir() )
        return self._cmfListObj

    # ------------------------------------------------------------------------------------
    def getSchemaNameList(self):
        slObj = self.getSchemaListObj()
        return [sObj.name for sObj in slObj]

    # ------------------------------------------------------------------------------------
    def getAbsPSFilePath(self):
        return os.path.join(cfg.dsaLocator, self.name, cfg.pywrapper.psfFile)

    # ------------------------------------------------------------------------------------
    def getAbsTempPSFilePath(self):
        psfTemp=getCachedDsaFile(cfg.pywrapper.psfFile[:-4]+'_tmpCfgTool.xml', self.name)
        return psfTemp

    # ------------------------------------------------------------------------------------
    def getPSFObj(self, tmp=False):
        '''Reads the PSF for a given datasource. If tmp is true read from the temporary PSF.'''
        if self.psfObj is None:
            if tmp:
                # read temporary PSF
                absFilePath = self.getAbsTempPSFilePath()
            else:
                # read real PSF
                absFilePath = self.getAbsPSFilePath()
            self.psfObj = ProviderSetup(dsaObj=self)
            self.psfObj.readPSFile(filename=absFilePath)
        return self.psfObj

    # ------------------------------------------------------------------------------------
    def getBioCASeAccessPoint(self):
        return cfg.getAccessPoint(self.name, "biocase")

    # ------------------------------------------------------------------------------------
    def getSPICEAccessPoint(self):
        return cfg.getAccessPoint(self.name, "spice")

    # ------------------------------------------------------------------------------------
    def getDbmodObj(self):
        if self.dbmodObj is None:
            from biocase.wrapper.operations import getOperationsObjectForDBMod
            try:
                self.dbmodObj = getOperationsObjectForDBMod(psfObj=self.getPSFObj())
            except:
                log.debug("Could not get operation module")
                self.dbmodObj = None
        return self.dbmodObj
        
    # ------------------------------------------------------------------------------------
    def getDBConnectionStatus(self):
        if self.getDbmodObj() is not None:
            return cfg.html.dbConnectionStatusGood
        else:
            return cfg.html.dbConnectionStatusBad


############################################################################################################
#
#   FUNCTIONS
#
#===========================================================================================================

# ------------------------------------------------------------------------------------
def getDsaList():
    dsalist = []
    for dsa in os.listdir(cfg.dsaLocator):
        if os.path.isdir(os.path.join(cfg.dsaLocator, dsa)) and os.path.isfile(os.path.join(cfg.dsaLocator, dsa, 'provider_setup_file.xml')):
            dsalist.append( Datasource(dsa) )
    return dsalist

# ------------------------------------------------------------------------------------
def delDsa(dsa):
    pass

# ------------------------------------------------------------------------------------
def createDsa(dsa, template):
    '''Create a new datasource directory and copy the template files to it.'''
    # check dsa spelling. replace spaces with underscore
    if dsa is None:
        log.warn("Datasource name cannot be None!")
        return False
    dsa = dsa.replace(' ', '_')
    # abs path to new dir
    dsaDir = os.path.join(cfg.dsaLocator, dsa)
    # path to template dir
    tmplDir = os.path.join(cfg.dsaTemplateLocator, template)
    # does it already exist?
    if os.path.isdir( dsaDir ):
        log.warn("Datasource %s does already exist!"%dsa)
        return False
    elif not os.path.isdir(tmplDir):
        log.warn("Datasource template %s does not exist!"%template)
        return False
    else:
        # create directory and copy template files recursively
        shutil.copytree(tmplDir, dsaDir)
        log.info("Datasource %s created successfully!"%dsa)
        return True
    return False
    
    
    

##############################
if __name__ ==  "__main__":
    # start logging
    import biocase.initlogs
    dsa='pontoflex_maximus'
    biocase.initlogs.initWrapperLogging(dsa)
    dsaObj = Datasource(dsa)
    print dsaObj.getPSFObj()