-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.py
135 lines (121 loc) · 4.88 KB
/
logger.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
class MsgCreate:
def msg_reserve_init(fn):
def wrapper(self, *arg, **kwarg):
result = fn(self, *arg, **kwarg)
Log.message = F'Created a reservation with id {self._id} of {self._book}'
Log.message += F' from {self._from} to {self._to} for {self._for}.'
return result
return wrapper
def msg_reserve_overlap(fn):
def wrapper(self, other):
result = fn(self, other)
str = 'do'
if not result:
str = 'do not'
Log.message = F'Reservations {self._id} and {other._id} {str} overlap'
return result
return wrapper
def msg_reserve_includes(fn):
def wrapper(self, date):
result = fn(self, date)
str = 'includes'
if not result:
str = 'does not include'
Log.message = F'Reservation {self._id} {str} {date}'
return result
return wrapper
def msg_reserve_identify(fn):
def wrapper(self, date, book, for_):
result = fn(self, date, book, for_)
if result:
Log.message = F'Reservation {self._id} is valid {for_} of {book} on {date}.'
else:
if book != self._book:
Log.message = F'Reservation {self._id} reserves {self._book} not {book}.'
elif for_!=self._for:
Log.message = F'Reservation {self._id} is for {self._for} not {for_}.'
elif not self.includes(date):
Log.message = F'Reservation {self._id} is from {self._from} to {self._to} which '
Log.message += F'does not include {date}.'
return result
return wrapper
def msg_reserve_change_for(fn):
def wrapper(self, for_):
tmp = self._for
result = fn(self, for_)
Log.message = F'Reservation {self._id} moved from {tmp} to {for_}'
return result
return wrapper
def msg_library_init(fn):
def wrapper(self):
result = fn(self)
Log.message = F'Library created.'
return result
return wrapper
def msg_library_add_user(fn):
def wrapper(self, name):
result = fn(self, name)
if result:
Log.message = F'User {name} created.'
else:
Log.message = F'User not created, user with name {name} already exists.'
return result
return wrapper
def msg_library_add_book(fn):
def wrapper(self, name):
result = fn(self, name)
Log.message = F'Book {name} added. We have {self._books[name]} coppies of the book.'
return result
return wrapper
def msg_library_reserve(fn):
def wrapper(self, user, book, date_from, date_to):
result = fn(self, user, book, date_from, date_to)
if result >= 0:
Log.message = F'Reservation {result} included.'
return result
else:
Log.message = F'We cannot reserve book {book} for {user} from {date_from} to {date_to}. '
if user not in self._users:
Log.message += F'User does not exist.'
elif date_from > date_to:
Log.message += F'Incorrect dates.'
elif self._books.get(book, 0) == 0:
Log.message += F'We do not have that book.'
else:
Log.message += F'We do not have enough books.'
return result
return wrapper
def msg_library_check_reserve(fn):
def wrapper(self, user, book, date):
result = fn(self, user, book, date)
str = 'exists'
if not result:
str = 'does not exist'
Log.message = F'Reservation for {user} of {book} on {date} {str}.'
return result
return wrapper
def msg_library_change_reserve(fn):
def wrapper(self, user, book, date, new_user):
result = fn(self, user, book, date, new_user)
if result:
Log.message = F'Reservation for {user} of {book} on {date} changed to {new_user}.'
else:
if new_user not in self._users:
Log.message = F'Cannot change the reservation as {new_user} does not exist.'
else:
Log.message = F'Reservation for {user} of {book} on {date} does not exist.'
return result
return wrapper
class Log:
message = ""
def logger(fn):
def wrapper(self, *arg, **kwarg):
result = fn(self, *arg, **kwarg)
print(Log.message)
return result
return wrapper
def enable_logging(cls):
for name, value in vars(cls).items():
if callable(value):
setattr(cls, name, Log.logger(value))
return cls