-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemory-tracker.py
More file actions
114 lines (85 loc) · 3.61 KB
/
Copy pathmemory-tracker.py
File metadata and controls
114 lines (85 loc) · 3.61 KB
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
import argparse
import sys
import csv
import os
from datetime import date
from rich.console import Console
from rich.table import Table
csv_path = "./memory.csv" # Path to CSV files that contains memories
console = Console()
if os.path.exists(csv_path):
raise FileNotFoundError("Failed to locate the csv file (memory.csv)")
# This function should return the value of arguments
def argument_handler():
# Setting up argparse
parser = argparse.ArgumentParser(
prog="Sticky Memory",
description="Program that helps remind you of memories that are important to remember!",
epilog="Bye lol"
)
parser.add_argument("-o", "--option", default="write", nargs=1)
parser.add_argument("-c", "--category", default=None)
parser.add_argument("-d", "--date", default=date.today())
parser.add_argument("memory", nargs="?")
args = parser.parse_args() # Argument values
# Checking if there's no memory inserted in the arguments while the option is write
if args.memory is None and args.option == ["write"]:
console.print("Please insert memory you need to remember (python memory-tracker.py -h)")
console.print("Terminating program. . .")
raise SystemExit(1)
return parser.parse_args()
def main():
args = argument_handler() # Getting the argument values
# Compare options, and call function
match args.option:
case "fetch" | ["fetch"]:
fetch_memory(csv_path, args.category)
case "write" | ["write"]:
input_memory(csv_path, args.memory, args.category, args.date)
case "forget" | ["forget"]:
forget_memory(csv_path, args.memory)
case _:
console.print("Invalid options. Option has write and fetch only", style="red bold")
sys.exit("Terminating program..")
# Remembers a memory
def input_memory(csv_path, memory, category, time):
with open(csv_path, "a") as file:
writer = csv.DictWriter(file, fieldnames=["memory", "category", "datetime"])
writer.writerow({"memory": memory, "category": category, "datetime": time})
console.print("Got it! I'll remember that for you.", style="green bold")
# Fetching the memory to output
def fetch_memory(csv_path, category):
with open(csv_path) as file:
reader = csv.DictReader(file)
table = Table(title="Your memories (According to my research 🤓)")
table.add_column("Memory", style="bold")
table.add_column("Category", style="blue")
table.add_column("Date", style="purple")
for row in reader:
if row["category"] == category:
table.add_row(row["memory"], row["category"], row["datetime"])
elif category == None:
table.add_row(row["memory"], row["category"], row["datetime"])
console.print(table)
# Forgeting certain memory
def forget_memory(csv_path, memory):
csv_content = []
# Reading the CSV file and save it into csv_content
with open(csv_path) as file:
reader = csv.DictReader(file)
for row in reader:
if row["memory"] == memory:
continue
csv_content.append({
"memory": row["memory"],
"category": row["category"],
"datetime": row["datetime"]
})
# rewrite the entire CSV file
with open(csv_path, "w") as file:
writer = csv.DictWriter(file, fieldnames=["memory", "category", "datetime"])
writer.writeheader()
writer.writerows(csv_content)
console.print("Successfully dropped memory", style="green bold")
if __name__ == "__main__":
main()