|
From: <Z3...@us...> - 2010-11-28 12:16:42
|
Revision: 319
http://spd.svn.sourceforge.net/spd/?rev=319&view=rev
Author: Z3po
Date: 2010-11-28 12:16:35 +0000 (Sun, 28 Nov 2010)
Log Message:
-----------
reworked Config and Check Module
Modified Paths:
--------------
branches/spd-ng/src/spdCheck.py
branches/spd-ng/src/spdConfig.py
branches/spd-ng/src/spdInterface.py
branches/spd-ng/src/test.db
Modified: branches/spd-ng/src/spdCheck.py
===================================================================
--- branches/spd-ng/src/spdCheck.py 2010-11-28 01:01:51 UTC (rev 318)
+++ branches/spd-ng/src/spdCheck.py 2010-11-28 12:16:35 UTC (rev 319)
@@ -15,14 +15,22 @@
# module: which module called me??"""
if module == "spdInterface":
- self.checkSPDdir()
- self.checkConfigFile()
- self.getClientmode()
+ self.__checkSPDdir()
elif module == "spdCore":
pass
# }}}
- def checkSPDdir(self): # {{{
+ def __ErrorHandler(self, message): # {{{
+ '''ErrorHandler of the Check Module.
+ # message: means the \'Exception\' to throw'''
+ print '################################################'
+ print 'spdCheck ErrorHandler.'
+ print 'ERRORMESSAGE: ' + message
+ print '################################################'
+ sys.exit(2)
+ # }}}
+
+ def __checkSPDdir(self): # {{{
"""Check if the spd directory exists"""
if not os.access(self.spddir, os.F_OK):
@@ -33,37 +41,27 @@
"""Check the configfile."""
config = self.config
- if config.checkConfigFileExist():
- if config.checkConfigFileReadable():
- if config.hasSection("Main"):
- if config.getOption("Main","version"):
- version = config.getConfigVersion()
- if version == "0.3-alpha":
- pass
- else:
- config.ErrorHandler("Where you got THIS version from???\nCannot work with")
- else:
- config.setConfigVersion("0.3-alpha")
- else:
- config.setSection("Main")
- else:
- config.ErrorHandler("CONFIGFILE THERE BUT NOT READABLE")
- else:
- if config.checkConfigFileWriteable():
- newconfig = Defaults()
- newconfig.createDefaultConfig(config)
- else:
- config.ErrorHandler("CONFIGFILE PATH NOT WRITEABLE")
- # }}}
- def getClientmode(self):
- config = self.config
- if config.getOption('Main','clientmode'):
- self.clientmode = config.getOption('Main','clientmode')
- else:
- config.setOption('Main','clientmode','CLI')
- self.clientmode = config.getOption('Main','clientmode')
+ reval, version = config.getConfigVersion(raw=True)
+ if reval > 0 and reval < 64:
+ Defaults.createDefaultConfig(config)
+ elif reval == 64:
+ print 'ConfigVersion not found...setting Default one\n'
+ config.setConfigVersion('0.3-alpha')
+ version = config.getConfigVersion()
+
+ reval, clientmode = config.getClientmode(raw=True)
+
+ if reval > 0:
+ config.setClientmode('CLI')
+ clientmode = config.getClientmode()
+
+ return({ 'version' : version,
+ 'clientmode' : clientmode })
+
+ # }}}
+
# }}}
class Defaults(object): # {{{
@@ -73,9 +71,9 @@
"""Create the default config here.
# config: the configparser to use"""
- config.setSection("Main")
- config.setConfigVersion("0.3-alpha")
- config.setClientMode("CLI")
+ config.addConfigSection('Main')
+ config.setConfigVersion('0.3-alpha')
+ config.setClientmode('CLI')
# }}}
# }}}
Modified: branches/spd-ng/src/spdConfig.py
===================================================================
--- branches/spd-ng/src/spdConfig.py 2010-11-28 01:01:51 UTC (rev 318)
+++ branches/spd-ng/src/spdConfig.py 2010-11-28 12:16:35 UTC (rev 319)
@@ -5,12 +5,20 @@
import sys
class Config(object): # {{{
- """This is the Configuration Module
- You can read or set config options"""
+ '''This is the Configuration Module
+ You can read or set config options'''
- configfile = os.path.expanduser('~/.spd/config')
+ ReturnCodes = { 2 : 'Missing ConfigFile',
+ 4 : 'ConfigFile not Readable',
+ 8 : 'ConfigFile not Writeable',
+ 16 : 'Configfile Writeable but another Error Occured',
+ 32 : 'Section not found',
+ 64 : 'Option not found' }
- def ErrorHandler(self,message): # {{{
+ def __init__(self, conffile='~/.spd/config'):
+ self.configfile = os.path.expanduser(conffile)
+
+ def __ErrorHandler(self,message): # {{{
"""ErrorHandler of the Config Module.
# message: means the \'Exception\' to throw"""
print "################################################"
@@ -21,171 +29,251 @@
sys.exit(2)
# }}}
- def checkConfigFileExist(self): # {{{
+ def __checkConfigFileExist(self): # {{{
"""Does the Config file exist?"""
if os.access(self.configfile, os.F_OK):
- return True
+ return 0
else:
- return False
+ return 2
# }}}
- def checkConfigFileWriteable(self): # {{{
- """Is the Config file writeable?"""
+ def __checkConfigFileReadable(self): # {{{
+ """Is the Config file readable?"""
- if not self.checkConfigFileExist():
- open(self.configfile, 'w').close()
- if os.access(self.configfile,os.W_OK):
- return True
- else:
- return False
- # }}}
+ reval = self.__checkConfigFileExist()
- def checkConfigFileReadable(self): # {{{
- """Is the Config file readable?"""
+ if reval > 0:
+ return reval, None
config = RawConfigParser()
- if not self.checkConfigFileExist():
- self.ErrorHandler("CONFIGFILE MISSING")
+
if os.access(self.configfile,os.R_OK):
config.read(self.configfile)
- return config
+ return 0,config
else:
- return False
+ return 4, None
# }}}
- def writeConfigFile(self,config): # {{{
- """Write the Config given.
- # config: the configobject."""
+ def __checkConfigFileWriteable(self): # {{{
+ """Is the Config file writeable?"""
- configfile = open(self.configfile, 'wb')
+ reval = self.__checkConfigFileExist()
+
+ if reval > 0:
+ return reval, None
+
+ if os.access(self.configfile,os.W_OK):
+ configfile = open(self.configfile, 'wb')
+ return 0, configfile
+ else:
+ return 8, None
+ # }}}
+
+ def __writeConfigFile(self,config): # {{{
+ '''Write the Config given.
+ # config: the configobject.'''
+
+ reval, configfile = self.__checkConfigFileWriteable()
+ if reval > 0:
+ return reval
+
try:
config.write(configfile)
except Exception, e:
- self.ErrorHandler("An Error Occured" + str(e))
+ return 16
# }}}
- def hasSection(self,section): # {{{
+ def __hasSection(self,section): # {{{
"""Check if a given section exists.
# section: the given section."""
- config = self.checkConfigFileReadable()
- if not config:
- self.ErrorHandler("CONFIGFILE NOT READABLE")
+ reval, config = self.__checkConfigFileReadable()
+ if reval > 0:
+ return reval, None
+
if config.has_section(section):
- return config
+ return 0, config
+
+ return 32, config
# }}}
- def setSection(self,section): # {{{
+ def __addSection(self,section): # {{{
"""create a section if its not in configfile.
# section: the section to create."""
- if not self.checkConfigFileWriteable():
- self.ErrorHandler("CONFIGFILE NOT WRITEABLE")
- config = self.hasSection(section)
- if not config:
- config = self.checkConfigFileReadable()
- config.add_section(section)
- self.writeConfigFile(config)
- return True
- else:
- self.ErrorHandler("SECTION " + section + " already there")
+ reval, config = self.__hasSection()
+
+ if reval > 0 and reval < 32:
+ return reval
+
+ config.add_section(section)
+ reval = self.__writeConfigFile(config)
+
+ if reval > 0:
+ return reval
+
+ return 0
# }}}
- def getSections(self): # {{{
+ def __listSections(self): # {{{
"""Return all sections from configfile."""
- config = self.checkConfigFileReadable()
- if not config:
- self.ErrorHandler("CONFIGFILE NOT READABLE")
- else:
- return config.sections()
+ reval, config = self.__checkConfigFileReadable()
+
+ if reval > 0:
+ return reval, None
+
+ return 0, config.sections()
# }}}
- def delSection(self,section): # {{{
+ def __delSection(self,section): # {{{
"""delete a Section from configfile.
# section: the section to delete."""
- if not self.checkConfigFileWriteable():
- self.ErrorHandler("CONFIGFILE NOT WRITEABLE")
- config = self.hasSection(section)
- if config:
- config.remove_section(section)
- return True
- else:
- return False
+ reval, config = self.__hasSection(section)
+
+ if reval > 0 and reval < 32:
+ return reval
+
+ config.remove_section(section)
+ reval = self.__writeConfigFile(config)
+
+ if reval > 0:
+ return reval
+
+ return 0
# }}}
- def getOption(self,section,option): # {{{
+ def __getOption(self,section,option): # {{{
"""Return the option from section.
# section: in which section is the option configured?
# option: option to return."""
- config = self.hasSection(section)
- if config:
- if config.has_option(section,option):
- return config.get(section,option)
- else:
- return False
+ reval, config = self.__hasSection(section)
+
+ if reval > 0:
+ return reval, None
+
+ if config.has_option(section,option):
+ return 0, config.get(section,option)
else:
- self.ErrorHandler("SECTION " + section + " MISSING")
+ return 64, None
# }}}
- def setOption(self,section,option,value): # {{{
+ def __setOption(self,section,option,value): # {{{
"""Create or set the given option.
# section: section of the option.
# option: option to set/change.
# value: set the option to this value."""
- config = self.hasSection(section)
- if config:
- config.set(section,option,value)
- self.writeConfigFile(config)
- return True
- else:
- self.ErrorHandler("SECTION " + section + " MISSING")
+ reval, config = self.__hasSection(section)
+
+ if reval > 0:
+ return reval
+
+ config.set(section,option,value)
+ reval = self.__writeConfigFile(config)
+
+ if reval > 0:
+ return reval
+
+ return 0
# }}}
- def delOption(self,section,option): # {{{
+ def __delOption(self,section,option): # {{{
"""delete an option.
# section: which section holds the option?
# option: option to delete."""
- config = self.hasSection(section)
- if config:
- if self.getOption(section,option):
- config.remove_option(section,option)
- self.writeConfigFile(config)
- return True
- else:
- return False
- else:
- return False
+ reval, config = self.__hasSection(section)
+
+ if reval > 0:
+ return reval
+
+ reval, option = self.__getOption(section,option)
+
+ if reval > 0:
+ return reval
+
+ config.remove_option(section,option)
+ reval = self.__writeConfigFile(config)
+
+ if reval > 0:
+ return reval
+
+ return 0
+
# }}}
- def getConfigVersion(self): # {{{
+ def addConfigSection(self): # {{{
+ reval = self.__addSection('Main')
+
+ if reval > 0:
+ self.__ErrorHandler(self.ReturnCodes[reval])
+
+ # }}}
+
+ def getConfigVersion(self, raw=False): # {{{
"""Return the config version."""
- option = self.getOption("Main","version")
- if option:
- return option
- else:
- self.ErrorHandler("Option version missing")
+ reval, option = self.__getOption("Main","version")
+
+ if reval > 2 and reval < 32:
+ self.__ErrorHandler(self.ReturnCodes[reval]) # don't tolerate These
+
+ if reval > 0:
+ if raw:
+ return reval, None
+
+ self.__ErrorHandler(self.ReturnCodes[reval])
+
+ if raw:
+ return reval, option
+
+ return option
# }}}
def setConfigVersion(self,version): # {{{
"""set the config version to the given version.
# version: version number."""
- self.setOption("Main","version",version)
+ reval = self.__setOption("Main","version",version)
+
+ if reval > 0:
+ self.__ErrorHandler(self.ReturnCodes[reval])
# }}}
- def setClientMode(self,clientmode): # {{{
+ def getClientmode(self, raw=False): # {{{
+ '''get the clientmode.
+ # return the clientmode option'''
+
+ reval, clientmode = self.__getOption('Main', 'clientmode')
+
+ if reval > 0 and reval < 64:
+ self.__ErrorHandler(self.ReturnCodes[reval])
+
+ if reval > 0:
+ if raw:
+ return reval, None
+ else:
+ self.__ErrorHandler(self.ReturnCodes[reval])
+
+ if raw:
+ return reval, clientmode
+
+ return clientmode
+ # }}}
+
+ def setClientmode(self,clientmode): # {{{
"""set the clientmode.
# clientmode: clientmode to use."""
- self.setOption("Main","clientmode",clientmode)
+ reval = self.__setOption("Main","clientmode",clientmode)
+
+ if reval > 0:
+ self.__ErrorHandler(self.ReturnCodes[reval])
# }}}
# }}}
Modified: branches/spd-ng/src/spdInterface.py
===================================================================
--- branches/spd-ng/src/spdInterface.py 2010-11-28 01:01:51 UTC (rev 318)
+++ branches/spd-ng/src/spdInterface.py 2010-11-28 12:16:35 UTC (rev 319)
@@ -2,30 +2,30 @@
import spdCheck
import spdCore
-import spdCLI
import string
from sys import exit
-version = "0.3-alpha"
-
class Interface(object): # {{{
"""The Interface Module.
Connection between Output and Core."""
+ version = None
+ client = None
+ core = None
- def __init__(self,version,clientmode=False): # {{{
+ def __init__(self,clientmode=False): # {{{
"""init procedure"""
- self.version = version
+ check = spdCheck.Check("spdInterface")
+ initcheck = check.checkConfigFile()
- initcheck = spdCheck.Check("spdInterface")
+ self.version = initcheck['version']
if not clientmode:
- if initcheck.clientmode:
- clientmode = initcheck.clientmode
- else:
- clientmode = 'CLI'
+ clientmode = initcheck['clientmode']
+
del initcheck
if clientmode == 'CLI':
+ import spdCLI
self.client = spdCLI.Cli()
else:
print 'CLIENTMODE \'not yet\' available'
@@ -82,7 +82,7 @@
# }}}
-interface = Interface(version)
+interface = Interface()
interface.main()
Modified: branches/spd-ng/src/test.db
===================================================================
(Binary files differ)
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|