-
Notifications
You must be signed in to change notification settings - Fork 0
/
hedef.py
166 lines (119 loc) · 4.28 KB
/
hedef.py
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
import json
import sys
EXIT_SUCCESS = 0
EXIT_FAILURE = 1
FILENAME = "goals.json"
class Hedef:
next_goal_id = 1
goals = []
def __init__(self, goal_id, goal_name, goal_completed=False):
self.goal_id = goal_id
self.goal_name = goal_name
self.goal_completed = goal_completed
def __str__(self):
status = "X" if self.goal_completed else "."
return f"[{status}] {self.goal_id} {self.goal_name}"
def load_goals():
Hedef.goals = []
Hedef.next_goal_id = 1
try:
with open(FILENAME, "r") as file:
goals_list = json.loads(file.read())
for goal in goals_list:
Hedef.goals.append(Hedef(goal["goal_id"], goal["goal_name"], goal["goal_completed"]))
if Hedef.goals:
Hedef.next_goal_id = max([goal.goal_id for goal in Hedef.goals]) + 1
else:
Hedef.next_goal_id = 1
except FileNotFoundError:
Hedef.goals = []
Hedef.next_goal_id = 1
return Hedef.goals
def save_goals():
with open(FILENAME, "w") as file:
json.dump([{"goal_id": goal.goal_id, "goal_name": goal.goal_name, "goal_completed": goal.goal_completed} for goal in Hedef.goals], file, indent=4)
try:
Hedef.next_goal_id = max([goal.goal_id for goal in Hedef.goals]) + 1
except ValueError:
Hedef.next_goal_id = 1
def add_goal(goal_name):
Hedef.goals = load_goals()
for goal in Hedef.goals:
if goal.goal_name == goal_name:
print(f"The goal already exists: {goal}")
sys.exit(EXIT_FAILURE)
goal = Hedef(Hedef.next_goal_id, goal_name, False)
Hedef.goals.append(goal)
save_goals()
print(f"The goal has been appended: {goal}")
def list_goals():
Hedef.goals = load_goals()
for goal in Hedef.goals:
print(goal)
def complete_goal(goal_id):
Hedef.goals = load_goals()
try:
goal_id = int(goal_id)
except ValueError:
print(f"Invalid goal id number to complete: {goal_id}")
sys.exit(EXIT_FAILURE)
for goal in Hedef.goals:
if goal.goal_id == goal_id:
if goal.goal_completed:
print(f"The goal is already completed: {goal}")
sys.exit(EXIT_FAILURE)
goal.goal_completed = True
break
else:
print(f"Invalid goal id number to complete: {goal_id}")
sys.exit(EXIT_FAILURE)
save_goals()
print(f"The goal has been completed: {goal}")
def delete_goal(goal_id):
Hedef.goals = load_goals()
try:
goal_id = int(goal_id)
except ValueError:
print(f"Invalid goal id number to delete: {goal_id}")
sys.exit(EXIT_FAILURE)
for goal in Hedef.goals:
if goal.goal_id == goal_id:
Hedef.goals.remove(goal)
break
else:
print(f"Invalid goal id number to delete: {goal_id}")
sys.exit(EXIT_FAILURE)
save_goals()
print(f"The goal has been removed: {goal}")
def main(args):
if len(args) == 2 or len(args) == 3:
command = args[1]
if command == "add":
if len(args) != 3:
print("Invalid argument count! Usage: python/python3 hedef.py add <goal_name>")
return EXIT_FAILURE
add_goal(args[2])
elif command == "list":
if len(args) != 2:
print("Invalid argument count! Usage: python/python3 hedef.py list")
return EXIT_FAILURE
list_goals()
elif command == "complete":
if len(args) != 3:
print("Invalid argument count! Usage: python/python3 hedef.py complete <goal_id>")
return EXIT_FAILURE
complete_goal(args[2])
elif command == "delete":
if len(args) != 3:
print("Invalid argument count! Usage: python/python3 hedef.py delete <goal_id>")
return EXIT_FAILURE
delete_goal(args[2])
else:
print("Invalid command! Usage: python/python3 hedef.py <add|list|complete|delete>")
return EXIT_FAILURE
else:
print("Invalid argument count! Usage: python/python3 hedef.py <add|list|complete|delete>")
return EXIT_FAILURE
return EXIT_SUCCESS
if __name__ == "__main__":
sys.exit(main(sys.argv))