-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.py
123 lines (94 loc) · 3.71 KB
/
command.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
from abc import ABC, abstractmethod
from colorama import Fore
from note import Note
from notes_manager import NoteManager
from commandQueue import CommandQueue
import json
class Command(ABC):
@abstractmethod
def execute(self) -> None:
pass
@abstractmethod
def undo(self) -> None:
pass
@abstractmethod
def to_dict(self) -> dict:
pass
class CreateNoteCommand(Command):
def __init__(self, title: str, content: str, noteManager: NoteManager) -> None:
self._title = title
self._content = content
self._noteManager = noteManager
def execute(self) -> None:
new_note = Note(self._title, self._content)
if self._noteManager.insert(new_note):
print(f"""created a note
title : {self._title}
content : {self._content}
i.d. : {new_note._id}""")
else:
print(Fore.LIGHTYELLOW_EX +
f"title = {new_note._title} for a note already exists\nIf you want to delete it please use 'delete'" +
Fore.RESET)
def undo(self) -> None:
self._noteManager.remove(self._title)
def to_dict(self) -> dict:
return {"type": self.__class__.__name__, "title": self._title, "content": self._content}
class ViewNoteCommand(Command):
map_func = {"int": "view_note_by_id",
"str": "view_note_by_title"}
def __init__(self, identifier, noteManager: NoteManager) -> None:
self._identifier = identifier
self._noteManager: NoteManager = noteManager
def execute(self) -> None:
identifier_type = self._identifier.__class__.__name__
if identifier_type in ViewNoteCommand.map_func:
getattr(self, ViewNoteCommand.map_func[identifier_type])(
self._identifier)
else:
print("identifier ERORR")
def view_note_by_id(self, id: int):
if id in self._noteManager._note_index:
note = self._noteManager._note_list_by_title[self._noteManager._note_index[id]]
print(json.dumps(note.__dict__, indent=4))
else:
print(f"ERROR: Note with ID = {id} not found")
def view_note_by_title(self, title: str):
if title in self._noteManager._note_list_by_title:
print(json.dumps(
self._noteManager._note_list_by_title[title].__dict__, indent=4))
else:
print(f"ERROR: Note with title = {title} not found")
def undo(self) -> None:
pass
def to_dict(self) -> dict:
return {"type": self.__class__.__name__, "identifier": self._identifier}
class DeleteNoteCommand(Command):
def __init__(self, identifier, noteManager: NoteManager) -> None:
self._identifier = identifier
self._noteManager: NoteManager = noteManager
self._note = None
def execute(self) -> None:
self._note = self._noteManager.remove(self._identifier)
# if note
def undo(self) -> None:
if self._note:
CreateNoteCommand(self._note._title,
self._note._content, self._noteManager).execute()
else:
pass # todo
def to_dict(self) -> dict:
return {"type": self.__class__.__name__, "identifier": self._identifier}
class UndoLestCommand(Command):
def __init__(self, commandQueue: CommandQueue) -> None:
self._CommandQueue: CommandQueue = commandQueue
def execute(self) -> None:
self._CommandQueue.dequeue()
def undo(self) -> None:
self._CommandQueue.dequeue()
def to_dict(self) -> dict:
return {"type": self.__class__.__name__}
if __name__ == "__main__":
nm = NoteManager("notes.json")
cm = ViewNoteCommand("dwcs", nm)
cm.execute()