From: <Z3...@us...> - 2012-10-07 14:08:48
|
Revision: 346 http://spd.svn.sourceforge.net/spd/?rev=346&view=rev Author: Z3po Date: 2012-10-07 14:08:40 +0000 (Sun, 07 Oct 2012) Log Message: ----------- restructured some of the code and did a little cleanup Modified Paths: -------------- branches/spd-ng/src/spdConfig.py branches/spd-ng/src/spdCore.py branches/spd-ng/src/spdDefaults.py branches/spd-ng/src/spdInterface.py branches/spd-ng/src/spdInterfaces/cli.py Modified: branches/spd-ng/src/spdConfig.py =================================================================== --- branches/spd-ng/src/spdConfig.py 2012-09-27 20:53:33 UTC (rev 345) +++ branches/spd-ng/src/spdConfig.py 2012-10-07 14:08:40 UTC (rev 346) @@ -6,20 +6,17 @@ from ConfigParser import RawConfigParser +from sys import exit import os -#import sys class Config(object): # {{{ '''This is the Configuration Module You can read or set config options''' __configfile = None - 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' } + ReturnCodes = { 1 : 'Missing ConfigFile', + 2 : 'Section not found', + 4 : 'Option not found' } ConfigSection = 'Main' def __init__(self, configfile='~/.spd/config'): # {{{ @@ -32,7 +29,7 @@ if os.access(self.__configfile, os.F_OK): return 0 else: - return 2 + return 1 # }}} def __checkConfigFileReadable(self): # {{{ @@ -44,31 +41,22 @@ if reval > 0: return reval, config - if os.access(self.__configfile,os.R_OK): config.read(self.__configfile) return 0,config else: - return 4, config + print 'ConfigFile not readable!' + exit(2) # }}} def __checkConfigFileWriteable(self): # {{{ """Is the Config file writeable?""" - reval = self.__checkConfigFileExist() - - if reval > 0: - try: - configfile = open(self.__configfile, 'wb') - except IOError: - return 8, configfile - else: - return 0, configfile - if os.access(self.__configfile,os.W_OK): configfile = open(self.__configfile, 'wb') else: - return 8, None + print 'ConfigFile not writeable!' + exit(2) return 0, configfile # }}} @@ -77,14 +65,8 @@ '''Write the Config given. # config: the configobject.''' - reval, configfile = self.__checkConfigFileWriteable() - if reval > 0: - return reval - - try: - config.write(configfile) - except Exception, e: - return 16 + configfile = self.__checkConfigFileWriteable() + config.write(configfile) # }}} def __hasSection(self,section): # {{{ @@ -98,7 +80,7 @@ if config.has_section(section): return 0, config - return 32, config + return 2, config # }}} def __addSection(self,section): # {{{ @@ -107,13 +89,11 @@ reval, config = self.__hasSection(section) - if reval > 0 and reval < 32 and reval is not 2: - return reval - - config.add_section(section) - reval = self.__writeConfigFile(config) + if reval == 2: + config.add_section(section) + self.__writeConfigFile(config) - if reval > 0: + else: return reval return 0 @@ -125,12 +105,10 @@ reval, config = self.__hasSection(section) - if reval > 0 and reval < 32: - return reval + if reval == 0: + config.remove_section(section) + self.__writeConfigFile(config) - config.remove_section(section) - reval = self.__writeConfigFile(config) - if reval > 0: return reval @@ -150,7 +128,7 @@ if config.has_option(section,option): return 0, config.get(section,option) else: - return 64, None + return 4, None # }}} def __setOption(self,section,option,value): # {{{ @@ -205,7 +183,7 @@ if config.sections(): return 0, config.sections() - return 32, config + return 2, config # }}} def getSections(self, raw=False): # {{{ @@ -235,7 +213,7 @@ reval, option = self.__getOption("Main","version") - if reval > 2 and reval < 32: + if reval > 1 and reval < 2: raise KeyError(self.ReturnCodes[reval]) # don't tolerate These if reval > 0: @@ -266,7 +244,7 @@ reval, clientmode = self.__getOption('Main', 'clientmode') - if reval > 0 and reval < 64: + if reval > 0 and reval < 4: raise KeyError(self.ReturnCodes[reval]) if reval > 0: @@ -297,7 +275,7 @@ reval, clientmode = self.__getOption('Main', 'passwordfield') - if reval > 0 and reval < 64: + if reval > 0 and reval < 4: raise KeyError(self.ReturnCodes[reval]) if reval > 0: @@ -328,7 +306,7 @@ reval, storetype = self.__getOption('Main', 'storetype') - if reval > 0 and reval < 64: + if reval > 0 and reval < 4: raise KeyError(self.ReturnCodes[reval]) if reval > 0: @@ -349,7 +327,7 @@ reval, storetype = self.__getOption('Main', 'file') - if reval > 0 and reval < 64: + if reval > 0 and reval < 4: raise KeyError(self.ReturnCodes[reval]) if reval > 0: Modified: branches/spd-ng/src/spdCore.py =================================================================== --- branches/spd-ng/src/spdCore.py 2012-09-27 20:53:33 UTC (rev 345) +++ branches/spd-ng/src/spdCore.py 2012-10-07 14:08:40 UTC (rev 346) @@ -150,18 +150,16 @@ if __newcolumns == '': __newcolumns = __newcolumn else: - __newcolumns = __newcolumns + ',' + __newcolumn + __newcolumns += ',' + __newcolumn if __columns == '': __columns = __column else: - __columns = __columns + ',' + __column + __columns += ',' + __column # it's not that easy, to delete a column in SQLite self.dbc.executescript(""" - CREATE TEMPORARY TABLE """ + self.PasswordPreset + """_temp(""" + __newcolumns + """); - INSERT INTO """ + self.PasswordPreset + """_temp SELECT """ + __columns + """ FROM """ + self.PasswordPreset + """; - DROP TABLE """ + self.PasswordPreset + """; + ALTER TABLE """ + self.PasswordPreset + """ RENAME TO """ + self.PasswordPreset + """_temp; CREATE TABLE """ + self.PasswordPreset + """(""" + __newcolumns + """); INSERT INTO """ + self.PasswordPreset + """ SELECT """ + __columns + """ FROM """ + self.PasswordPreset + """_temp; DROP TABLE """ + self.PasswordPreset + """_temp; @@ -169,6 +167,37 @@ self.db.commit() # }}} + def renameColumn(self, oldcolumnname, newcolumnname): #{{{ + __columns = '' + __newcolumns = '' + for __column in self.getColumns(showid=True): + if __column == oldcolumnname and oldcolumnname != '__ID': + __newcolumn = newcolumnname + ' text' + elif __column == '__ID': + __newcolumn = __column + ' INTEGER PRIMARY KEY' + else: + __newcolumn = __column + ' text' + + if __newcolumns == '': + __newcolumns = __newcolumn + else: + __newcolumns += ',' + __newcolumn + + if __columns == '': + __columns = __column + else: + __columns += ',' + __column + + # it's not that easy, to delete a column in SQLite + self.dbc.executescript(""" + ALTER TABLE """ + self.PasswordPreset + """ RENAME TO """ + self.PasswordPreset + """_temp; + CREATE TABLE """ + self.PasswordPreset + """(""" + __newcolumns + """); + INSERT INTO """ + self.PasswordPreset + """ SELECT """ + __columns + """ FROM """ + self.PasswordPreset + """_temp; + DROP TABLE """ + self.PasswordPreset + """_temp; + """) + self.db.commit() + # }}} + def makepass(self,size=16): # {{{ chars = [] chars.extend([i for i in string.ascii_letters]) Modified: branches/spd-ng/src/spdDefaults.py =================================================================== --- branches/spd-ng/src/spdDefaults.py 2012-09-27 20:53:33 UTC (rev 345) +++ branches/spd-ng/src/spdDefaults.py 2012-10-07 14:08:40 UTC (rev 346) @@ -12,18 +12,6 @@ __version__ = '0.3-alpha' -def __ErrorHandler(function, message): # {{{ - '''ErrorHandler of the Defaults Module. - # function: the function who called the ErrorHandler - # message: means the \'Exception\' to throw''' - print '################################################' - print 'spdDefaults ErrorHandler.' - print 'ERRORFUNCTION: ' + function - print 'ERRORMESSAGE: ' + message - print '################################################' - exit(2) -# }}} - def __oldSPDCheck(): # {{{ '''Checks if a old spd configuration file is there and tries to mutate it''' __oldspdconfig = '~/.spd/spd.conf' Modified: branches/spd-ng/src/spdInterface.py =================================================================== --- branches/spd-ng/src/spdInterface.py 2012-09-27 20:53:33 UTC (rev 345) +++ branches/spd-ng/src/spdInterface.py 2012-10-07 14:08:40 UTC (rev 346) @@ -46,23 +46,23 @@ reval, self.configversion = self.ConfigObject.getConfigVersion(raw=True) - if reval > 0 and reval < 64: + if reval == 1: spdDefaults.createDefaultConfig(self.ConfigObject) - return self.__startupCheck() - elif reval == 64: + self.configversion = self.ConfigObject.getConfigVersion() + elif reval == 4: 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() - reval, initcheck['clientmode'] = self.ConfigObject.getClientmode(raw=True) + if not clientmode: + reval, initcheck['clientmode'] = self.ConfigObject.getClientmode(raw=True) + if reval == 4: + 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() }) - if reval > 0: - print 'Clientmode not found...setting it to CLI\n' - __blackhole = raw_input('Press any key') - self.ConfigObject.setClientmode('cli') - initcheck.update({ 'clientmode' : self.ConfigObject.getClientmode() }) - reval, __passwordfield = self.ConfigObject.getPasswordfield(raw=True) if reval == 0: @@ -74,7 +74,7 @@ self.client = __import__('spdInterfaces.' + clientmode.lower(), fromlist=['',]) if 'passwordfield' in initcheck: - self.client._passwordfield = initcheck['passwordfield'] + self.client.passwordfield = initcheck['passwordfield'] del initcheck # }}} @@ -140,9 +140,25 @@ if column in self.core.getColumns(showid=False): self.core.delColumn(column) else: - raise ValueError('deleteColumn','column ' + column + ' is not part of the passwordfile') + raise ValueError('column ' + column + ' is not part of the passwordfile') # }}} + def renameColumn(self,args): # {{{ + if 'args' in args: + columns = args['args'] + else: + columns = [] + if len(columns) != 2: + raise ValueError('You need to give old and new columnname as argument') + else: + oldcolumn = columns[0] + newcolumn = columns[1] + if oldcolumn in self.core.getColumns(showid=False): + self.core.renameColumn(oldcolumn, newcolumn) + else: + raise ValueError('column ' + column + ' is not part of the passwordfile') + # }}} + def addColumn(self,args): # {{{ if 'args' in args: columns = args['args'] Modified: branches/spd-ng/src/spdInterfaces/cli.py =================================================================== --- branches/spd-ng/src/spdInterfaces/cli.py 2012-09-27 20:53:33 UTC (rev 345) +++ branches/spd-ng/src/spdInterfaces/cli.py 2012-10-07 14:08:40 UTC (rev 346) @@ -31,7 +31,6 @@ def printPasswords(passdict,args=None): # {{{ """Print the passwords out of a dictionary. # passdict: the dictionary full of passwords""" - __keylength = {} pwonly = False if args is not None: @@ -46,7 +45,7 @@ __ErrorHandler('printPasswords', 'pwonly selected but more than one result returned') else: print passdict[passwordfield][0] - + # print everything line by line elif __outmode == 'line': length = 0 for key in passdict.keys(): @@ -63,12 +62,13 @@ else: print key.ljust(length) + ' : ' print "" + # print every entry in one line elif __outmode == 'wide': + __keylength = {} + # create a dictionary for all the column lengths for key in passdict: - __keylength.update({ key : 0 }) + __keylength.update({ key : len(key) }) - if len(key) > __keylength[key]: - __keylength[key] = len(key) for value in passdict[key]: if value is not None: if search('\n',str(value)): @@ -79,24 +79,33 @@ if len(str(value)) > __keylength[key]: __keylength[key] = len(str(value)) + stdout.write(' ' + '__ID'.ljust(__keylength['__ID']) + ' |') for key in passdict: - stdout.write(key.ljust(__keylength[key]) + '|') + if key != '__ID': + stdout.write(' ' + key.ljust(__keylength[key]) + ' |') stdout.write('\n') __entries = len(passdict.values()[0]) for i in range(0,__entries): __space = '' + + # write out the ID first + stdout.write(' ' + str(passdict['__ID'][i]).ljust(__keylength['__ID']) + ' |') + __space += ''.ljust(__keylength['__ID'],'>') + '>>>' + # now comes the rest for key in passdict: + if key == '__ID': + continue if passdict[key][i] is None: passdict[key][i] = '' __count = 0 - for line in str(passdict[key][i]).split('\n'): + for line in passdict[key][i].split('\n'): if __count > 0: stdout.write('\n' + __space) - stdout.write(line.ljust(__keylength[key]) + '|') + stdout.write(' ' + line.ljust(__keylength[key]) + ' |') __count += 1 - __space = __space + ''.ljust(__keylength[key],'>') + '>' + __space += ''.ljust(__keylength[key],'>') + '>>>' stdout.write('\n') else: __ErrorHandler('printPasswords', 'output mode unknown: ' + str(__outmode)) @@ -236,6 +245,12 @@ default = False, help ="Delete Columns given") + group.add_option( "--rename-columns", + action = "store_true", + dest ="renameColumn", + default = False, + help ="Rename OLDCOLUMNNAME to NEWCOLUMNNAME") + parser.add_option_group(group) (options, args) = parser.parse_args() @@ -259,6 +274,8 @@ elif options.deleteColumn: return [("deleteColumn","saveData"), __optionsdict] + elif options.renameColumn: + return [("renameColumn","saveData"), __optionsdict] elif options.addColumn: return [("addColumn","saveData"), __optionsdict] This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |