webserver, sites and settings splitted

included some new functions from the perl file
the clearsilver templates have to be modified next
This commit is contained in:
age 2006-08-24 00:06:19 +00:00
parent 2fec1260c5
commit 697d5f9b0c
5 changed files with 249 additions and 183 deletions

View file

@ -1,196 +1,34 @@
#!/usr/bin/env python2.4
import os,sys
import CryptoBox
import os
import CryptoBoxWebserverSites
try:
import cherrypy
except:
print "could not import cherrypy module! Try apt-get install python-cherrypy."
sys.exit(1)
try:
import neo_cgi, neo_util, neo_cs
except:
print "could not import clearsilver modules! Try apt-get install python-clearsilver."
class CryptoBoxSite:
'''this is the motherclass of all cbx sites
put the stuff in her, that every site will need access to'''
class CryptoBoxWebserver:
'''this class starts the cherryp webserver and serves the single sites'''
def __init__(self):
pass
def print_foo(self):
'''a stupid demo method'''
self.cbx_inheritance_test()
print "fooooooooooooobaaaaaaaaaaaaaaaaaaaar"
cherrypy.root = CryptoBoxWebserverSites.CryptoBoxWebserverSites()
#expose static content:
#I currently have no idea how to cleanly extract the stylesheet path from
#the config object without an extra CryptoBox.CryptoBoxProps instance.
#perhaps put config handling into a seperate class in CryptoBox.py?
cherrypy.config.configMap.update(
{
"/cryptobox.css": {
"staticFilter.on" : True,
"staticFilter.file": os.path.abspath("../www-data/cryptobox.css" )
}
})
#class CryptoBoxSites(CryptoBox.CryptoBoxProps, CryptoBoxSite):
class CryptoBoxSites(CryptoBox.CryptoBox, CryptoBox.CryptoBoxProps, CryptoBoxSite):
def index(self,action="show_status",page="",weblang="",device="", type=""):
#PROPOSAL:
#cherrypy could create nice paths like http://crypto.box/doc/,
#but that would require an update of the links in the templates.
#is the style worth it? - yes :)
self.print_foo() # this method was inherited by CryptoBoxSite, be careful not to overwrite methods unwanted..
#sanitize input
page = self.sanitize_input(page)
action = self.sanitize_input(action)
device = self.sanitize_input(device)
type = self.sanitize_input(type)
#setting settings ;)
settings={}
TemplateDir = self.cbxPrefs["Settings"]["TemplateDir"]
settings["Settings.TemplateDir"] = TemplateDir
settings["Settings.Stylesheet"] = self.cbxPrefs["Settings"]["Stylesheet"]
LangDir = self.cbxPrefs["Settings"]["LangDir"]
settings["Settings.LangDir"] = LangDir
Language = self.cbxPrefs["Settings"]["Language"]
settings["Settings.DocDir"] = self.cbxPrefs["Settings"]["DocDir"]
settings["Settings.DocLang"] = self.cbxPrefs["Settings"]["DocLang"]
# BEWARE: there are two kinds of actions:
# * some require a harddisk
# * some do not require a harddisk
# take care, that you put a new action into the appropriate block below
#TODO: check for ssl, see cryptobox.pl lines 483-9
#--------------------------------------------------------------#
# here you may define all cases that do not require a harddisk #
# put all other cases below the harddisk check #
#--------------------------------------------------------------#
#################### show_log #######################
if action == "show_log":
settings["Data.Action"] = "show_log"
##################### doc ############################
elif action == "doc":
settings["Data.Action"] = "show_doc"
if page != "":
settings["Data.Doc.Page"] = page
else:
settings["Data.Doc.Page"] ="CryptoBoxUser"
##################### poweroff ######################
elif action == "system_ask":
settings["Data.Action"] = "form_system"
##################### reboot ########################
elif action == "shutdown_do":
if type == "reboot":
#TODO: call function for system reboot
pass
settings["Data.Success"] = "ReBoot"
settings["Data.Redirect.Action"] = "show_status"
settings["Data.Redirect.Delay"] = "180"
else:
settings["Data.Success"] = "PowerOff"
settings["Data.Action"] = "empty"
##################### check for a harddisk ##########################
# catch this error, to prevent all following actions from execution #
#####################################################################
elif not True: #TODO: replace True with check for hard disk is_harddisk_available()
settings["Data.Error"] = "NoHardDisk"
#-------------------------------------------------------#
# here you may define all cases that require a harddisk #
#-------------------------------------------------------#
################ umount_do #######################
elif action == "unmount_do":
if device == "":
#TODO: debug message: "invalid device: "+ device
settings["Data.Warning"] = "InvalidDevice"
settings["Data.Action"] = "empty"
elif not True: #TODO: replace True with check_config()
settings["Data.Warning"] = "NotInitialized"
settings["Data.Action"] = "form_init"
elif True: #TODO: replace True with check_init_running()
settings["Data.Warning"] = "InitNotFinished"
settings["Data.Action"] = "empty"
settings["Data.Redirect.Action"] = "form_config"
settings["Data.Redirect.Delay"] = "30"
elif not True: #TODO: replace True with check_mounted(device)
settings["Data.Warning"] = "NotMounted"
settings["Data.Action"] = "show_volume"
else: #unmount
#TODO: replace this line with umount_vol(device)
if True: #TODO: replace True with check_mounted(device)
settings["Data.Warning"] = "UmountFailed"
settings["Data.Action"] = "show_volume"
else:
settings["Data.Action"] = "show_volume"
################ mount_do ########################
elif action == "mount_do":
if device != "":
pass #TODO: is_encrypted = check_device_encryption(device)
if device == "":
#TODO: debug_msg(DEBUG_INFO, "invalid device: " + device)
settings["Data.Warning"] = "InvalidDevice"
settings["Data.Action"] = "empty"
elif not True: #TODO: replace True with check_config()
settings["Data.Warning"] = "NotInitialized"
settings["Data.Action"] = "form_init"
#at cryptobox.pl line 568
#go render stuff
cs = CryptoBoxRenderSite()
self.log.debug("rendering site")
content = cs.render(self.cbxPrefs["Settings"]["TemplateDir"]+"/main.cs",settings, \
self.cbxPrefs["Settings"]["LangDir"]+"/"+Language+".hdf")
return content
def sanitize_input(self,data):
#TODO: do things
return data
index.exposed = True
class CryptoBoxRenderSite:
def render(self,cs_path,settings = {},hdf_path = ""):
"""
render a clearsilver template and return the result.
gets:
- path to clearsilver template
- dictionary with settings (optional)
- path to hdf dataset (optional)
"""
hdf = neo_util.HDF()
if hdf_path != "":
hdf.readFile(hdf_path)
for key in settings.keys():
hdf.setValue(key,str(settings[key]))
cs = neo_cs.CS(hdf)
cs.parseFile(cs_path)
return cs.render()
##############################################################
def start(self):
cherrypy.server.start()
if __name__ == "__main__":
cherrypy.root = CryptoBoxSites()
#expose static content:
#I currently have no idea how to cleanly extract the stylesheet path from
#the config object without an extra CryptoBox.CryptoBoxProps instance.
#perhaps put config handling into a seperate class in CryptoBox.py?
cherrypy.config.configMap.update(
{
"/cryptobox.css": {
"staticFilter.on" : True,
"staticFilter.file": os.path.abspath("../www-data/cryptobox.css" )
}
})
cherrypy.server.start()
cbw = CryptoBoxWebserver()
cbw.start()

