Project

General

Profile

Files » config_reader.py

a program to read the config file - Pratama Kwee BRANDON, 11/12/2025 08:22 PM

 
1
import configparser
2

    
3
class ConfigReader:
4
    """
5
    @brief Configuration file reader class
6
    
7
    @details This class provides methods to read, parse and retrieve 
8
    configuration parameters from INI-style configuration files.
9
    It uses Python's configparser module internally.
10
    """
11
    def __init__(self, config_path):
12
        """
13
      @brief Constructor for ConfigReader class
14
      
15
      @param config_path Path to the configuration file
16
      @exception FileNotFoundError If configuration file is not found
17
      """
18
        self.config_path = config_path
19
        self.config = configparser.ConfigParser()
20
        self.config.read(self.config_path)
21

    
22
    def get_value(self, section, key, fallback=None):
23
        """
24
        @brief Get value from configuration file
25
        
26
        @param section Configuration section name
27
        @param key Configuration key name
28
        @param fallback Default value if key not found
29
        @return Configuration value as string or fallback value
30
        """
31
        try:
32
            return self.config.get(section, key)
33
        except (configparser.NoSectionError, configparser.NoOptionError):
34
            return fallback
35

    
36
    def get_linear_parameter(self):
37
        """
38
        @brief Get linear parameter from configuration
39
        
40
        @return Linear parameter as float value
41
        """
42
        return float(self.get_value('Parameters', 'linear_parameter'))
43
    
44
    def get_image_path(self):
45
        """
46
        @brief Get image path from configuration
47
        
48
        @return Image file path as string
49
        """
50
        return self.get_value('Parameters', 'image_path')
51

    
52
    def get_video_path(self):
53
        """
54
       @brief Get video path from configuration
55
       
56
       @return Video file path as string
57
       """
58
        return self.get_value('Parameters', 'video_path')
59
    
60
    def get_overlap(self):
61
        """
62
        @brief Get overlap parameter from configuration
63
        
64
        @return Overlap value as string
65
        """
66
        return self.get_value('Parameters', 'overlap')
67
    
68
    def get_blend_mode(self):
69
        """
70
       @brief Get blend mode from configuration
71
       
72
       @return Blend mode as string
73
       """
74
        return self.get_value('Parameters', 'blend_mode')
75

    
76
    def save_config(self):
77
        """
78
        @brief Save current configuration to file
79
        
80
        @details Writes the current configuration state back to the file.
81
        This can be used to persist changes made to configuration values.
82
        """
83
        with open(self.config_path, 'w') as configfile:
84
            self.config.write(configfile)
(14-14/17)