HTMLify

TRX Manager
Views: 127 | Author: abh
from datetime import date
from os import system, listdir
from sys import argv


class Transaction:
    def __init__(self, filepath=None):
        self.filepath = filepath

        entry = open(filepath).read()

        entry = entry.splitlines()

        self.id = int(filepath.split("/")[-1].split(".")[0])

        self.ammount = float(entry[0])
        self.date = date(int(entry[1][6:]), int(entry[1][3:5]), int(entry[1][:2]))
        self.description = entry[2]

    def save(self):
        with open(self.filepath, "w") as f:
            f.write(
                str(self.ammount) + "\n" +
                self.date.strftime("%d/%m/%Y") + "\n" +
                self.description
            )

    def reload(self):
        trx = Transaction(self.filepath)
        self.id = trx.id
        self.ammount = trx.ammount
        self.date = trx.date
        self.description = trx.description


class Manager:
    def __init__(self, dir:str):
        if dir and dir[-1] != "/":
            dir += "/"
        self.dir = dir
        self.transactions: list[Transaction] = []

        self.load_transactions()

    def load_transactions(self):
        n = len(self.transactions)
        while True:
            n += 1
            try:
                trx = Transaction(filepath=self.dir + str(n) + ".txt")
                self.transactions.append(trx)
            except:
                break

    def reload_transactions(self):
        self.transactions.clear()
        self.load_transactions()

    def display_entry(self):
        cb = 0

        print("S no.", " C balance", "  ammount", "Date    ", "Description", sep="\t")
        for entry in self.transactions:

            cb = round(cb + entry.ammount, 2)

            if entry.ammount < 1:
                ams = style["red"]
            else:
                ams = style["green"]

            print(
                str(entry.id).zfill(4)+".",
                sfill(str(cb), 10),
                #(" "*(8-len(str(cb))))+str(cb)+("0" if len(str(cb).split(".")[-1]) == 1 else "")+"    ",
                ams + sfill(str(entry.ammount)+("0" if len(str(entry.ammount).split(".")[-1])==1 else ""), 9)+"" + style["reset"],
                entry.date,
                entry.description,
                sep="\t"
            )

    def new_transaction(self):
        newid = 1
        if self.transactions:
            newid = self.transactions[-1].id + 1
        system("vim "+self.dir+str(newid)+".txt")
        self.load_transactions()
        #if len(self.transactions) >= 2:
        #    if self.transactions[-1].date < self.transactions[-2].date:
        #        self.insert_transaction(self.transactions[-1])

    def edit_transaction(self, id):
        system("vim "+self.dir+str(id)+".txt")
        for entry in self.transactions:
             if entry.id == id:
                entry.reload()

    def insert_transaction(self, transaction: Transaction):
        """This method is not implimented yet"""
        # creating a backup 
        system("mkdir " + self.dir + "backup-trx")
        system("cp *.txt "+ self.dir +"backup-trx")

        #for transaction2 in self.transactions[::-1]:
        #    if transaction2.date <

        for transaction2 in self.transactions[::-1][:-2]:
            if transaction.date <= transaction2.date:
                break
        print(transaction.id)
        print(transaction2.id)

        for trx in self.transactions:
            if trx == transaction:
                continue
            if trx == transaction2:
                trx.filepath = transaction.filepath
                trx.ammount = transaction.ammount
                trx.date = transaction.date
                trx.save()
            if trx.id > transaction2.id:
                trx.filepath = str(id+1)+".txt"
                trx.save()




        #transaction.save()
        # removing backup
        system("rm -r "+self.dir+"backup-trx")

        self.reload_transactions()




# Helper functions

def sfill(s, n):
    return " "*(n-len(s))+s

# 

style = {
    "yellow": "\033[33m",
    "green" : "\033[32m",
    "red"   : "\033[31m",
    "chek"  : "\033[30m",
    "blue"  : "\033[34m",
    "purple": "\033[35m",
    "cyan"  : "\033[36m",
    "reset" : "\033[0m",
}



#manager = Manager("testtrx")
manager = Manager(argv[1] if len(argv) > 1 else ".")
#manager.load_transactions()
#manager.display_entry()

# Intrepeter
while True:
    i = input(">>> ").split(" ")
    if i[0] == "help" or i[0] == "h":
        print(
            "This is trxmanager",
            "available commands:",
            "display: Display entries",
            "new: Create new entry",
            "edit <id>: Edit entry",
            "help, h: Show help",
            "quit, q: Quit",
            sep="\n"
        )
    if i[0] == "q" or i[0] == "quit":
        break
    if i[0] == "new":
        manager.new_transaction()
    if i[0] == "edit":
        manager.edit_transaction(int(i[1]))
    if i[0] == "display":
        manager.display_entry()

print("Exiting..")
quit()

Comments