|
| 1 | +import json |
| 2 | +import os |
| 3 | + |
| 4 | +class Config: |
| 5 | + """ |
| 6 | + Class for managing configuration settings for the satellite network. |
| 7 | +
|
| 8 | + Attributes: |
| 9 | + config_file (str): Path to the configuration file. |
| 10 | + settings (dict): Dictionary to store configuration settings. |
| 11 | + """ |
| 12 | + |
| 13 | + def __init__(self, config_file='config.json'): |
| 14 | + """ |
| 15 | + Initializes a Config instance. |
| 16 | +
|
| 17 | + Args: |
| 18 | + config_file (str): Path to the configuration file. |
| 19 | + """ |
| 20 | + self.config_file = config_file |
| 21 | + self.settings = {} |
| 22 | + self.load_config() |
| 23 | + |
| 24 | + def load_config(self): |
| 25 | + """Loads configuration settings from a JSON file.""" |
| 26 | + if os.path.exists(self.config_file): |
| 27 | + with open(self.config_file, 'r') as file: |
| 28 | + self.settings = json.load(file) |
| 29 | + print(f"Configuration loaded: {self.settings}") |
| 30 | + else: |
| 31 | + print(f"Configuration file {self.config_file} not found. Using default settings.") |
| 32 | + self.set_default_config() |
| 33 | + |
| 34 | + def set_default_config(self): |
| 35 | + """Sets default configuration settings.""" |
| 36 | + self.settings = { |
| 37 | + 'node_capacity': 100, # Maximum number of nodes |
| 38 | + 'communication_protocol': 'quantum', # Default communication protocol |
| 39 | + 'max_data_size': 1024, # Maximum data size in bytes |
| 40 | + 'quantum_key_length': 128, # Length of the quantum key |
| 41 | + 'location': 'geostationary', # Default node location |
| 42 | + 'logging_level': 'INFO' # Default logging level |
| 43 | + } |
| 44 | + self.save_config() |
| 45 | + |
| 46 | + def save_config(self): |
| 47 | + """Saves the current configuration settings to a JSON file.""" |
| 48 | + with open(self.config_file, 'w') as file: |
| 49 | + json.dump(self.settings, file, indent=4) |
| 50 | + print(f"Configuration saved: {self.settings}") |
| 51 | + |
| 52 | + def get_setting(self, key): |
| 53 | + """ |
| 54 | + Retrieves a configuration setting. |
| 55 | +
|
| 56 | + Args: |
| 57 | + key (str): The key of the setting to retrieve. |
| 58 | +
|
| 59 | + Returns: |
| 60 | + The value of the setting, or None if the key does not exist. |
| 61 | + """ |
| 62 | + return self.settings.get(key, None) |
| 63 | + |
| 64 | + def set_setting(self, key, value): |
| 65 | + """ |
| 66 | + Sets a configuration setting. |
| 67 | +
|
| 68 | + Args: |
| 69 | + key (str): The key of the setting to set. |
| 70 | + value: The value to set for the setting. |
| 71 | + """ |
| 72 | + self.settings[key] = value |
| 73 | + self.save_config() |
| 74 | + |
| 75 | +# Example usage |
| 76 | +if __name__ == "__main__": |
| 77 | + config = Config() |
| 78 | + print("Node Capacity:", config.get_setting('node_capacity')) |
| 79 | + config.set_setting('node_capacity', 150) |
| 80 | + print("Updated Node Capacity:", config.get_setting('node_capacity')) |
0 commit comments