Dashboard Temp Share Shortlinks Frames API

HTMLify

TRX Manager
Views: 550 | Author: abh
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
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()