-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontacts.py
More file actions
116 lines (95 loc) · 3.32 KB
/
Copy pathcontacts.py
File metadata and controls
116 lines (95 loc) · 3.32 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
import json
import os
CONTACTS_FILE = "contacts.json"
DEFAULT_NAME = "JohnDoe"
class Contact:
"""A class for contact"""
def __init__(
self,
host: str,
port: int,
username: str = DEFAULT_NAME
):
self.host = host
self.port = port
self.username = username
self.self = (host, port)
def __eq__(self, other):
if isinstance(other, Contact):
return (self.host == other.host
and self.port == other.port)
return False
def __repr__(self):
return f"Contact:{self.username} ({self.host}:{self.port})"
def to_dict(self):
"""Casts this object to dict for JSON serialization"""
return {
"host": self.host,
"port": self.port,
"username": self.username
}
@classmethod
def from_dict(cls, data: dict):
"""Creates a friend from JSON data"""
return cls(data["host"], data["port"], data["username"])
def __hash__(self):
return hash(self.self)
class Contacts:
"""A class for contacts"""
def __init__(self):
self.contacts = list[Contact]()
self.load_contacts()
def load_contacts(self):
"""Loads contacts from file"""
if not os.path.exists(CONTACTS_FILE):
return
try:
with open(CONTACTS_FILE, "r") as f:
content = f.read()
if content.strip():
loaded_data = json.loads(content)
self.contacts = [
Contact.from_dict(item) for item in loaded_data
]
else:
self.contacts = []
except json.JSONDecodeError:
print(f"Can't load from '{CONTACTS_FILE}'")
self.contacts = []
def save_contacts(self):
"""Saves contacts to file"""
with open(CONTACTS_FILE, "w") as f:
contacts = [Contact.to_dict(contact) for contact in self.contacts]
json.dump(contacts, f, indent=4, default=str)
def add_peer(self, contact: Contact):
"""Adds new contact to base"""
self.contacts.append(contact)
def print_contacts(self):
"""Prints all contacts"""
if len(self.contacts) == 0:
print("No available contacts")
return
for i, contact in enumerate(self.contacts):
print(f"{i + 1}. {contact.username} "
f"({contact.host}:{contact.port})")
def get_contact_by_host(self, host: str) -> Contact | None:
"""Returns contact by host"""
for peer in self.contacts:
if peer.host == host:
return peer
return None
def get_contact_by_username(self, username: str) -> Contact | None:
"""Returns contact by username"""
for peer in self.contacts:
if peer.username == username:
return peer
return None
def update_peer(self, host: str, port: int, username: str):
"""If new host, adds it to base"""
hosts = [contact.host for contact in self.contacts]
if host not in hosts:
self.contacts.append(Contact(host, port, username))
def clear(self):
"""Clears all contacts"""
self.contacts = list[Contact]()
self.save_contacts()