View file

@ -0,0 +1,29 @@
try:
import neo_cgi, neo_util, neo_cs
except:
print "could not import clearsilver modules! Try apt-get install python-clearsilver."
class CryptoBoxWebserverRender:
def render(self, website):
'''
render a clearsilver template and return the result.
gets:
- path to clearsilver template
- dictionary with settings (optional)
- path to hdf dataset (optional)
'''
## overload to the max
website.log.info("rendering site: "+website.settings["Data.Action"]+" "+str(website.settings))
cs_path = website.cbxPrefs["Settings"]["TemplateDir"]+"/main.cs"
hdf_path = website.cbxPrefs["Settings"]["LangDir"]+"/"+website.cbxPrefs["Settings"]["Language"]+".hdf"
hdf = neo_util.HDF()
if hdf_path != "":
hdf.readFile(hdf_path)
for key in website.settings.keys():
hdf.setValue(key,str(website.settings[key]))
cs = neo_cs.CS(hdf)
cs.parseFile(cs_path)
return cs.render()

View file

@ -0,0 +1,24 @@
class CryptoBoxWebserverSettings:
'''this is the motherclass of all cbx site settings
put the stuff in here, that every site will need access to'''
def setSettings(self, website):
'''a dictionary gets filled here, with values from the configfile
there can also be set some useful standards here'''
website.settings={}
## put all found Settings values in the dictionary
for key in self.cbxPrefs["Settings"].keys():
website.settings["Settings."+key] = self.cbxPrefs["Settings"][key]
#self.log.info(self.settings)
def print_foo(self):
'''a stupid demo method
if there are methods necessary for more than one site,
put them in here like this stupid demo'''
self.log.info("'print_foo' was called by some site and triggerd an inheritated method")
self.cbx_inheritance_test("fooooooooooooobaaaaaaaaaaaaaaaaaaaar")

View file

