-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaskobj.py
76 lines (64 loc) · 2.24 KB
/
taskobj.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
import logging
import json
import os
class Task:
def __init__(self, id: str, name: str, status: str):
self.id = id
self.name = name
self.status = status
def update(self) -> None:
pass
def to_dict(self) -> dict:
return self.__dict__
@staticmethod
def from_string(string: str):
task_dict = dict()
try:
task_dict = json.loads(string)
except json.decoder.JSONDecodeError:
logging.error(f"Wrong query {string=}, not json format")
try:
task_obj = Task(
id=task_dict['id'],
name=task_dict['name'],
status=task_dict['status']
)
return task_obj
except KeyError:
logging.error(f"Wrong key found in query {string=}")
class TaskFile:
tasks_filename = 'tasks.json'
def __init__(self, filename=tasks_filename):
self.filename = filename
if not (os.path.exists(filename) and os.path.isfile(filename)):
logging.warning(f"taskfile do not exist, creating new with name {self.filename}")
with open(self.filename, 'w') as file:
json.dump(list(), file)
def get_tasks(self) -> list:
tasks = list()
with open(self.filename, 'r') as file:
for task in json.load(file):
tasks.append(
Task(
task['id'],
task['name'],
task['status']
)
)
return tasks
def append_task(self, new_task: Task) -> None:
tasks = self.get_tasks()
tasks.append(new_task)
self.__write(tasks)
def update_task(self, task_id: int) -> None:
tasks = self.get_tasks()
tasks[task_id].update()
self.__write(tasks)
def pop_task(self, task_id: int) -> None:
tasks = self.get_tasks()
tasks.pop(task_id)
self.__write(tasks)
def __write(self, task_list: list) -> None:
new_task_list = [task.to_dict() for task in task_list]
with open(self.filename, 'w') as file:
json.dump(new_task_list, file)