From eaba709b46a36bad1206657ebb50bd588b1e35e9 Mon Sep 17 00:00:00 2001 From: JohnE Date: Fri, 1 Jan 2016 02:00:38 -0800 Subject: [PATCH] WIP: index query, updated cursor connection code --- libs/fingerprint_index.py | 99 +++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 50 deletions(-) diff --git a/libs/fingerprint_index.py b/libs/fingerprint_index.py index 0903bf1..1276c13 100644 --- a/libs/fingerprint_index.py +++ b/libs/fingerprint_index.py @@ -35,18 +35,23 @@ class FingerprintIndex: logging.info("DB Open SUCCESSFUL") else: logging.info("No index file found, creating index now...") - self.createIndex(fp_dir) + self.__createIndex(fp_dir) + try: + self.cur = self.db_conn.cursor() + self.__populateIndex(fp_dir) + except Exception as ex: + raise FingerprintIndexWrite("Error populating index file\n{}".format(ex)) + logging.info("Successfully populated the index") self.db_conn = sql.connect(fq_fpidx) - - self.cur = self.db_conn.cursor() - except: + except Exception as ex: if self.db_conn: self.db_conn.close() self.db_conn = None - raise FingerprintIndexOpen("Error opening/creating an index file") + logging.error(ex) + raise FingerprintIndexOpen("Error opening/creating an index file\n") # - def createIndex(self, fp_dir): + def __createIndex(self, fp_dir): fq_fpidx = fp_dir + os.path.sep + INDEX_FILENAME try: self.db_conn = sql.connect(fq_fpidx) @@ -63,37 +68,31 @@ class FingerprintIndex: fp_list TEXT, fp_count INTEGER); ''') - logging.info("Successfully created index table") - self.__populateIndex(fp_dir) - logging.info("Successfully populated the index") + logging.info("Successfully created index table") except Exception as ex: - raise FingerprintIndexWrite("Error creating an index file\n{}".format(ex)) - finally: - if self.db_conn: - self.db_conn.close() - self.db_conn = None + raise FingerprintIndexWrite("Error creating index file\n{}".format(ex)) -# -def qDatabaseMD5(self, md5_db): - try: - rows = self.cur.execute(''' - SELECT md5_list, fp_list, fp_count - FROM md5_all - WHERE md5_db=? - ''', [md5_db]) - except Exception as ex: - logging.error(ex) + # + def qDatabaseMD5(self, md5_db): + try: + rows = self.cur.execute(''' + SELECT md5_list, fp_list, fp_count + FROM md5_all + WHERE md5_db=? + ''', [md5_db]) + except Exception as ex: + logging.error(ex) -# -def qTableMD5(self, md5_table): - try: - rows = self.cur.execute(''' - SELECT fp_list, fp_count - FROM md5_tables - WHERE md5_table=? - ''', [md5_table]) - except Exception as ex: - logging.error(ex) + # + def qTableMD5(self, md5_table): + try: + rows = self.cur.execute(''' + SELECT fp_list, fp_count + FROM md5_tables + WHERE md5_table=? + ''', [md5_table]) + except Exception as ex: + logging.error(ex) # def __populateIndex(self, fp_dir): @@ -112,11 +111,11 @@ def qTableMD5(self, md5_table): finCount = finCount+1 if ((finCount % 5) == 0): self.db_conn.commit() - except Exception as e: - logging.error(e) + except Exception as ex: + logging.error(ex) failCount = failCount+1 - except: - pass + except Exception as ex: + logging.error(ex) finally: self.db_conn.commit() logging.info("Completed populating the index. Completed: {} Failed: {} ".format(str(finCount), str(failCount))) @@ -139,10 +138,10 @@ def qTableMD5(self, md5_table): ''' UPDATE md5_all SET fp_list=?, fp_count=? WHERE md5_db=? ''', [fp_list, fp_count, md5_db]) - except Exception as e: - raise FingerprintIndexWrite("Error updating a row\n{}".format(e)) - except Exception as e: - raise FingerprintIndexWrite("Error inserting a row\n{}".format(e)) + except Exception as ex: + raise FingerprintIndexWrite("Error updating a row\n{}".format(ex)) + except Exception as ex: + raise FingerprintIndexWrite("Error inserting a row\n{}".format(ex)) # def __insertMod_md5_tables(self, md5_db, filename): @@ -162,10 +161,10 @@ def qTableMD5(self, md5_table): ''' UPDATE md5_tables SET fp_list=?, fp_count=? WHERE md5_table=? ''', [fp_list, fp_count, md5_table]) - except Exception as e: - raise FingerprintIndexWrite("Error updating a row\n{}".format(e)) - except Exception as e: - raise FingerprintIndexWrite("Error inserting a row\n{}".format(e)) + except Exception as ex: + raise FingerprintIndexWrite("Error updating a row\n{}".format(ex)) + except Exception as ex: + raise FingerprintIndexWrite("Error inserting a row\n{}".format(ex)) # def __selectFileList(self, md5_db): @@ -176,8 +175,8 @@ def qTableMD5(self, md5_table): ''', [md5_db]) for row in rows: return (row[0], row[1]) - except: - raise FingerprintIndexWrite("Error selecting fp_list\n{}".format(e)) + except Exception as ex: + raise FingerprintIndexWrite("Error selecting fp_list\n{}".format(ex)) # def __selectFileList222(self, md5_table): @@ -188,8 +187,8 @@ def qTableMD5(self, md5_table): ''', [md5_table]) for row in rows: return (row[0], row[1]) - except: - raise FingerprintIndexWrite("Error selecting fp_list\n{}".format(e)) + except Exception as ex: + raise FingerprintIndexWrite("Error selecting fp_list\n{}".format(ex)) # def __checkIntegrity(self):