|
From: <Z3...@us...> - 2012-11-02 20:56:42
|
Revision: 348
http://spd.svn.sourceforge.net/spd/?rev=348&view=rev
Author: Z3po
Date: 2012-11-02 20:56:35 +0000 (Fri, 02 Nov 2012)
Log Message:
-----------
restructured ConfigObject
Modified Paths:
--------------
branches/spd-ng/src/spdConfig.py
branches/spd-ng/src/spdInterface.py
branches/spd-ng/src/spdInterfaces/__init__.py
branches/spd-ng/src/spdInterfaces/cli.py
Modified: branches/spd-ng/src/spdConfig.py
===================================================================
--- branches/spd-ng/src/spdConfig.py 2012-10-14 17:40:17 UTC (rev 347)
+++ branches/spd-ng/src/spdConfig.py 2012-11-02 20:56:35 UTC (rev 348)
@@ -6,7 +6,7 @@
from ConfigParser import RawConfigParser
-from sys import exit
+import spdDefaults
import os
class Config(object): # {{{
@@ -22,32 +22,32 @@
def __init__(self, configfile='~/.spd/config'): # {{{
self.__configfile = os.path.expanduser(configfile)
+ if not self.__checkConfigFileExist:
+ spdDefaults.createDefaultConfig(self.ConfigObject)
# }}}
def __checkConfigFileExist(self): # {{{
"""Does the Config file exist?"""
if os.access(self.__configfile, os.F_OK):
- return 0
+ return True
else:
- return 1
+ return False
# }}}
def __checkConfigFileReadable(self): # {{{
"""Is the Config file readable?"""
- reval = self.__checkConfigFileExist()
- config = RawConfigParser()
+ if self.__checkConfigFileExist():
+ config = RawConfigParser()
- if reval > 0:
- return reval, config
-
- if os.access(self.__configfile,os.R_OK):
- config.read(self.__configfile)
- return 0,config
+ if os.access(self.__configfile,os.R_OK):
+ config.read(self.__configfile)
+ return config
+ else:
+ raise OSError('ConfigFile unreadable')
else:
- print 'ConfigFile not readable!'
- exit(2)
+ return False
# }}}
def __checkConfigFileWriteable(self): # {{{
@@ -56,10 +56,9 @@
if os.access(self.__configfile,os.W_OK):
configfile = open(self.__configfile, 'wb')
else:
- print 'ConfigFile not writeable!'
- exit(2)
+ raise OSError('ConfigFile not writeable!')
- return 0, configfile
+ return configfile
# }}}
def __writeConfigFile(self,config): # {{{
@@ -74,46 +73,51 @@
"""Check if a given section exists.
# section: the given section."""
- reval, config = self.__checkConfigFileReadable()
- if reval > 0:
- return reval, config
+ config = self.__checkConfigFileReadable()
+
+ if not config:
+ raise OSError('ConfigFile does not exist!')
if config.has_section(section):
- return 0, config
-
- return 2, config
+ return config
+ else:
+ return False
# }}}
def __addSection(self, section): # {{{
"""create a section if its not in configfile.
# section: the section to create."""
- reval, config = self.__hasSection(section)
+ config = self.__hasSection(section)
- if reval == 2:
+ if not config:
config.add_section(section)
self.__writeConfigFile(config)
-
+ return True
else:
- return reval
-
- return 0
+ return False
# }}}
def __delSection(self, section): # {{{
"""delete a Section from configfile.
# section: the section to delete."""
- reval, config = self.__hasSection(section)
+ config = self.__hasSection(section)
- if reval == 0:
+ if config:
config.remove_section(section)
self.__writeConfigFile(config)
+ else:
+ return False
+ # }}}
- if reval > 0:
- return reval
-
- return 0
+ def setPreset(self, preset): # {{{
+ """Sets the used Preset"""
+ if preset not in self.getSections():
+ if preset != 'Main':
+ raise KeyError('Preset unavailable!')
+ else:
+ self.ConfigSection = preset
# }}}
def getOption(self, section, option): # {{{
@@ -121,15 +125,15 @@
# section: in which section is the option configured?
# option: option to return."""
- reval, config = self.__hasSection(section)
+ config = self.__hasSection(section)
- if reval > 0:
- return reval, None
+ if not config:
+ return False
if config.has_option(section,option):
- return 0, config.get(section,option)
+ return config.get(section,option)
else:
- return 4, None
+ return False
# }}}
def setOption(self, section, option, value): # {{{
@@ -138,18 +142,14 @@
# option: option to set/change.
# value: set the option to this value."""
- reval, config = self.__hasSection(section)
+ 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
+ if not config:
+ config.set(section, option, value)
+ self.__writeConfigFile(config)
+ return True
+ else:
+ return False
# }}}
def delOption(self, section, option): # {{{
@@ -157,181 +157,97 @@
# section: which section holds the option?
# option: option to delete."""
- 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
+ if self.__hasSection(section):
+ config = self.getOption(section,option)
+ if config:
+ config.remove_option(section,option)
+ self.__writeConfigFile(config)
+ return True
# }}}
def getAllSections(self): # {{{
- reval, config = self.__checkConfigFileReadable()
- if reval > 0:
- return reval, config
+ config = self.__checkConfigFileReadable()
+ if not config:
+ raise OSError('ConfigFile does not exist!')
+
if config.sections():
return 0, config.sections()
return 2, config
# }}}
- def getSections(self, raw=False): # {{{
+ def getSections(self): # {{{
"""Return all sections from configfile."""
- reval, config = self.__checkConfigFileReadable()
+ config = self.__checkConfigFileReadable()
- if reval > 0:
- raise KeyError(self.ReturnCodes[reval])
+ if not config:
+ raise OSError('ConfigFile does not exist!')
- if raw:
- return 0, config.sections()
-
return config.sections()
-
# }}}
def addConfigSection(self, section): # {{{
- reval = self.__addSection(section)
+ set = self.__addSection(section)
- if reval > 0:
- raise KeyError(self.ReturnCodes[reval])
+ if not set:
+ raise KeyError('Section already exists!')
# }}}
- def getConfigVersion(self, raw=False): # {{{
+ def getConfigVersion(self): # {{{
"""Return the config version."""
- reval, option = self.getOption(self.ConfigSection,"version")
-
- if reval > 1 and reval < 2:
- raise KeyError(self.ReturnCodes[reval]) # don't tolerate These
-
- if reval > 0:
- if raw:
- return reval, None
-
- raise KeyError(self.ReturnCodes[reval])
-
- if raw:
- return reval, option
-
- return option
+ return self.getOption(self.ConfigSection,"version")
# }}}
def setConfigVersion(self, version): # {{{
"""set the config version to the given version.
# version: version number."""
- reval = self.setOption(self.ConfigSection,"version",version)
-
- if reval > 0:
- raise KeyError(self.ReturnCodes[reval])
+ return self.setOption(self.ConfigSection,"version",version)
# }}}
- def getClientmode(self, raw=False): # {{{
- '''get the clientmode.
- # return the clientmode option'''
+ def getClientmode(self): # {{{
+ '''get the clientmode.
+ # return the clientmode option'''
- reval, clientmode = self.getOption(self.ConfigSection, 'clientmode')
-
- if reval > 0 and reval < 4:
- raise KeyError(self.ReturnCodes[reval])
-
- if reval > 0:
- if raw:
- return reval, None
- else:
- raise KeyError(self.ReturnCodes[reval])
-
- if raw:
- return reval, clientmode
-
- return clientmode
+ return self.getOption(self.ConfigSection, 'clientmode')
# }}}
def setClientmode(self, clientmode): # {{{
"""set the clientmode.
# clientmode: clientmode to use."""
- reval = self.setOption(self.ConfigSection,"clientmode",clientmode)
-
- if reval > 0:
- raise KeyError(self.ReturnCodes[reval])
+ return self.setOption(self.ConfigSection,"clientmode",clientmode)
# }}}
- def getPasswordfield(self, section=ConfigSection, raw=False): # {{{
- '''get the clientmode.
- # return the Password Column'''
+ def getPasswordfield(self, section=ConfigSection): # {{{
+ '''get the clientmode.
+ # return the Password Column'''
- reval, clientmode = self.getOption(section, 'passwordfield')
-
- if reval > 0 and reval < 4:
- raise KeyError(self.ReturnCodes[reval])
-
- if reval > 0:
- return reval, None
-
- if raw:
- return reval, clientmode
-
- return clientmode
+ return self.getOption(section, 'passwordfield')
# }}}
def setPasswordfield(self, passcolumn, section=ConfigSection): # {{{
"""set the Password Column.
# passcolumn: passcolumn to set."""
- reval = self.setOption(section,"passwordfield",passcolumn)
-
- if reval > 0:
- raise KeyError(self.ReturnCodes[reval])
+ return self.setOption(section,"passwordfield",passcolumn)
# }}}
- def getStoretype(self, raw=False, section=ConfigSection): # {{{
- '''get the clientmode.
- # return the clientmode option'''
+ def getStoretype(self, section=ConfigSection): # {{{
+ '''get the clientmode.
+ # return the clientmode option'''
- reval, storetype = self.getOption(section, 'storetype')
-
- if reval > 0 and reval < 4:
- raise KeyError(self.ReturnCodes[reval])
-
- if reval > 0:
- if raw:
- return reval, None
- else:
- raise KeyError(self.ReturnCodes[reval])
-
- if raw:
- return reval, storetype
-
- return storetype
+ return self.getOption(section, 'storetype')
# }}}
def getPasswordfile(self, section=ConfigSection): # {{{
- '''get the password field.
- # return the password field'''
+ '''get the password field.
+ # return the password field'''
- reval, storetype = self.getOption(section, 'file')
-
- if reval > 0:
- raise KeyError(self.ReturnCodes[reval])
-
- if reval > 0:
- raise KeyError(self.ReturnCodes[reval])
-
- return storetype
+ return self.getOption(section, 'file')
# }}}
# }}}
Modified: branches/spd-ng/src/spdInterface.py
===================================================================
--- branches/spd-ng/src/spdInterface.py 2012-10-14 17:40:17 UTC (rev 347)
+++ branches/spd-ng/src/spdInterface.py 2012-11-02 20:56:35 UTC (rev 348)
@@ -22,7 +22,6 @@
import os
import string
import spdConfig
-import spdDefaults
import spdCore
@@ -35,48 +34,34 @@
def __init__(self,clientmode=False): # {{{
"""init procedure"""
- initcheck = {}
- self.ConfigObject = spdConfig.Config()
-
- self.core = spdCore.Core(self.ConfigObject)
-
if not os.access(self.spddir, os.F_OK):
os.mkdir(self.spddir)
- reval, self.configversion = self.ConfigObject.getConfigVersion(raw=True)
+ self.ConfigObject = spdConfig.Config()
- if reval == 1:
- spdDefaults.createDefaultConfig(self.ConfigObject)
- self.configversion = self.ConfigObject.getConfigVersion()
- elif reval == 4:
+ self.configversion = self.ConfigObject.getConfigVersion()
+
+ if not self.configversion:
print 'ConfigVersion not found...setting Default one\n'
- __blackhole = raw_input('Press any key')
self.ConfigObject.setConfigVersion('0.3-alpha')
self.configversion = self.ConfigObject.getConfigVersion()
+ __blackhole = raw_input('Press any key')
if not clientmode:
- reval, initcheck['clientmode'] = self.ConfigObject.getClientmode(raw=True)
- if reval == 4:
+ clientmode = self.ConfigObject.getClientmode()
+ if not clientmode:
print 'Default Clientmode not found...setting it to CLI\n'
- __blackhole = raw_input('Press any key')
self.ConfigObject.setClientmode('cli')
- initcheck.update({ 'clientmode' : self.ConfigObject.getClientmode() })
+ clientmode = self.ConfigObject.getClientmode()
+ __blackhole = raw_input('Press any key')
- reval, __passwordfield = self.ConfigObject.getPasswordfield(raw=True)
-
- if reval == 0:
- initcheck.update({ 'passwordfield' : __passwordfield })
-
- if not clientmode:
- clientmode = initcheck['clientmode']
-
self.client = __import__('spdInterfaces.' + clientmode.lower(), fromlist=['',])
- if 'passwordfield' in initcheck:
- self.client.passwordfield = initcheck['passwordfield']
+ passwordfield = self.ConfigObject.getPasswordfield()
- del initcheck
+ if passwordfield:
+ self.client.passwordfield
# }}}
def __findPasswords(self,args): # {{{
@@ -102,6 +87,14 @@
return passdict
# }}}
+ def updatePreset(self, preset): # {{{
+ self.ConfigObject.setPreset(preset)
+ # }}}
+
+ def loadStore(self): # {{{
+ self.core = spdCore.Core(self.ConfigObject)
+ # }}}
+
def saveData(self, __blackhole='nothing'): # {{{
self.core.saveData()
# }}}
@@ -179,7 +172,10 @@
# }}}
def main(self): # {{{
- functions, args = self.client.parseArgs(self.core.version)
+ functions, args = self.client.parseArgs()
+ if 'preset' in args:
+ self.updatePreset(args['preset'])
+ self.loadStore()
if type(functions).__name__ == 'tuple':
for function in functions:
getattr(self, function)(args)
Modified: branches/spd-ng/src/spdInterfaces/__init__.py
===================================================================
--- branches/spd-ng/src/spdInterfaces/__init__.py 2012-10-14 17:40:17 UTC (rev 347)
+++ branches/spd-ng/src/spdInterfaces/__init__.py 2012-11-02 20:56:35 UTC (rev 348)
@@ -7,3 +7,5 @@
'''spdInterfaces Module
This Module takes Care of the Interface used by SPD.'''
+
+# vim:filetype=python:foldmethod=marker:autoindent:expandtab:tabstop=4
Modified: branches/spd-ng/src/spdInterfaces/cli.py
===================================================================
--- branches/spd-ng/src/spdInterfaces/cli.py 2012-10-14 17:40:17 UTC (rev 347)
+++ branches/spd-ng/src/spdInterfaces/cli.py 2012-11-02 20:56:35 UTC (rev 348)
@@ -15,6 +15,7 @@
from re import sub, search
passwordfield = 'Password'
+version = '0.3'
def __ErrorHandler(function,message): # {{{
"""ErrorHandler of the Client Module.
@@ -167,12 +168,12 @@
return Entry
# }}}
-def parseArgs(coreversion): # {{{
+def parseArgs(): # {{{
__optionsdict = {}
parser = OptionParser( "%prog [Options] [Keywords]",
description = "SPD - Simple Password Displayer - CLI",
- version = coreversion,
+ version = version,
epilog = """With no parameters it will print out everything!
Please report Bugs or Feature Request at http://spd.sourceforge.net""")
@@ -226,11 +227,13 @@
# dest = "localStore",
# default=False,
# help = "do not operate on the configured RCS, only use the local password container")
- #
- #parser.add_option( "--passfile",
- # dest = "passfile",
- # help = "do not use the confgured passfile but the one given here")
+ #
+ parser.add_option( "--preset",
+ dest = "preset",
+ default='Main',
+ help = "Use the given preset instead of Main")
+
group = OptionGroup(parser, "Columns Manipulation")
group.add_option( "--add-columns",
@@ -263,7 +266,8 @@
__optionsdict.update({ 'args' : args,
'byid' : options.byid,
'outmode' : __outmode,
- 'pwonly' : options.pwonly })
+ 'pwonly' : options.pwonly,
+ 'preset' : options.preset})
if options.addEntry:
return [("fetchData","addEntry","saveData"), __optionsdict]
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|