forked from sarvesh68/DistributedChatSystem
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathidentity.py
More file actions
28 lines (20 loc) · 683 Bytes
/
identity.py
File metadata and controls
28 lines (20 loc) · 683 Bytes
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
"""Simple peer identity management.
Generates or loads a persistent UUID for a peer from a local file.
"""
import uuid
from pathlib import Path
PEER_ID_FILE = "peer_id.txt"
def load_or_generate_peer_id(PEER_ID_FILE=PEER_ID_FILE):
"""Load a peer UUID from disk or generate a new one.
Args:
PEER_ID_FILE (str): Path to the file storing the peer UUID.
Returns:
str: The peer UUID, either loaded from disk or newly generated.
"""
path = Path(PEER_ID_FILE)
if path.exists():
return path.read_text().strip()
else:
new_id = str(uuid.uuid4())
path.write_text(new_id)
return new_id