From: <tre...@us...> - 2009-09-06 21:03:09
|
Revision: 270 http://spd.svn.sourceforge.net/spd/?rev=270&view=rev Author: treibholz Date: 2009-09-06 21:02:57 +0000 (Sun, 06 Sep 2009) Log Message: ----------- * internal database now has a primary key (__ID) (not used for anything yet) * code cleanup * fixed a bug when deleting an entry Modified Paths: -------------- trunk/src/spd trunk/src/spdCLI.py trunk/src/spdCore.py Modified: trunk/src/spd =================================================================== --- trunk/src/spd 2009-09-06 15:35:32 UTC (rev 269) +++ trunk/src/spd 2009-09-06 21:02:57 UTC (rev 270) @@ -58,13 +58,14 @@ if options.add: pc.addEntry(interface.newEntry(pc.getCollumns())) - pc.recipients = config.users pc.dumpSQLite() pc.exportPasswordsPlain() elif options.delete: - pc.delEntry(options.delete) - pc.recipients = config.users + searchPatterns = sys.argv + del searchPatterns[0] + del searchPatterns[0] + pc.delEntry(searchPatterns) pc.dumpSQLite() pc.exportPasswordsPlain() @@ -81,4 +82,4 @@ # EOF -# vim:foldmethod=marker:tabstop=3:autoindent +# vim:foldmethod=marker:tabstop=3:autoindent:shiftwidth=3 Modified: trunk/src/spdCLI.py =================================================================== --- trunk/src/spdCLI.py 2009-09-06 15:35:32 UTC (rev 269) +++ trunk/src/spdCLI.py 2009-09-06 21:02:57 UTC (rev 270) @@ -109,38 +109,6 @@ return entry # }}} -def main(clientoptions, config, pc): - interface = cli() - - # check if it is a search or not. - - if clientoptions.add: - pc.importPasswordsPlain(config.file,"\t",config.encryption,config.vcs) - pc.fillSQLite() - pc.flushPasswordsPlain() - pc.addEntry(interface.newEntry(pc.getCollumns())) - pc.recipients = config.users - pc.dumpSQLite() - pc.exportPasswordsPlain(config.file,"\t",config.encryption,config.vcs) - - elif clientoptions.delete: - pc.importPasswordsPlain(config.file,"\t",config.encryption,config.vcs) - pc.fillSQLite() - pc.flushPasswordsPlain() - pc.delEntry(clientoptions.delete) - pc.recipients = config.users - pc.dumpSQLite() - pc.exportPasswordsPlain(config.file,"\t",config.encryption,config.vcs) - - else: - pc.importPasswordsPlain(config.file,"\t",config.encryption,config.vcs) - pc.fillSQLite() - pc.flushPasswordsPlain() - searchPatterns = sys.argv - del searchPatterns[0] - interface.listOutput(pc.findPassword(searchPatterns)) - - # EOF -# vim:foldmethod=marker:tabstop=3:autoindent +# vim:foldmethod=marker:tabstop=3:autoindent:shiftwidth=3 Modified: trunk/src/spdCore.py =================================================================== --- trunk/src/spdCore.py 2009-09-06 15:35:32 UTC (rev 269) +++ trunk/src/spdCore.py 2009-09-06 21:02:57 UTC (rev 270) @@ -47,9 +47,13 @@ # creates an internal memory-based SQLite database and declares a # a the self.__table dictionary # file: string, the config-file - def __init__(self,file): #{{{ + def __init__(self,file,debug=0): #{{{ - self.__sql = sqlite3.connect(":memory:") + if not debug: + self.__sql = sqlite3.connect(":memory:") + else: + self.__sql = sqlite3.connect("/tmp/spd_debug.db") + self.__table = {} # read the config @@ -167,7 +171,7 @@ if (encryption == "GnuPG"): fobj = GPGFile() - fobj.open(file,"w",self.recipients) + fobj.open(file,"w",self.users) else: fobj = open(file,"w") @@ -207,30 +211,35 @@ cursor = self.__sql.cursor() count = 0 - # cursor.execute("IF EXISTS DROP TABLE passwords") - query = "CREATE TABLE passwords (" - insertStatement = "INSERT into passwords VALUES ( " + query = "CREATE TABLE passwords ( __ID INTEGER PRIMARY KEY," + self.__insertStatement = "INSERT into passwords ( " + insertValues = "" for collumns in self.__table: # first create the table count += 1 query += collumns + " TEXT" - insertStatement += ":" + collumns + insertValues += ":" + collumns + self.__insertStatement += collumns if (count == len(self.__table)): # write ) if last collumn query += ")" - insertStatement += ")" + self.__insertStatement += ")" + insertValues += ")" else: # otherwise write a , query += ", " - insertStatement += ", " + self.__insertStatement += ", " + insertValues += ", " cursor.execute(query) - + + self.__insertStatement += ' VALUES (' + insertValues + for i in range(0,len(self.__table[collumns])): values = {} for collumns in self.__table: values[collumns] = self.__table[collumns][i] try: - cursor.execute(insertStatement, values) + cursor.execute(self.__insertStatement, values) except sqlite3.OperationalError: #DEBUG print values @@ -253,17 +262,14 @@ # create the query count = 0 - query = "select * from passwords where (" - - for filter in patterns: - for collumns in self.__table: - if (collumns != self.passwordField): - query += collumns + " like '%" + filter + "%' or " - count += 1 - - query = re.sub(r'or $',') and (',query) # delete the last - query = re.sub(r'\) and \($',')',query) # delet the last + searchCollumns = '' + for i in self.__table: + searchCollumns += i + ',' + searchCollumns = re.sub(r',$','',searchCollumns) + + query = "select " + searchCollumns + " from passwords where " + self.__buildWhere(patterns) + cursor.execute(query) # convert the result in a Dictionary @@ -284,28 +290,12 @@ self.__table[collumns] = [] #}}} - # defines the receipients for encryption - # recipients: list of strings - def setRecipients(self,recipients): # {{{ - self.recipients = recipients - # }}} - # add a new entry # entry: dictionary like self.__table def addEntry(self, entry): # {{{ cursor = self.__sql.cursor() - insertStatement = "INSERT into passwords VALUES ( " count = 0 - for i in entry: - count += 1 - insertStatement += ":" + i - if (count != len(entry)): - insertStatement += ", " - else: - insertStatement +=')' - - #print insertStatement - cursor.execute(insertStatement, entry) + cursor.execute(self.__insertStatement, entry) self.__sql.commit() del entry @@ -315,11 +305,17 @@ # patterns: list of strings def delEntry(self,patterns): #{{{ cursor = self.__sql.cursor() - + # create the query - count = 0 - query = "delete from passwords where (" + query = "delete from passwords where " + self.__buildWhere(patterns) + cursor.execute(query) + #}}} + + # internal to build the WHERE + def __buildWhere(self,patterns): # {{{ + count = 0 + query = '(' for filter in patterns: for collumns in self.__table: if (collumns != self.passwordField): @@ -329,10 +325,9 @@ query = re.sub(r'or $',') and (',query) # delete the last query = re.sub(r'\) and \($',')',query) # delet the last - cursor.execute(query) - - #}}} - + return query + #}}} + # return a list of used collumns def getCollumns(self): # {{{ return self.__table.keys() @@ -392,4 +387,4 @@ # }}} # EOF -# vim:foldmethod=marker:tabstop=3:autoindent +# vim:foldmethod=marker:tabstop=3:autoindent:shiftwidth=3 This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |