Repository navigation
Expand file tree
/
Copy pathtask-cli.py
More file actions
129 lines (110 loc) · 3.8 KB
/
Copy pathtask-cli.py
File metadata and controls
129 lines (110 loc) · 3.8 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import json
import argparse
import os
import datetime
# Save function
def save():
with open(name_tracker, 'w') as archivo:
json.dump(js, archivo, indent=4)
# Add function
def add(desc: str):
if js: # Si existe entonces buscamos el mayor indice
indice = max(int(key) for key in js.keys())
else: # Sino, comenzamos en 0
indice = 0
id = indice+1
js[id] = {"desc":desc,
"status":"Todo",
"createdAt":str(datetime.datetime.now().date()),
"updatedAt":0
}
print(f"Task added successfully (ID: {id})")
# Save
save()
# Update
def update(id:str, new_desc:str):
try:
old_desc = js[id]["desc"]
js[id]["desc"] = new_desc
js[id]["updatedAt"] = str(datetime.datetime.now().date())
print("Description changed:")
print(f"'{old_desc}'\nto\n'{new_desc}'")
# Guardamos
save()
except KeyError:
print("Task not found")
# Delete
def delete(id:str):
try:
info = js[id]["desc"]
del js[id]
print(f"Deleted task {id} with description: {info}")
save()
except KeyError:
print("Task not found")
#mark in progresss
def mark_in_progress(id:str):
try:
js[id]["status"] = "In progress"
print(f"Task {id} is in progress now!")
save()
except KeyError:
print("Task not found")
# Mark done
def mark_done(id: str):
try:
js[id]["status"] = "Done"
print(f"Task {id} is done now!")
save()
except KeyError:
print("Task not found")
# List
def listar(mode="All"):
if mode == "In-progress":
for number,info in js.items():
if js[number]["status"] == "In progress":
print(f"{number}: {info['desc']} ({info['status']})")
elif mode == "Todo":
for number,info in js.items():
if js[number]["status"] == "Todo":
print(f"{number}: {info['desc']} ({info['status']})")
elif mode == "Done":
for number,info in js.items():
if js[number]["status"] == "Done":
print(f"{number}: {info['desc']} ({info['status']})")
elif mode =="All":
for number,info in js.items():
print(f"{number}: {info['desc']} ({info['status']})")
if __name__ == "__main__":
name_tracker = "tracker.json" # Nombre predeterminado
# Creamos diccionario del json
if os.path.exists(name_tracker): # Si ya existe lo cargamos
with open(name_tracker, "r") as file:
js = json.load(file)
else:
with open(name_tracker, "w") as file: # Si no existe creamos el diccionario
js = {}
# Parser
parser = argparse.ArgumentParser(description="ClI Tracker")
# Arguments
parser.add_argument("-A","--add", type=str, help="Add a task")
parser.add_argument("-U","--update", type=str,nargs=2, metavar=('Task_id', 'Desc'), help="Update a task")
parser.add_argument("-D","--delete", type=str, help="Delete a task")
parser.add_argument("-I","--inprogress", type=str, help="Mark like 'in progress' that task")
parser.add_argument("--done", type=str, help="Mark like 'done' that task")
parser.add_argument("-L",'--listar',type=str,choices=['Done', 'Todo', 'In-progress', "All"],
default='All',help='List tasks')
args = parser.parse_args()
# Arguments
if args.add:
add(args.add)
elif args.update:
update(args.update[0], args.update[1])
elif args.delete:
delete(args.delete)
elif args.inprogress:
mark_in_progress(args.inprogress)
elif args.done:
mark_done(args.done)
elif args.listar:
listar(args.listar)