#!c:\Program Files\Python39\python.exe
# -*- coding: UTF-8 -*-
'''
$RCSfile: system.py,v $
$Revision$
$Author: markus $
$Date$
The BioCASE querytool
'''
import os
# ***** include the biocase.lib directory in the python sys path for importing *****
exec(open(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir, 'lib', 'biocase', 'adjustpath.py'))).read())
exec(open(os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir, os.path.pardir, 'lib', 'biocase', 'appinit.py'))).read())
from biocase.configtool.general import *
from biocase.tools.caching import clearCache
import biocase.datasources
from biocase.cmfinfo import CMFTemplateListClass
import shutil
def replNone(x, replace_string=''):
if x is None:
return replace_string
else:
return x
############################################################################################################
#
# MAIN
#
#===========================================================================================================
# check authentication!
authorize(form, dsa=None)
#
# PROCESS FORMS
#
if action == 'dsa_create':
# create a new DSA
newdsa = form.getfirst('dsa_name_new', None)
if newdsa is not None:
biocase.datasources.createDsa(newdsa, form.getfirst('dsa_template_new', None))
elif action == 'dsa_delete':
# delete a DSA
dsaDir = os.path.join(cfg.dsaLocator, form.getfirst('del_dsa', None))
if os.path.isdir(dsaDir):
recycleBinDir = str(cfg.dsaLocator + ".recycle.bin")
if not os.path.exists(recycleBinDir):
os.makedirs(recycleBinDir)
dsaRecycleBinDir = os.path.join(recycleBinDir, form.getfirst('del_dsa', None))
# Check if dsa already exists in recycle bin
if os.path.exists(dsaRecycleBinDir):
shutil.rmtree(dsaRecycleBinDir, False)
shutil.move(dsaDir, dsaRecycleBinDir)
log.info("Datasource %s is moved to recycle bin."%dsa)
try:
del_dsa = form.getfirst('del_dsa', None)
archivePath = os.path.join(cfg.archiveWorkLocator, del_dsa)
dldPath = os.path.join(cfg.archiveDownloadLocator, del_dsa)
shutil.rmtree(archivePath)
shutil.rmtree(dldPath)
except:
log.error("Can't delete archive folders " + archivePath + " and " + dldPath + ". Please removit manually")
else:
log.error("Can't delete datasource %s. Please remove it manually."%dsa)
elif action == 'dsa_restore':
dsaSource = dsaRecycleBinDir = os.path.join(str(cfg.dsaLocator) + ".recycle.bin", form.getfirst('restore_dsa', None))
dsaTarget = os.path.join(cfg.dsaLocator, form.getfirst('restore_dsa', None))
if os.path.isdir(dsaSource):
shutil.move(dsaSource, dsaTarget)
log.info("Datasource %s is restored."%dsa)
elif action == 'empty_recyclebin':
dsaRecycleBinDir = str(cfg.dsaLocator) + ".recycle.bin"
if os.path.exists(dsaRecycleBinDir):
for dsa in os.listdir(dsaRecycleBinDir):
shutil.rmtree(os.path.join(dsaRecycleBinDir, dsa), False)
log.info("Datasource %s is recycled."%dsa)
elif action == 'cfg_update':
for opt in cfg._listOptions("server"):
if form.getfirst("server."+opt) is not None:
if opt == 'graphviz_dot':
cfg.server.__setOption__(opt, form.getfirst("server."+opt).replace("\\", "/"))
else:
cfg.server.__setOption__(opt, form.getfirst("server."+opt))
cfg.__save__()
elif action == 'clear_cache':
clearCache()
#
# CREATE OUTPUT VIA TEMPLATE
#
# load html template
tmpl = PageMacro('Content', PageMacro.DELMODE)
tmpl.load('Content', os.path.join(templateDir, '_system.html'))
# server config
for opt in cfg._listOptions("server"):
tmpl["server."+opt] = str(getattr(cfg.server, str(opt)))
# DATASOURCES
dsaObjList = biocase.datasources.getDsaList()
dsaTmplList = [{'dsa':dsaObj.name} for dsaObj in dsaObjList]
tmpl.expand('Content', 'datasourcelist', dsaTmplList)
# Recycle Bin
dsaObjList = biocase.datasources.getDsaInRecycleBinList()
if len(dsaObjList) == 0:
tmpl["emptyButton"] = "disabled"
dsaTmplList = [{'dsa':dsaObj.name} for dsaObj in dsaObjList]
tmpl.expand('Content', 'recyclebin', dsaTmplList)
# DSA TEMPLATES
tmpl['dsa_template_options']=getDropDownOptionHtml([t for t in os.listdir(cfg.dsaTemplateLocator) if not t.startswith('.')],'empty')
# Logfiles
links = ['%s' % (fname, fname) for fname in os.listdir(cfg.logLocator) if fname != 'dummy.txt']
tmpl['view_logs'] = '
'.join(links)
# CMF TEMPLATES
cmfTmplListObj = CMFTemplateListClass()
cmfTmplList = [{'name':cmf.name, 'ns':cmf.NS, 'concept_num':str(cmf.concepts)} for cmf in cmfTmplListObj]
tmpl.expand('Content', 'cmftemplates', cmfTmplList)
# ALL CFGs
tmpl['cfgprintout'] = str(cfg).replace("\\", "\\\\")
#
# print HTML !
#
printOverHTTP( tmpl )