Skip to content

Dev MasterControl.py

Tarun Aditya Thurlapati edited this page Apr 24, 2020 · 3 revisions

Code Explanation - MasterControl.py

Imports

from .Models import ToDoData
import os

This boolean tells us if the database is in operation:

DatabaseInOperation = False

Import Database

This function is executed when the database is imported by the user:

  1. It also toggles the DatabaseInOperation variable.
  2. It writes the data into Temp.db
  3. It then executes This function in ToDoData.py
def ImportToDoData(CurrentDir,rfile,contentLength):
    with open(os.path.join(CurrentDir,'Models\Temp.db'),mode='wb') as file: # b is important -> binary
        file.seek(0)
        file.truncate()
        file.write(rfile.read(contentLength))
        if ToDoData.InitTodoFrame():
            global DatabaseInOperation
            DatabaseInOperation = True
            return "The Database was imported successfully"
        else:
            return "There was an error importing the database"

Creates new database

This function reads the template database and then writes to Temp.db

  1. It also toggles the DatabaseInOperation variable.
  2. It then executes This function in ToDoData.py
def NewToDoDatabase(CurrentDir):
    ToDoFileData = None
    with open(os.path.join(CurrentDir,'Models\ToDo.db'), mode='rb') as ToDofile: # b is important -> binary
        ToDoFileData = ToDofile.read()
    with open(os.path.join(CurrentDir,'Models\Temp.db'),mode='wb') as Tempfile: # b is important -> binary
        Tempfile.seek(0)
        Tempfile.truncate()
        Tempfile.write(ToDoFileData)
    if ToDoData.InitTodoFrame():
        global DatabaseInOperation
        DatabaseInOperation = True
        return "New Todo database created successfully"
    else:
        return "There was an error in creating the database"

Get list Data

This the function which returns all the list data encoded as a string in a custom format to the calling function. DBI: Database item DBG: Database Group DBP: Database Item Property

def GetListDataAsString():
    fileContent = ""
    for x in ToDoData.TF.todoList:
        if x[2] == 31 or x[2] == 30:
            continue
        if x[2] == 2:
            fileContent+="DBG:"+str(x[1])
        else:
            fileContent+="DBI:"+"DBP:"+str(x[0])+"DBP:"+str(x[1])+"DBP:"+str(x[2])+"DBP:"+str(x[3])
    return fileContent

Add Item To List

This functions takes the data which it gets as an argument and decodes it since it is in a custom formatted string and then interprets it to modify the list as required and return True is successful and False If not.

def AddItemToList(Data):
    try:
        group = int(str(Data).split('ITEM:')[0].split('DBG:')[1])
        grpStartNO = 1 #temporary assignment
        grp = 0
        for i in ToDoData.TF.todoList:
            if int(i[2])==2:
                grp+=1
            if grp == group:
                ToDoData.TF.todoList.insert(i[0]+1,[0,str(Data).split('ITEM:')[1],0,grp-1]) #the 0 in the list is arbitrary
                break
        for i in range(len(ToDoData.TF.todoList)):
            ToDoData.TF.todoList[i][0] = i
        return "True"
    except:
        return "False"

Delete task

This functions takes the data which it gets as an argument and decodes it since it is in a custom formatted string and then interprets it to modify the list as required and return True is successful and False If not.

def DeleteItem(Data):
    try:
        ToDoData.TF.todoList.pop(int(str(Data).split('DBI:')[1]))
        for i in range(len(ToDoData.TF.todoList)):
            ToDoData.TF.todoList[i][0] = i
        return "True"
    except:
        return "False"

Edit task name

This functions takes the data which it gets as an argument and decodes it since it is in a custom formatted string and then interprets it to modify the list as required and return True is successful and False If not.

def EditItem(Data):
    try:
        tempVar = str(Data).split('DBI:')[1].split('EDIT:')
        ToDoData.TF.todoList[int(tempVar[0])][1] = str(tempVar[1])
        return "True"
    except:
        return "False"

Done Or Undone a task

This functions takes the data which it gets as an argument and decodes it since it is in a custom formatted string and then interprets it to modify the list as required and return True is successful and False If not.

def DoneOrUndone(Data):
    try:
        tempVar = str(Data).split('DBI:')[1].split('DONE:')
        ToDoData.TF.todoList[int(tempVar[0])][2] = int(tempVar[1])
        return "True"
    except:
        return "False"

