-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
71 lines (56 loc) · 2.01 KB
/
utils.py
File metadata and controls
71 lines (56 loc) · 2.01 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
#!/usr/bin/env python3
""" utilities """
from typing import Tuple, Union
from logger.logger import logger
addr_type = Tuple[str, int]
def parse_config_file(file_path: str) -> dict[str, Union[str, int]] | None:
"""
Parses a configuration file and returns the configurations
as a dictionary. The configuration file should have key-value pairs in
the format "KEY=VALUE". Keys "PORT" and "PAYLOAD_SIZE" will have their
values converted to integers.
Args:
file_path (str): The path to the configuration file.
Returns:
dict | None: A dictionary containing the configurations if the file
is successfully parsed,
None if the file is not found or an error occurs during
parsing.
Raises:
FileNotFoundError: If the configuration file is not found.
Exception: If an error occurs during parsing.
Example:
Given a configuration file 'config.txt' with the following
content:
HOST=127.0.0.1
PORT=8080
PAYLOAD_SIZE=1024
DEBUG=True
The function will return:
{
'HOST': '127.0.0.1',
'PORT': 8080,
'PAYLOAD_SIZE': 1024,
'DEBUG': 'True'
}
"""
configurations: dict = {}
try:
with open(file_path, "r") as file:
for line in file:
line = line.strip()
key: str
val: str | int
key, val = line.split("=", 1)
if key in ["PORT", "PAYLOAD_SIZE"]:
val = int(val)
configurations[key] = val
return configurations
except FileNotFoundError:
logger.debug(f"Error: Configuration file '{file_path}' not found.")
return None
except Exception as e:
logger.debug(f"Error parsing the configuration file: {e}")
return None
return None
server_configurations = parse_config_file("./config/config.txt")