-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsole.py
executable file
·248 lines (229 loc) · 7.68 KB
/
console.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#!/usr/bin/python3
"""the console module to control the models"""
import cmd
import readline
import sys
import json
import re
from storage import storage
from models.staff import Staff
from models.patient import Patient
from models.doctor import Doctor
from models.nurse import Nurse
from models.pharmacist import Pharmacist
from models.record import RecordOfficer
from models.admin import Admin
from models.notes.consult import Consultation
from models.notes.prescription import Prescription
from models.notes.vitals import VitalSign
from models.notes.nursenote import NurseNote
from models.drug import Drug
class HMIS(cmd.Cmd):
"""class the defines the console object"""
prompt = "(hmis) "
_classes = {
"Staff": Staff,
"Patient": Patient,
"Doctor": Doctor,
"Nurse": Nurse,
"Pharmacist": Pharmacist,
"RecordOfficer": RecordOfficer,
"Admin": Admin,
"Consultation": Consultation,
"Prescription": Prescription,
"VitalSign": VitalSign,
"NurseNote": NurseNote,
"Drug": Drug
}
def do_EOF(self, arg: str) -> None:
"""Exits the interpreter when crtl+D is entered.
USAGE: EOF\n"""
sys.exit(0)
def do_quit(self, arg: str) -> None:
"""Quit command to exit the program\n"""
sys.exit(0)
def emptyline(self):
return
def default(self, line: str) -> None:
"""
called when the command does not match
a defined console function. To process
commands in the format <classname>.<command>(<args>)
"""
pattern = re.compile(r'(\w+)\.(\w+)\(([\S ]*)\)')
res = pattern.findall(line)
if len(res) < 1 or len(res[0]) < 3:
super().default(line)
return
class_name: str = res[0][0]
command: str = res[0][1]
args: str = res[0][2]
if command == "all":
self.onecmd(f"{command} {class_name}")
return
if command == "count":
count = self.do_all(f"{class_name}", count=True)
print(count)
return
if "{" in args:
self.dict_update(class_name, args)
return
if command in ["show", "destroy", "create", "update"]:
self.onecmd(f"{command} {class_name} {args}")
return
super().default(line)
def do_create(self, arg: str) -> None:
"""creates a new instance of a class passed as argument.
USAGE: create <class> or <class_name>.create()"""
args = parse_args(arg)
if validate_args(args, 1) == -1:
return
if args[0] in HMIS._classes:
new_obj = HMIS._classes[args[0]]()
storage.new(new_obj)
print(new_obj.id)
def do_show(self, arg: str) -> None:
"""prints the str representation of an instance.
USAGE: show <class> <id> or <classname>.show(<id>)"""
args = parse_args(arg)
if validate_args(args, 2) == -1:
return
cls = HMIS._classes[args[0]]
obj = storage.get(cls, 'id', args[1])
if obj is not None:
print(obj)
else:
print("** no instance found **")
def do_destroy(self, arg: str) -> None:
"""deletes a given instance from storage.
USAGE: destroy <class> <id> or <classname>.destroy(<id>)"""
args = parse_args(arg)
if validate_args(args, 2) == -1:
return
cls = HMIS._classes[args[0]]
obj = storage.get(cls, 'id', args[1])
if obj is None:
print("** no instance found **")
return
storage.delete(obj)
def do_all(self, arg: str, count=False):
"""prints all instances. USAGE: <classname>.all() or all [class]"""
args = parse_args(arg)
obj_list = []
if len(args) > 0:
if validate_args(args, 1) == -1:
return
cls = HMIS._classes[args[0]]
for obj in storage.all(cls):
obj_list.append(str(obj))
if count:
return len(obj_list)
print(obj_list)
return
for obj in storage.all():
obj_list.append(str(obj))
print(obj_list)
def do_save(self, arg: str):
"""persist the current objects to storage
Gives time to update mandatory attributes that would not
be created automatically before persisting to the db"""
storage.save()
storage.close()
def do_update(self, arg: str) -> None:
"""update the given attribute of a given object.
USAGE: <class name>.update(<id>, <attr name>, '<attr value>')
or <classname>.update(<id>, {attr_dict})"""
args = parse_args(arg)
if validate_args(args, 4) == -1:
return
cls, id = HMIS._classes[args[0]], args[1]
obj = storage.get(cls, 'id', id)
if obj is None:
print("** no instance found **")
return
if len(args) < 3:
print("** attribute name missing **")
return
if len(args) < 4:
print("** value missing **")
return
attr, value = args[2], args[3]
if attr in dir(obj):
attr_type = type(getattr(obj, attr))
if attr_type is not None:
setattr(obj, attr, attr_type(value))
else:
setattr(obj, attr, value)
def dict_update(self, class_name: str, arg: str) -> None:
"""
parsed the args when update command is called
with a dictionary
"""
pattern = re.compile(r'([\w\-]+),\s*(\{.*\})')
res = pattern.findall(arg)
if len(res) < 1:
self.onecmd(f"update {class_name} {arg}")
return
id = res[0][0]
obj_dict = res[0][1]
obj_dict = obj_dict.strip("{}").split(",")
for attr_str in obj_dict:
attr = attr_str.split(":")
name = attr[0].strip(' "')
value = ""
if len(attr) > 1:
value = attr[1].strip(' ').replace('_', ' ')
print(value)
self.onecmd(f"update {class_name} {id} {name} {value}")
def parse_args(arg: str, delim=" ") -> list:
"""parses the arg string into a list of args"""
if arg == "":
return []
args = arg.split(delim)
i = 0
while i < len(args):
curr = args[i].strip(",")
if curr[0] == '"' and curr[-1] != '"':
if i == len(args) - 1:
args[i] = curr.replace('"', '')
break
for j in range(i + 1, len(args)):
next = args[j]
if next[-1] == '"':
break
full = curr
for k in range(i + 1, j + 1):
full += f" {args[k]}"
full = full.strip('"')
full = full.strip("'")
args.insert(i, full)
args_copy = args.copy()
for k in range(i + 1, j + 2):
args.pop(args.index(args_copy[k]))
else:
curr = curr.strip('"')
args[i] = curr.strip("'")
i += 1
return args
def validate_args(args: list, n_args: int) -> int:
"""
validate the completeness of arguments passed
Args:
args: the arg list
n_args: least number of args required by calling command
Return:
0: if passed
-1: if not passed
"""
if len(args) == 0:
print("** class name missing **")
return -1
if args[0] not in HMIS._classes:
print("** class doesn't exist **")
return -1
if (len(args) < 2) and (n_args >= 2):
print("** instance id missing **")
return -1
return 0
if __name__ == "__main__":
HMIS().cmdloop()