Projekt

Obecné

Profil

Stáhnout (2.5 KB) Statistiky
| Větev: | Tag: | Revize:
1
from sys import exit
2
from configparser import RawConfigParser
3

    
4

    
5
class Config:
6
    """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

    
12
    def __init__(self, filepath):
13
        """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
        self.config = RawConfigParser()
22

    
23
        # Try to parse the configuration file. If it fails,
24
        # terminate the application.
25
        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
        # Parse the 'usb detector' section.
30
        self._parse_usb_detector_section()
31

    
32
        # Parse the 'server' section (API).
33
        self._parse_server_section()
34

    
35
        # Parse the 'logger' section.
36
        self._parse_logger_section()
37

    
38
        # Parse the 'cache' section.
39
        self._parse_cache_section()
40

    
41
    def _parse_usb_detector_section(self):
42
        """Parse the 'usb detector' section of the configuration file.
43
        """
44
        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
        """Parse the 'server' section of the configuration file.
50
        """
51
        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
        """Parse the 'logger' section of the configuration file.
58
        """
59
        section_name = "logger"
60
        self.logger_config_file = self.config[section_name]["config_file"]
61

    
62
    def _parse_cache_section(self):
63
        """Parse the 'cache' section of the configuration file.
64
        """
65
        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"])
(1-1/2)