-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathclient.py
More file actions
87 lines (75 loc) · 2.33 KB
/
client.py
File metadata and controls
87 lines (75 loc) · 2.33 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
import json
clientFile = "clients.json"
clients = {}
def loadClients():
_clearClients()
global clients
try:
with open(clientFile, encoding='utf8') as file:
data = json.load(file)
except FileNotFoundError:
data = []
for client in data:
_addClient(client)
def clientsList():
data = []
for matricules in clients:
client = dict(clients[matricules])
client['matricules'] = list(matricules)
data.append(client)
return data
def saveClients():
data = clientsList()
with open(clientFile, 'w', encoding='utf8') as file:
json.dump(data, file, indent='\t')
def _clearClients():
global clients
clients = {}
def clearClients():
_clearClients()
saveClients()
def _addClient(data):
clients[frozenset(data['matricules'])]=data
del(data['matricules'])
def addClient(data):
if 'matricules' not in data:
raise ValueError('You must define the "matricules" key')
if type(data['matricules']) != list:
raise ValueError('"matricules" must be a list')
if len(data['matricules']) < 1:
raise ValueError('"matricules" list must contain at least one matricule')
if len(data['matricules']) > 2:
raise ValueError('"matricules" list must contain at most two matricule')
if any(map(lambda x: type(x) != str, data['matricules'])):
raise ValueError('"matricules" list must contain strings')
if 'name' not in data:
raise ValueError('You must define the "name" key')
if type(data['name']) != str:
raise ValueError('"name" must be a string')
if any(map(lambda x: x[1]['name'] == data['name'], clients.items())):
raise ValueError(f'Name "{data["name"]}" already used')
if 'port' not in data:
raise ValueError('You must define the "port" key')
if type(data['port']) != int:
raise ValueError('"port" must be a int')
if 'ip' not in data:
raise ValueError('You must define the "ip" key')
if type(data['ip']) != str:
raise ValueError('"ip" must be a str')
_addClient(data)
saveClients()
def removeClient(name):
toDel = None
for key, value in clients.items():
if value['name'] == name:
toDel = key
break
if toDel is not None:
del(clients[toDel])
saveClients()
if __name__ == '__main__':
#addClient({'matricules': ['12345', '54321'], 'name': 'lur', 'port': 8080, 'ip': '1.2.3.4'})
#addClient({'matricules': ['12346', '64321'], 'name': 'lrg', 'port': 8080, 'ip': '4.3.2.1'})
#loadClients()
#rint(clientsJSON())
clearClients()