@ -0,0 +1,152 @@
import CryptoBox
import CryptoBoxWebserverSettings
import CryptoBoxWebserverRender
website = CryptoBoxWebserverRender.CryptoBoxWebserverRender()
class CryptoBoxWebserverSites(CryptoBox.CryptoBoxProps, CryptoBoxWebserverSettings.CryptoBoxWebserverSettings):
'''
url2func = {'index':'show_status','doc':'show_doc','logs':'show_log'}
'''
def __prepare(self, csname="show_status"):
'''this method handles stuff that all sites need as preparation
self.settings is filled be the following methodcall
thus every rendered site will get actual values from the configfile
after that the site-method may set individual values too
'''
self.setSettings(self)
self.settings
## default site to render is 'show_status'
self.settings["Data.Action"] = csname
#TODO: check ssl status
def __sanitize_input(self,data):
#TODO: don't let evil cracker fuck up your code
return data
def __check_config(self):
pass
def __check_init_running(self):
pass
######################################################################
## put real sites down here and don't forget to expose the mat the end
def logs(self):
'''be careful to name this method just "log" seems to be a
reserved word'''
self.__prepare("show_log")
return website.render(self)
def status(self):
self.__prepare("show_status")
if not self.__check_config():
self.settings["Data.Warning"] = "NotInitialized"
self.settings["Data.Action"] = "form_init"
elif self.__check_init_running():
self.settings["Data.Warning"] = "InitNotFinished"
self.settings["Data.Action"] = "empty"
self.settings["Data.Redirect.Action"] = "form_config"
self.settings["Data.Redirect.Delay"] = "30"
else:
self.settings["Data.Action"] = "show_status"
self.settings["Data.Redirect.Delay"] = "60"
return website.render(self)
def doc(self,action="",page="",weblang=""):
#TODO: action is unnessessary, remove here and from all templates
self.__prepare("show_doc")
if not page == "":
self.settings["Data.Doc.Page"] = page
#self.log.info(page)
#self.log.info(self.settings)
else:
self.settings["Data.Doc.Page"] ="CryptoBoxUser"
#self.log.info(page)
#self.log.info(self.settings)
return website.render(self)
def system(self,type=""):
self.__prepare("form_system")
if type == "reboot":
self.settings["Data.Success"] = "ReBoot"
self.settings["Data.Redirect.Action"] = "show_status"
self.settings["Data.Redirect.Delay"] = "180"
self.log.info("TODO: call function for system reboot")
elif type == "poweroff":
self.settings["Data.Success"] = "PowerOff"
self.log.info("TODO: call function for system shutdown")
else:
self.log.warn("someone tried to shutdown the system")
return website.render(self)
def index(self):
self.__prepare("show_status")
return website.render(self)
'''
## DONE: this functions are pythonized
#################### show_log #######################
##################### doc ############################
##################### poweroff ######################
##################### reboot ########################
## but there are even more TODO
##################### check for a harddisk ##########################
# catch this error, to prevent all following actions from execution #
#####################################################################
elif not True: #TODO: replace True with check for hard disk is_harddisk_available()
settings["Data.Error"] = "NoHardDisk"
#-------------------------------------------------------#
# here you may define all cases that require a harddisk #
#-------------------------------------------------------#
################ umount_do #######################
elif action == "unmount_do":
if device == "":
#TODO: debug message: "invalid device: "+ device
settings["Data.Warning"] = "InvalidDevice"
settings["Data.Action"] = "empty"
elif not True: #TODO: replace True with check_config()
settings["Data.Warning"] = "NotInitialized"
settings["Data.Action"] = "form_init"
elif True: #TODO: replace True with check_init_running()
settings["Data.Warning"] = "InitNotFinished"
settings["Data.Action"] = "empty"
settings["Data.Redirect.Action"] = "form_config"
settings["Data.Redirect.Delay"] = "30"
elif not True: #TODO: replace True with check_mounted(device)
settings["Data.Warning"] = "NotMounted"
settings["Data.Action"] = "show_volume"
else: #unmount
#TODO: replace this line with umount_vol(device)
if True: #TODO: replace True with check_mounted(device)
settings["Data.Warning"] = "UmountFailed"
settings["Data.Action"] = "show_volume"
else:
settings["Data.Action"] = "show_volume"
################ mount_do ########################
elif action == "mount_do":
if device != "":
pass #TODO: is_encrypted = check_device_encryption(device)
if device == "":
#TODO: debug_msg(DEBUG_INFO, "invalid device: " + device)
settings["Data.Warning"] = "InvalidDevice"
settings["Data.Action"] = "empty"
elif not True: #TODO: replace True with check_config()
settings["Data.Warning"] = "NotInitialized"
settings["Data.Action"] = "form_init"
#at cryptobox.pl line 568
'''
############################################################################
## to make the sites visible through the webserver they must be exposed here
index.exposed = True
doc.exposed = True
logs.exposed = True
system.exposed = True
status.exposed = True

View file

@ -0,0 +1,23 @@
import unittest
import cherrypy, os
import CryptoBox
import CryptoBoxWebserver
class TestCryptoBoxWebserver(unittest.TestCase):
cbw = CryptoBoxWebserver.CryptoBoxWebserver()
cherrypy.config.configMap.update(
{
"/cryptobox.css": {
"staticFilter.on" : True,
"staticFilter.file": os.path.abspath("../www-data/cryptobox.css" )
}
})
cbw.start()
def testConfigFile(self):
'''testConfigFile TODO'''
self.assertTrue(1)
if __name__ == "__main__":
unittest.main()