Edit List Name

This functions takes the data which it gets as an argument and decodes it since it is in a custom formatted string and then interprets it to modify the list as required and return True is successful and False If not.

def EditListName(Data):
    try:
        group = int(str(Data).split('EDIT:')[0])
        grpStartNO = 1 #temporary assignment
        grp = 0
        for i in ToDoData.TF.todoList:
            if int(i[2])==2:
                grp+=1
            if grp == group:
                i[1] = (str(Data).split('EDIT:')[1]) #the 0 in the list is arbitrary
                break
        return "True"
    except:
        return "False"

Delete List

This functions takes the data which it gets as an argument and decodes it since it is in a custom formatted string and then interprets it to modify the list as required and return True is successful and False If not.

def DeleteList(Data):
    try:
        delete = int(Data)-1
        for i in ToDoData.TF.todoList:
            if int(i[3])==delete:
                start = i[0]
                break
        while True:
            if ToDoData.TF.todoList[start][3]!=delete:
                break
            ToDoData.TF.todoList.pop(start)
        for i in range(len(ToDoData.TF.todoList)):
            ToDoData.TF.todoList[i][0] = i
        grp = -1
        for i in range(1,len(ToDoData.TF.todoList)-1):
            if ToDoData.TF.todoList[i][2]==2:
                grp+=1
            ToDoData.TF.todoList[i][3] = grp
        return "True"
    except:
        return "False"

Add List

This functions takes the data which it gets as an argument and decodes it since it is in a custom formatted string and then interprets it to modify the list as required and return True is successful and False If not.

def AddList(Data):
    try:
        grp = -1
        for i in ToDoData.TF.todoList:
            if int(i[2])==2:
                grp+=1
        ToDoData.TF.todoList.insert(len(ToDoData.TF.todoList)-1,[0,str(Data),2,grp+1])
        for i in range(len(ToDoData.TF.todoList)):
            ToDoData.TF.todoList[i][0] = i
        return "True"
    except:
        return "False"

Other Functions:

Print List to terminal as table

This functions prints the List to terminal as table so that we can take down the way the list looks at any one point.

def DispToDoDataFrame():
    print("~~~~~~~~~~~~~~~~~~~~~~~~")
    for x in ToDoData.TF.todoList:
        print(x)
    print("~~~~~~~~~~~~~~~~~~~~~~~~")

Destroy the List and close DB connection

This function destroys the List and close DB connection using ToDoData.CloseToDoFrame().

  1. it also toggles DatabaseInOperation
  2. it also erases Temp.db
def DestroyToDoDBFrame(CurrentDir):
    try:
        with open(os.path.join(CurrentDir,'Models\Temp.db'),mode='wb') as file: # b is important -> binary
            file.seek(0)
            file.truncate()
        global DatabaseInOperation
        if DatabaseInOperation:
            ToDoData.CloseToDoFrame()
            DatabaseInOperation = False
        return "True"
    except:
        return "False"

Shutdown app

This functions is evoked when the app is required to shutdown.

  1. it closes dataframe, if the app is in operation
  2. it also erases Temp.db.
def Shutdown(CurrentDir):
    with open(os.path.join(CurrentDir,'Models\Temp.db'),mode='wb') as file: # b is important -> binary
        file.seek(0)
        file.truncate()
    global DatabaseInOperation
    if DatabaseInOperation:
        ToDoData.CloseToDoFrame()
        DatabaseInOperation = False
    return "Cleared Trash."

Write data to Temp.db

This function is called when the app needs to export the data file, before which it writes all changes to the list to the data file using the ExecuteCommandOnToDoDB function (Which is imported directly into ToDoData.py from SQLiteDBMS.py).

def WriteToTempDB():
    ToDoData.ExecuteCommandOnToDoDB('DROP TABLE TODO')
    ToDoData.ExecuteCommandOnToDoDB('''CREATE TABLE TODO (ID INT PRIMARY KEY NOT NULL,
    TASK TEXT NOT NULL, DONE INT NOT NULL, LISTNO INT NOT NULL);''')
    for i in ToDoData.TF.todoList:
        ToDoData.ExecuteCommandOnToDoDB("INSERT INTO TODO VALUES({id}, '{Task}',{Done}, {Listno})"\
            .format(id = i[0],Task = i[1],Done = i[2],Listno = i[3]))
    print("~Data successfully written from python list to Temp.db")