
import mysql.connector
from mysql.connector import errorcode
from db_connector import DBConnector
#from validate_email import validate_email

class ChecksumManager:
    """
    Provides an interface to control checksum/filename pair table.
    
    Checksum Database Table

    +---------+-------------+------+-----+---------+-------+
    | Field   | Type        | Null | Key | Default | Extra |
    +---------+-------------+------+-----+---------+-------+
    | filename| varchar(255)| YES  |     | NULL    |       |
    | checksum| varchar(32) | YES  |     | NULL    |       |
    +---------+-------------+------+-----+---------+-------+
    
    The filename in the table is stored as the absolute filename.

    For example a file in the home directory would be stored as:

    /home/user/file.txt

    """
    def __init__(self, table_name="CHECKSUMS"):
        self.filename_field_length = 255
        self.checksum_field_length = 64 
        self.connector = DBConnector()
        self.connection = self.connector.get_connection()
        self.table_name = table_name

    def checksum_table_exists(self):
        """
        Check to see if the checksum table exists in the database.
        
        :return: bool -- True if the table exists. False otherwise.
        """
        pass

    def create_checksum_table(self):
        """
        Creates a new checksum table in the database with the same properties
        as described in the class documentation. 

        :return: bool -- True if the table was created or already existed.
                         False otherwise.
        """
        try:
            cursor = self.connection.cursor()
            sql = """CREATE TABLE IF NOT EXISTS %s (filename VARCHAR(%d) NOT
            NULL PRIMARY KEY, checksum VARCHAR(%d) NOT NULL)""" % (self.table_name, self.filename_field_length,self.checksum_field_length)
            print(sql, "HERE")
            cursor.execute(sql)
            return True
        except Exception as err:
            print(err)
            return False

    def add_checksum_pair(self, filename):
        """
        Calculates the checksum of file filenamd and then add the new
        checksum/filename entry to the database. If the table does
        not yet exist, then the table is first created and then the checksum
        pair is added. 

        :param filename: The name of the file whose filename/checksum is added
        :type filename: string
        :return: bool -- True if added successfuly. False otherwise.
        """
        pass

    def remove_checksum_pair(self, filename):
        """
        Removes the entry with filename filename in the checksum table. If 
        the checksum pair does not exist in the database or was not removed, 
        the function returns False.

        :param filename: The name of the file whose filename/checksum pair is being removed.
        :type filename: string
        :return: bool -- True if removed successfuly. False otherwise.
        """
        pass

    def get_checksum_pairs(self):
        """
        Returns a list of tuples formated as follows: (filename, hash)

        :return: list -- List of string tuples
                         Return an empty list if no checksum pairs exist
                         or the table/database does not exist.
        """
        pass
    
    def get_abspath(self, filename):
        """
        Returns the absolute path of filename

        :param filename: filename of the file whose path we want to find
        :returns: string -- Absolute path if succssful. None otherwise
        """

        if os.path.exists(filename):
            return os.path.abspath(filename)
        else:
            return  None

if __name__ == '__main__':
    c = ChecksumManager()
    print c.create_checksum_table()


@@ -92,16 +92,67 @@
 nnector%0A
+from checksum_calculator import ChecksumCalculator%0A
 #from va
@@ -1115,24 +1115,80 @@
 = table_name
+%0A        self.checksum_calculator = ChecksumCalculator()
 %0A%0A    def ch
@@ -2249,17 +2249,17 @@
  filenam
-d
+e
  and the
@@ -2632,36 +2632,798 @@
     %22%22%22%0A        
-pass
+if not self. create_checksum_table():%0A            return False%0A        %0A        if len(filename) %3E self.filename_field_length:%0A            return False%0A%0A        checksum = self.checksum_calculator.calculate_checksum(filename)%0A        print checksum%0A        if checksum:%0A            try:%0A                cursor = self.connection.cursor()%0A                sql = %22%22%22INSERT INTO %25s (%0A                filename,checksum) VALUES ('%25s','%25s')%22%22%22 %25 (self.table_name, filename, checksum)%0A                cursor.execute(sql)%0A                self.connection.commit()%0A                return True%0A            except Exception as err:%0A                print(err)%0A                self.connection.rollback()%0A                return False%0A%0A        else:%0A            return False%0A%0A        
 %0A%0A    def remove
@@ -4633,24 +4633,24 @@
 umManager()%0A
-
     print c.
@@ -4672,10 +4672,54 @@
 _table()
+%0A    print c.add_checksum_pair('monitor.py')
 %0A%0A

add checksum_pair