1 |
474551ae
|
silhavyj
|
from sys import exit
|
2 |
bba28c53
|
silhavyj
|
from configparser import RawConfigParser
|
3 |
|
|
|
4 |
|
|
|
5 |
474551ae
|
silhavyj
|
class Config:
|
6 |
6b5427e6
|
silhavyj
|
"""This class holds the configuration values of the application.
|
7 |
|
|
|
8 |
|
|
It reads the configuration file passed in through the constructor
|
9 |
|
|
and stores the values into class variables.
|
10 |
|
|
"""
|
11 |
474551ae
|
silhavyj
|
|
12 |
|
|
def __init__(self, filepath):
|
13 |
6b5427e6
|
silhavyj
|
"""Constructor of the class.
|
14 |
|
|
|
15 |
|
|
It instantiates the class, reads the configuration file,
|
16 |
|
|
and parses all sections defined in it.
|
17 |
|
|
|
18 |
|
|
:param filepath: path to the configuration file
|
19 |
|
|
"""
|
20 |
|
|
# Create a new ConfigParser
|
21 |
474551ae
|
silhavyj
|
self.config = RawConfigParser()
|
22 |
6b5427e6
|
silhavyj
|
|
23 |
|
|
# Try to parse the configuration file. If it fails,
|
24 |
|
|
# terminate the application.
|
25 |
474551ae
|
silhavyj
|
if not self.config.read(filepath):
|
26 |
|
|
print(f"Failed to parse the config file {filepath}. Make sure you entered a valid path.")
|
27 |
|
|
exit(1)
|
28 |
|
|
|
29 |
6b5427e6
|
silhavyj
|
# Parse the 'usb detector' section.
|
30 |
474551ae
|
silhavyj
|
self._parse_usb_detector_section()
|
31 |
6b5427e6
|
silhavyj
|
|
32 |
|
|
# Parse the 'server' section (API).
|
33 |
474551ae
|
silhavyj
|
self._parse_server_section()
|
34 |
6b5427e6
|
silhavyj
|
|
35 |
|
|
# Parse the 'logger' section.
|
36 |
474551ae
|
silhavyj
|
self._parse_logger_section()
|
37 |
6b5427e6
|
silhavyj
|
|
38 |
|
|
# Parse the 'cache' section.
|
39 |
474551ae
|
silhavyj
|
self._parse_cache_section()
|
40 |
|
|
|
41 |
|
|
def _parse_usb_detector_section(self):
|
42 |
6b5427e6
|
silhavyj
|
"""Parse the 'usb detector' section of the configuration file.
|
43 |
|
|
"""
|
44 |
474551ae
|
silhavyj
|
section_name = "usb_detector"
|
45 |
|
|
self.scan_period_seconds = float(self.config[section_name]["scan_period_seconds"])
|
46 |
|
|
self.connected_devices_filename = self.config[section_name]["connected_devices_filename"]
|
47 |
|
|
|
48 |
|
|
def _parse_server_section(self):
|
49 |
6b5427e6
|
silhavyj
|
"""Parse the 'server' section of the configuration file.
|
50 |
|
|
"""
|
51 |
474551ae
|
silhavyj
|
section_name = "server"
|
52 |
|
|
self.server_url = self.config[section_name]["url"]
|
53 |
|
|
self.server_port = self.config[section_name]["port"]
|
54 |
|
|
self.server_endpoint = self.config[section_name]["end_point"]
|
55 |
|
|
|
56 |
|
|
def _parse_logger_section(self):
|
57 |
6b5427e6
|
silhavyj
|
"""Parse the 'logger' section of the configuration file.
|
58 |
|
|
"""
|
59 |
474551ae
|
silhavyj
|
section_name = "logger"
|
60 |
|
|
self.logger_config_file = self.config[section_name]["config_file"]
|
61 |
|
|
|
62 |
|
|
def _parse_cache_section(self):
|
63 |
6b5427e6
|
silhavyj
|
"""Parse the 'cache' section of the configuration file.
|
64 |
|
|
"""
|
65 |
474551ae
|
silhavyj
|
section_name = "cache"
|
66 |
|
|
self.cache_dir = self.config[section_name]["directory"]
|
67 |
|
|
self.cache_max_entries = int(self.config[section_name]["max_entries"])
|
68 |
|
|
self.cache_max_retries = int(self.config[section_name]["max_retries"])
|
69 |
|
|
self.cache_retry_period_seconds = float(self.config[section_name]["retry_period_seconds"])
|