inheritance part2

This commit is contained in:
age 2006-08-20 16:33:52 +00:00
parent 447958e685
commit c14d5cfbd7
2 changed files with 80 additions and 55 deletions

View file

@ -21,28 +21,56 @@ except:
import types
import unittest
CONF_LOCATIONS = [
"./cryptobox.conf",
"~/.cryptobox.conf",
"/etc/cryptobox/cryptobox.conf"]
class CryptoBox:
'''this class rules them all!
class CryptoBoxProps:
put things like logging, conf and oter stuff in here,
that might be used by more classes, it will be passed on to them'''
def __init__(self):
self.initLogging()
pass
def initLogging(self):
'''initialises the logging system
use it with: 'self.log.[debug|info|warning|error|critical](logmessage)'
from all classes the inherited from CryptoBox
TODO: read the logfile from the config - this is a hen-egg problem
i would prefer start logging to stdout, read the config and redirect
logging to the logfile found in the config [a] '''
## basicConfig(...) needs python >= 2.4
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(module)s %(levelname)s %(message)s',
filename='./cryptobox.log',
filemode='w')
self.log = logging.getLogger("CryptoBoxProps")
self.log.info("loggingsystem is up'n running")
def cbx_inheritance_test(self):
print "you lucky widow"
class CryptoBoxProps(CryptoBox):
'''Get and set the properties of a CryptoBox
This class contains all available devices that may be accessed.
All properties of the cryptobox can be accessed by this class.
'''
def __init__(self, conf_file=None):
'''read config and fill class variables'''
"enable it again - or remove the priv-dropping"
#if os.geteuid() != 0:
# sys.stderr.write("You need to be root to run this program!\n")
# sys.exit(1)
self.initLogging()
#self.cbx_inheritance_test()
if conf_file == None:
for f in CONF_LOCATIONS:
if os.path.exists(os.path.expanduser(f)):
@ -82,21 +110,6 @@ class CryptoBoxProps:
if self.isDeviceAllowed(device):
self.containers.append(CryptoBoxContainer.CryptoBoxContainer(device, self))
def initLogging(self):
'''initialises the logging system
use it with:
'self.log.[debug|info|warning|error|critical](logmessage)'''
## basicConfig(...) needs python >= 2.4
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(module)s %(levelname)s %(message)s',
#TODO: read the logfile from the config
filename='./cryptobox.log',
filemode='w')
self.log = logging.getLogger("CryptoBoxProps")
self.log.info("loggingsystem is up'n running")
def isDeviceAllowed(self, devicename):
"check if a device is white-listed for being used as cryptobox containers"
"TODO: broken!"

View file

@ -1,6 +1,7 @@
#!/usr/bin/env python
import os,sys
import CryptoBox
try:
import cherrypy
except:
@ -11,38 +12,28 @@ try:
except:
print "could not import clearsilver modules! Try apt-get install python-clearsilver."
class clearsilver:
def __init__(self):
pass
class CryptoBoxSite:
'''this is the motherclass of all cbx sites
put the stuff in her, that every site will need access to'''
def print_foo(self):
'''a stupid demo method'''
self.cbx_inheritance_test()
print "fooooooooooooobaaaaaaaaaaaaaaaaaaaar"
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()
class CryptoBoxSite(CryptoBox.CryptoBoxProps):
class CryptoBoxSites(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?
#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)
@ -150,7 +141,7 @@ class CryptoBoxSite(CryptoBox.CryptoBoxProps):
#go render stuff
cs = clearsilver()
cs = CryptoBoxRenderSite()
content = cs.render(TemplateDir+"/main.cs",settings,LangDir+"/"+Language+".hdf")
return content
@ -160,19 +151,40 @@ class CryptoBoxSite(CryptoBox.CryptoBoxProps):
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()
##############################################################
cherrypy.root = CryptoBoxSite()
#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" )
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()
cherrypy.server.start()