-
Notifications
You must be signed in to change notification settings - Fork 0
/
mvc.py
200 lines (149 loc) · 4.97 KB
/
mvc.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
# This very rough snippet is a implementation of MVC design pattern with a routing class routing to
# different controller classes linking to different models and views.
#
# The Controller, Model and Views classes are subclasses from abstract base classes
#!/usr/bin/env python3
from abc import ABC, abstractmethod
from collections.abc import Iterable
from sys import argv
class Model(ABC, Iterable):
_instances = []
@abstractmethod
def __iter__(self):
return iter(getattr(self, '_instances', []))
@abstractmethod
def get(self, item):
...
class TestModel(Model):
test_items = {
"one": { "name": "1" },
"two": { "name": "2" },
}
def __iter__(self):
yield from self.test_items
def get(self, item):
try:
return self.test_items[item]
except KeyError as key_err:
print("Key cannot be found", key_err)
class DummyModel(Model):
test_items = {
"one": { "name": "1" },
"two": { "name": "2" },
}
def __iter__(self):
yield from self.test_items
def get(self, item):
try:
return self.test_items[item]
except KeyError as key_err:
print("Key cannot be found", key_err)
class View(ABC):
@abstractmethod
def show_item_list(self, item_list):
...
@abstractmethod
def show_item_information(self, item_name, item_info):
...
@abstractmethod
def item_not_found(self, item_name):
...
class TestView(View):
def show_item_list(self, item_list):
print(item_list)
for item in item_list:
print(item)
print("")
def show_item_information(self, item_name, item_info):
print(f" INFORMATION of username {item_name} \n")
labels = ""
printout = ""
for key, value in item_info.items():
labels += (str(key)).upper() + "\t"
print(labels)
for key, value in item_info.items():
printout += str(value) + "\t\t"
print(printout)
def item_not_found(self, item_name):
print(f'That "{item_name}" does not exist in the records')
class DummyView(View):
def show_item_list(self, item_list):
print(item_list)
for item in item_list:
print(item)
print("")
def show_item_information(self, item_name, item_info):
print(f" INFORMATION of username {item_name} \n")
labels = ""
printout = ""
for key, value in item_info.items():
labels += (str(key)).upper() + "\t"
print(labels)
for key, value in item_info.items():
printout += str(value) + "\t\t"
print(printout)
def item_not_found(self, item_name):
print(f'That "{item_name}" does not exist in the records')
class BaseController(ABC):
@abstractmethod
def show_items(self):
pass
@abstractmethod
def show_item_information(self, item_name):
pass
class TestController(BaseController):
def __init__(self, model, view):
self.model = model
self.view = view
def show_items(self):
items = list(self.model)
#item_type = self.model.item_type
self.view.show_item_list(items)
def show_item_information(self, item_name):
try:
item_info = self.model.get(item_name)
except Exception:
item_type = self.model.item_type
self.view.item_not_found(item_type, item_name)
else:
item_type = self.model.item_type
self.view.show_item_information(item_type, item_name, item_info)
class DummyController(BaseController):
def __init__(self, model, view):
self.model = model
self.view = view
def show_items(self):
items = list(self.model)
#item_type = self.model.item_type
self.view.show_item_list(items)
def show_item_information(self, item_name):
try:
item_info = self.model.get(item_name)
except Exception:
item_type = self.model.item_type
self.view.item_not_found(item_type, item_name)
else:
item_type = self.model.item_type
self.view.show_item_information(item_type, item_name, item_info)
class Router:
def __init__(self):
self.routes = {}
def register(self, route, controller, model, view):
model = model(); view = view()
self.routes[route] = controller(model, view)
def resolve(self, route):
if self.routes.get(route):
controller = self.routes[route]
return controller
else:
raise ValueError("Cannot find route", route)
def main():
router = Router()
router.register("test", TestController, TestModel, TestView)
router.register("dummy", DummyController, DummyModel, DummyView)
request = argv[1]
controller = router.resolve(request)
if hasattr(controller, "show_items"):
controller.show_items()
if __name__ == '__main__':
main()