|
| 1 | +Module ctfsolver.config.global_config |
| 2 | +===================================== |
| 3 | +Global configuration management for the CTFSolverScript package. |
| 4 | + |
| 5 | +This module provides the `GlobalConfig` class, which handles the creation, initialization, |
| 6 | +and access of a global configuration file stored in the user's home directory. The configuration |
| 7 | +file is used to store persistent settings required by the CTFSolverScript inline tool. |
| 8 | + |
| 9 | +Classes: |
| 10 | + GlobalConfig: Manages the global configuration file, including creation, initialization, |
| 11 | + reading, and attribute/dictionary-style access to configuration values. |
| 12 | + |
| 13 | +Attributes: |
| 14 | + CONFIG (GlobalConfig): Singleton instance of the GlobalConfig class for global access. |
| 15 | + |
| 16 | +Example: |
| 17 | + >>> from ctfsolver.config.global_config import CONFIG |
| 18 | + >>> from ctfsolver.config import CONFIG |
| 19 | + >>> CONFIG.initializing() # Initializes the global configuration |
| 20 | + |
| 21 | +Typical usage involves initializing the configuration (creating the file and writing initial |
| 22 | +content if necessary) and accessing configuration values via attribute or dictionary-style access. |
| 23 | + |
| 24 | +Raises: |
| 25 | + AttributeError: If an attribute is accessed that does not exist in the configuration. |
| 26 | + KeyError: If a key is accessed that does not exist in the configuration. |
| 27 | + |
| 28 | +Classes |
| 29 | +------- |
| 30 | + |
| 31 | +`GlobalConfig(*args, **kwargs)` |
| 32 | +: Initializes the global configuration for the CTF solver application. |
| 33 | + This constructor sets the path to the global configuration file and loads its content. |
| 34 | + |
| 35 | + Attributes: |
| 36 | + global_config_file_path (Path): Path to the global configuration JSON file. |
| 37 | + |
| 38 | + Raises: |
| 39 | + Any exceptions raised by `get_content()` will propagate. |
| 40 | + |
| 41 | + ### Methods |
| 42 | + |
| 43 | + `creating(self)` |
| 44 | + : Creates a global configuration file in the user's home directory. |
| 45 | + |
| 46 | + This method ensures that the required directories and configuration file exist, |
| 47 | + creating them if necessary. It is typically called during the initial run of the |
| 48 | + inline tool or when global configuration setup is required. |
| 49 | + |
| 50 | + Args: |
| 51 | + None |
| 52 | + |
| 53 | + Returns: |
| 54 | + None |
| 55 | + |
| 56 | + Raises: |
| 57 | + OSError: If the directory or file cannot be created due to permission issues. |
| 58 | + |
| 59 | + `get_content(self)` |
| 60 | + : Get the content of the global configuration file. |
| 61 | + This method reads the global configuration file and returns its content |
| 62 | + as a dictionary. |
| 63 | + If the file does not exist or is empty, it returns an empty dictionary. |
| 64 | + It is intended to be used to retrieve the current global configuration |
| 65 | + settings for use in the inline tool or other parts of the application. |
| 66 | + |
| 67 | + |
| 68 | + Returns: |
| 69 | + dict: The content of the global configuration file as a dictionary. |
| 70 | + |
| 71 | + `initial_content(self)` |
| 72 | + : Sets the initial content of the global configuration file. |
| 73 | + |
| 74 | + This method loads a configuration template from 'config_template.json' and writes it to the global |
| 75 | + configuration file if the file is empty or does not exist. If the template file is missing, a default |
| 76 | + initial content is used instead. |
| 77 | + |
| 78 | + Args: |
| 79 | + None |
| 80 | + |
| 81 | + Returns: |
| 82 | + None |
| 83 | + |
| 84 | + Raises: |
| 85 | + FileNotFoundError: If the template file or global configuration file path does not exist. |
| 86 | + json.JSONDecodeError: If the template file contains invalid JSON. |
| 87 | + |
| 88 | + Side Effects: |
| 89 | + Writes initial configuration content to the global configuration file if it is empty. |
| 90 | + Prints status messages to the console. |
| 91 | + |
| 92 | + `initializing(self)` |
| 93 | + : Initialize global configuration settings. |
| 94 | + This method can be used to set up any necessary global configurations |
| 95 | + required by the inline tool. |
0 commit comments