From: <Z3...@us...> - 2012-09-27 20:53:40
|
Revision: 345 http://spd.svn.sourceforge.net/spd/?rev=345&view=rev Author: Z3po Date: 2012-09-27 20:53:33 +0000 (Thu, 27 Sep 2012) Log Message: ----------- finalized plain spdStore module Modified Paths: -------------- branches/spd-ng/src/spdCore.py branches/spd-ng/src/spdInterface.py branches/spd-ng/src/spdInterfaces/cli.py branches/spd-ng/src/spdStore/devel.py branches/spd-ng/src/spdStore/plain.py Modified: branches/spd-ng/src/spdCore.py =================================================================== --- branches/spd-ng/src/spdCore.py 2012-09-26 21:58:52 UTC (rev 344) +++ branches/spd-ng/src/spdCore.py 2012-09-27 20:53:33 UTC (rev 345) @@ -26,7 +26,7 @@ self.PasswordColumn = ConfigObject.getPasswordfield() self.PasswordFile = ConfigObject.getPasswordfile() self.spdStore = __import__('spdStore.' + ConfigObject.getStoretype(), fromlist=['',]) - self.db = self.spdStore.getData(self.PasswordFile) + self.db = self.spdStore.getData(self.PasswordFile, self.PasswordPreset) self.db.row_factory = self.__sql2dict self.dbc = self.db.cursor() # }}} @@ -39,12 +39,16 @@ return result # }}} + def saveData(self): # {{{ + if self.spdStore != None: + self.spdStore.saveData(self.PasswordFile, self.PasswordPreset, self.db) + # }}} + def searchEntry(self,SearchList,byid=False): # {{{ query = 'select * from ' + self.PasswordPreset + ' where ' - # search by ID - if byid: + if byid: # search by id for __id in SearchList: if re.match('\d+',__id): query += '__ID = ' + __id + ' or ' @@ -55,10 +59,10 @@ # Build the where-clause for pattern in SearchList: query += '(' - for i in self.getColumns(): - if i != self.PasswordColumn: - query += i + " like '%" + pattern + "%' or " + if i != '__ID': + if i != self.PasswordColumn: + query += i + " like '%" + pattern + "%' or " query = query[0:-4] + ') and ' query = query[0:-5] Modified: branches/spd-ng/src/spdInterface.py =================================================================== --- branches/spd-ng/src/spdInterface.py 2012-09-26 21:58:52 UTC (rev 344) +++ branches/spd-ng/src/spdInterface.py 2012-09-27 20:53:33 UTC (rev 345) @@ -1,3 +1,4 @@ +#!/usr/bin/python # -*- coding: utf-8 -*- # # Copyright (C) 2009 @@ -24,6 +25,7 @@ import spdDefaults import spdCore + class Interface(object): # {{{ """The Interface Module. Interface between client and Core.""" @@ -85,7 +87,7 @@ if 'byid' in args: byid = args['byid'] - if 'args' in args: + if 'args' in args and args['args'] != []: searchString = args['args'] else: searchString = '%' @@ -95,14 +97,15 @@ else: searchlist = string.split(searchString) - if len(searchlist) == 0: - searchlist = ['%',] - passdict = self.core.searchEntry(searchlist,byid) return passdict # }}} + def saveData(self, __blackhole='nothing'): # {{{ + self.core.saveData() + # }}} + def printPasswords(self,args): # {{{ passdict = self.__findPasswords(args) self.client.printPasswords(passdict, args) @@ -114,7 +117,7 @@ if len(__passdict) == 0: raise ValueError('deleteEntry','No matches found') - self.client.printPasswords(__passdict) + self.client.printPasswords(__passdict, args) ids = self.client.deleteEntry(__passdict) self.core.delEntry(ids) # }}} @@ -151,13 +154,17 @@ # }}} def addEntry(self, args): # {{{ - Entry = self.client.createEntry(self.core.getColumns(showid=False)) + Entry = self.client.addEntry(self.core.getColumns(showid=False)) self.core.addEntry(Entry) # }}} def main(self): # {{{ - function, args = self.client.parseArgs(self.core.version) - getattr(self, function)(args) + functions, args = self.client.parseArgs(self.core.version) + if type(functions).__name__ == 'tuple': + for function in functions: + getattr(self, function)(args) + else: + getattr(self, functions)(args) # }}} # }}} @@ -166,6 +173,5 @@ Iface = Interface() Iface.main() - # EOF # 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-09-26 21:58:52 UTC (rev 344) +++ branches/spd-ng/src/spdInterfaces/cli.py 2012-09-27 20:53:33 UTC (rev 345) @@ -54,14 +54,14 @@ length = len(key) print "" for i in range(0,len(passdict.values()[0])): - print "__ID".ljust(length) + ':' + str(passdict["__ID"][i]) + print "__ID".ljust(length) + ' : ' + str(passdict["__ID"][i]) for key in passdict: if key == '__ID': continue if type(passdict[key][i]).__name__ != 'NoneType': - print key.ljust(length) + ': ' + sub('\n','\n ' + ' '.ljust(length),passdict[key][i]) + print key.ljust(length) + ' : ' + sub('\n','\n ' + ' '.ljust(length),passdict[key][i]) else: - print key.ljust(length) + ': ' + print key.ljust(length) + ' : ' print "" elif __outmode == 'wide': for key in passdict: @@ -141,7 +141,7 @@ return __ids # }}} -def createEntry(columns): # {{{ +def addEntry(columns): # {{{ Entry = {} print "Adding a new Entry to our passfile" for column in columns: @@ -251,16 +251,16 @@ 'pwonly' : options.pwonly }) if options.addEntry: - return ["addEntry", __optionsdict] + return [("addEntry","saveData"), __optionsdict] elif options.deleteEntry: - return ["deleteEntry", __optionsdict] + return [("deleteEntry","saveData"), __optionsdict] elif options.editEntry: - return ["editEntry", __optionsdict] + return [("editEntry","saveData"), __optionsdict] elif options.deleteColumn: - return ["deleteColumn", __optionsdict] + return [("deleteColumn","saveData"), __optionsdict] elif options.addColumn: - return ["addColumn", __optionsdict] + return [("addColumn","saveData"), __optionsdict] return ["printPasswords", __optionsdict] # }}} Modified: branches/spd-ng/src/spdStore/devel.py =================================================================== --- branches/spd-ng/src/spdStore/devel.py 2012-09-26 21:58:52 UTC (rev 344) +++ branches/spd-ng/src/spdStore/devel.py 2012-09-27 20:53:33 UTC (rev 345) @@ -12,11 +12,11 @@ testdb = '/home/scabrera/Projects/spd/branches/spd-ng/src/test.db' -def getData(passwordfile): # {{{ +def getData(passwordfile, passwordpreset): # {{{ db = sqlite3.connect(testdb) return db # }}} -def saveData(): # {{{ +def saveData(passwordfile, passwordpreset, database): # {{{ pass # }}} Modified: branches/spd-ng/src/spdStore/plain.py =================================================================== --- branches/spd-ng/src/spdStore/plain.py 2012-09-26 21:58:52 UTC (rev 344) +++ branches/spd-ng/src/spdStore/plain.py 2012-09-27 20:53:33 UTC (rev 345) @@ -3,18 +3,39 @@ # Copyright (C) 2009 # Klaus Umbach <tre...@us...> # Sebastian Cabrera <z3...@us...> +'''This storage module handles plaintext files seperated by "seperator"''' import sqlite3 separator = '\t' -def getData(passwordfile): #{{{ +def getSQLData(passwordpreset, sqlitedb): # {{{ + sqlitecursor = sqlitedb.cursor() + + query = "pragma Table_info('" + passwordpreset + "')" + sqlitecursor.execute(query) + + columns = [] + for i in sqlitecursor: + if i['name'] == "__ID": + continue + columns.append(str(i['name'])) + + query = "SELECT * FROM " + passwordpreset + sqlitecursor.execute(query) + data = sqlitecursor.fetchall() + + return columns, data + # }}} + + +def getData(passwordfile, passwordpreset): #{{{ """This function reads the password file""" passfile = open(passwordfile,"r") sqlitedb = sqlite3.connect(":memory:") sqlitecursor = sqlitedb.cursor() - query = "CREATE TABLE passwords ( __ID INTEGER PRIMARY KEY," + query = "CREATE TABLE " + passwordpreset + " ( __ID INTEGER PRIMARY KEY," for linecount, i in enumerate(passfile): line = i.strip('\n').split(separator) @@ -24,22 +45,46 @@ query += '`' + column + "` TEXT, " query = query[0:-2] + ' )' sqlitecursor.execute(query) - query = "INSERT INTO passwords ( " + + query = "INSERT INTO " + passwordpreset + " ( " for column in line: query += column + ', ' - query = query[0:-2] + ' ) VALUES ( ' - next + query = query[0:-2] + ' ) VALUES ( \"' + continue inserts = "" for column in line: - inserts += column + ', ' - inserts = inserts[0:-2] + ' )' - print str(query + inserts) + inserts += column + '\", \"' + inserts = inserts[0:-4] + '\" )' sqlitecursor.execute(query + inserts) sqlitedb.commit() + passfile.close() return sqlitedb #}}} +def saveData(passwordfile, passwordpreset, sqlitedb): # {{{ + passfile = open(passwordfile,"w") + columns, newdata = getSQLData(passwordpreset, sqlitedb) + + insertdata = "" + + for column in columns: + insertdata += str(column) + '\t' + insertdata = insertdata[0:-1] + '\n' + + passfile.write(insertdata) + insertdata = "" + + for datarow in newdata: + for column in columns: + insertdata += str(datarow[column]) + '\t' + insertdata = insertdata[0:-1] + '\n' + + passfile.write(insertdata) + + passfile.close() +# }}} + # vim:filetype=python:foldmethod=marker:autoindent:expandtab:tabstop=4 This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |