-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcar_registration.py
More file actions
executable file
·164 lines (131 loc) · 4.77 KB
/
car_registration.py
File metadata and controls
executable file
·164 lines (131 loc) · 4.77 KB
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
#!/usr/bin/env python3
# Copyright (c) 2008-11 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version. It is provided for educational
# purposes and is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
import collections
import pickle
import socket
import struct
import sys
import Console
Address = ["localhost", 9653]
CarTuple = collections.namedtuple("CarTuple", "seats mileage owner")
class SocketManager:
def __init__(self, address):
self.address = address
def __enter__(self):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect(self.address)
return self.sock
def __exit__(self, *ignore):
self.sock.close()
def main():
if len(sys.argv) > 1:
Address[0] = sys.argv[1]
call = dict(c=get_car_details, m=change_mileage, o=change_owner,
n=new_registration, s=stop_server, q=quit)
menu = ("(C)ar Edit (M)ileage Edit (O)wner (N)ew car "
"(S)top server (Q)uit")
valid = frozenset("cmonsq")
previous_license = None
while True:
action = Console.get_menu_choice(menu, valid, "c", True)
previous_license = call[action](previous_license)
def retrieve_car_details(previous_license):
license = Console.get_string("License", "license",
previous_license)
if not license:
return previous_license, None
license = license.upper()
ok, *data = handle_request("GET_CAR_DETAILS", license)
if not ok:
print(data[0])
return previous_license, None
return license, CarTuple(*data)
def get_car_details(previous_license):
license, car = retrieve_car_details(previous_license)
if car is not None:
print("License: {0}\nSeats: {seats}\nMileage: {mileage}\n"
"Owner: {owner}".format(license, **car._asdict()))
return license
def change_mileage(previous_license):
license, car = retrieve_car_details(previous_license)
if car is None:
return previous_license
mileage = Console.get_integer("Mileage", "mileage",
car.mileage, 0)
if mileage == 0:
return license
ok, *data = handle_request("CHANGE_MILEAGE", license, mileage)
if not ok:
print(data[0])
else:
print("Mileage successfully changed")
return license
def change_owner(previous_license):
license, car = retrieve_car_details(previous_license)
if car is None:
return previous_license
owner = Console.get_string("Owner", "owner", car.owner)
if not owner:
return license
ok, *data = handle_request("CHANGE_OWNER", license, owner)
if not ok:
print(data[0])
else:
print("Owner successfully changed")
return license
def new_registration(previous_license):
license = Console.get_string("License", "license")
if not license:
return previous_license
license = license.upper()
seats = Console.get_integer("Seats", "seats", 4, 0)
if not (1 < seats < 10):
return previous_license
mileage = Console.get_integer("Mileage", "mileage", 0, 0)
owner = Console.get_string("Owner", "owner")
if not owner:
return previous_license
ok, *data = handle_request("NEW_REGISTRATION", license, seats,
mileage, owner)
if not ok:
print(data[0])
else:
print("Car {0} successfully registered".format(license))
return license
def quit(*ignore):
sys.exit()
def stop_server(*ignore):
handle_request("SHUTDOWN", wait_for_reply=False)
sys.exit()
def handle_request(*items, wait_for_reply=True):
SizeStruct = struct.Struct("!I")
data = pickle.dumps(items, 3)
try:
with SocketManager(tuple(Address)) as sock:
sock.sendall(SizeStruct.pack(len(data)))
sock.sendall(data)
if not wait_for_reply:
return
size_data = sock.recv(SizeStruct.size)
size = SizeStruct.unpack(size_data)[0]
result = bytearray()
while True:
data = sock.recv(4000)
if not data:
break
result.extend(data)
if len(result) >= size:
break
return pickle.loads(result)
except socket.error as err:
print("{0}: is the server running?".format(err))
sys.exit(1)
main()