prefs is inheretable

This commit is contained in:
age 2006-08-20 18:23:35 +00:00
parent c14d5cfbd7
commit 975b2bb14a
2 changed files with 66 additions and 56 deletions

View file

@ -7,24 +7,15 @@ progress. So things might be confusing here. Hopefully not for long.
:) :)
''' '''
import logging
import CryptoBoxLogger import CryptoBoxLogger
import CryptoBoxContainer import CryptoBoxContainer
import types
import re import re
import os import os
import sys import sys
try:
import configobj ## needed for reading and writing the config file
except:
print("Could't import 'configobj'! Try apt-get install python-configobj.")
sys.exit()
import types
import unittest import unittest
CONF_LOCATIONS = [ CONF_LOCATIONS = [ "./cryptobox.conf", "~/.cryptobox.conf", "/etc/cryptobox/cryptobox.conf"]
"./cryptobox.conf",
"~/.cryptobox.conf",
"/etc/cryptobox/cryptobox.conf"]
class CryptoBox: class CryptoBox:
'''this class rules them all! '''this class rules them all!
@ -32,10 +23,11 @@ class CryptoBox:
put things like logging, conf and oter stuff in here, put things like logging, conf and oter stuff in here,
that might be used by more classes, it will be passed on to them''' that might be used by more classes, it will be passed on to them'''
def __init__(self): def __init__(self):
self.initLogging() self.__initLogging()
pass self.__initPreferences()
def initLogging(self): def __initLogging(self):
import logging
'''initialises the logging system '''initialises the logging system
use it with: 'self.log.[debug|info|warning|error|critical](logmessage)' use it with: 'self.log.[debug|info|warning|error|critical](logmessage)'
@ -45,12 +37,46 @@ class CryptoBox:
i would prefer start logging to stdout, read the config and redirect i would prefer start logging to stdout, read the config and redirect
logging to the logfile found in the config [a] ''' logging to the logfile found in the config [a] '''
## basicConfig(...) needs python >= 2.4 ## basicConfig(...) needs python >= 2.4
logging.basicConfig(level=logging.DEBUG, try:
format='%(asctime)s %(module)s %(levelname)s %(message)s', logging.basicConfig(level=logging.DEBUG,
filename='./cryptobox.log', format='%(asctime)s %(module)s %(levelname)s %(message)s',
filemode='w') filename='./cryptobox.log',
self.log = logging.getLogger("CryptoBoxProps") filemode='w')
self.log.info("loggingsystem is up'n running") self.log = logging.getLogger("CryptoBoxProps")
self.log.info("loggingsystem is up'n running")
## from now on everything can be logged via self.log...
except:
sys.stderr.write("Something with the loggingsystem went wrong. I give up.")
sys.exit(1)
def __initPreferences(self):
try:
import configobj ## needed for reading and writing the config file
except:
self.log.error("Could't import 'configobj'! Try apt-get install python-configobj.")
sys.exit(1)
for f in CONF_LOCATIONS:
if os.path.exists(os.path.expanduser(f)):
conf_file = os.path.expanduser(f)
break
try:
self.cbxPrefs = configobj.ConfigObj(conf_file)
self.log.debug("found config: %s" % self.cbxPrefs.items())
except:
self.log.error("Could not read configuration file. I give up.\n")
sys.exit(1)
try:
nameDB_file = os.path.join(
self.cbxPrefs["Main"]["DataDir"],
self.cbxPrefs["Main"]["NameDatabase"])
if os.path.exists(nameDB_file):
self.nameDB = configobj.ConfigObj(nameDB_file)
else:
self.nameDB = configobj.ConfigObj(nameDB_file, create_empty=True)
except SyntaxError:
self.log.error("Error during parsing of name database file (%s).\n" % (nameDB_file, ))
sys.exit(1)
def cbx_inheritance_test(self): def cbx_inheritance_test(self):
print "you lucky widow" print "you lucky widow"
@ -63,40 +89,18 @@ class CryptoBoxProps(CryptoBox):
All properties of the cryptobox can be accessed by this class. All properties of the cryptobox can be accessed by this class.
''' '''
def __init__(self, conf_file=None): def __init__(self):
'''read config and fill class variables''' '''read config and fill class variables'''
"enable it again - or remove the priv-dropping" "enable it again - or remove the priv-dropping"
#if os.geteuid() != 0: #if os.geteuid() != 0:
# sys.stderr.write("You need to be root to run this program!\n") # sys.stderr.write("You need to be root to run this program!\n")
# sys.exit(1) # sys.exit(1)
CryptoBox.__init__(self)
#self.cbx_inheritance_test() #self.cbx_inheritance_test()
if conf_file == None: #print self.cbxPrefs.items()
for f in CONF_LOCATIONS: ####
if os.path.exists(os.path.expanduser(f)):
conf_file = os.path.expanduser(f)
break
else:
#sys.stderr.write("Could not find a configuration file. I give up.\n")
self.log.error("Could not find a configuration file. I give up.\n")
sys.exit(1)
try:
self.cbxPrefs = configobj.ConfigObj(conf_file)
except SyntaxError:
#sys.stderr.write("Error during parsing of configuration file (%s).\n" % (conf_file, ))
self.log.error("Could not find a configuration file. I give up.\n")
try:
nameDB_file = os.path.join(
self.cbxPrefs["Main"]["DataDir"],
self.cbxPrefs["Main"]["NameDatabase"])
if os.path.exists(nameDB_file):
self.nameDB = configobj.ConfigObj(nameDB_file)
else:
self.nameDB = configobj.ConfigObj(nameDB_file, create_empty=True)
except SyntaxError:
#sys.stderr.write("Error during parsing of name database file (%s).\n" % (nameDB_file, ))
self.log.error("Error during parsing of name database file (%s).\n" % (nameDB_file, ))
sys.exit(1)
self.__cboxUID = int(self.cbxPrefs["System"]["User"]) self.__cboxUID = int(self.cbxPrefs["System"]["User"])
self.__cboxGID = int(self.cbxPrefs["System"]["Group"]) self.__cboxGID = int(self.cbxPrefs["System"]["Group"])
self.debug = CryptoBoxLogger.CryptoBoxLogger( self.debug = CryptoBoxLogger.CryptoBoxLogger(
@ -321,14 +325,13 @@ Destination = /tmp/cryptobox.log
def testConfigFile(self): def testConfigFile(self):
self.assertRaises(KeyError, CryptoBoxProps, "/not/existing/path") CryptoBoxProps()
CryptoBoxProps(self.configFile)
self.assertTrue(os.path.exists(self.nameDBFile)) self.assertTrue(os.path.exists(self.nameDBFile))
self.assertTrue(os.path.exists(self.logFile)) self.assertTrue(os.path.exists(self.logFile))
def testDeviceCheck(self): def testDeviceCheck(self):
cb = CryptoBoxProps(self.configFile) cb = CryptoBoxProps()
self.assertTrue(cb.isDeviceAllowed("/dev/loop")) self.assertTrue(cb.isDeviceAllowed("/dev/loop"))
self.assertTrue(cb.isDeviceAllowed("/dev/loop1")) self.assertTrue(cb.isDeviceAllowed("/dev/loop1"))
self.assertTrue(cb.isDeviceAllowed("/dev/loop/urgd")) self.assertTrue(cb.isDeviceAllowed("/dev/loop/urgd"))
@ -343,4 +346,5 @@ Destination = /tmp/cryptobox.log
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()
#cb = CryptoBoxProps()

View file

@ -17,15 +17,18 @@ class CryptoBoxSite:
'''this is the motherclass of all cbx sites '''this is the motherclass of all cbx sites
put the stuff in her, that every site will need access to''' put the stuff in her, that every site will need access to'''
def __init__(self):
pass
def print_foo(self): def print_foo(self):
'''a stupid demo method''' '''a stupid demo method'''
self.cbx_inheritance_test() self.cbx_inheritance_test()
print "fooooooooooooobaaaaaaaaaaaaaaaaaaaar" print "fooooooooooooobaaaaaaaaaaaaaaaaaaaar"
class CryptoBoxSites(CryptoBox.CryptoBoxProps, CryptoBoxSite): #class CryptoBoxSites(CryptoBox.CryptoBoxProps, CryptoBoxSite):
''' class CryptoBoxSites(CryptoBox.CryptoBox, CryptoBox.CryptoBoxProps, CryptoBoxSite):
'''
def index(self,action="show_status",page="",weblang="",device="", type=""): def index(self,action="show_status",page="",weblang="",device="", type=""):
#PROPOSAL: #PROPOSAL:
@ -142,7 +145,9 @@ class CryptoBoxSites(CryptoBox.CryptoBoxProps, CryptoBoxSite):
#go render stuff #go render stuff
cs = CryptoBoxRenderSite() cs = CryptoBoxRenderSite()
content = cs.render(TemplateDir+"/main.cs",settings,LangDir+"/"+Language+".hdf") self.log.debug("rendering site")
content = cs.render(self.cbxPrefs["Settings"]["TemplateDir"]+"/main.cs",settings, \
self.cbxPrefs["Settings"]["LangDir"]+"/"+Language+".hdf")
return content return content
@ -156,18 +161,19 @@ class CryptoBoxRenderSite:
def render(self,cs_path,settings = {},hdf_path = ""): def render(self,cs_path,settings = {},hdf_path = ""):
""" """
render a clearsilver template and return the result. render a clearsilver template and return the result.
gets: gets:
- path to clearsilver template - path to clearsilver template
- dictionary with settings (optional) - dictionary with settings (optional)
- path to hdf dataset (optional) - path to hdf dataset (optional)
""" """
hdf=neo_util.HDF() hdf = neo_util.HDF()
if hdf_path != "": if hdf_path != "":
hdf.readFile(hdf_path) hdf.readFile(hdf_path)
for key in settings.keys(): for key in settings.keys():
hdf.setValue(key,str(settings[key])) hdf.setValue(key,str(settings[key]))
cs= neo_cs.CS(hdf) cs = neo_cs.CS(hdf)
cs.parseFile(cs_path) cs.parseFile(cs_path)
return cs.render() return cs.render()