| 16 | |
| 17 | |
| 18 | class SingletonSettings: |
| 19 | _instance = None |
| 20 | |
| 21 | def __new__(cls, *args, **kwargs): |
| 22 | if not cls._instance: |
| 23 | cls._instance = super(SingletonSettings, cls).__new__(cls, *args, **kwargs) |
| 24 | return cls._instance |
| 25 | |
| 26 | def __init__(self): |
| 27 | """ |
| 28 | Initialize the SingletonSettings instance by loading settings from the specified configuration files. |
| 29 | Check if the 'settings' attribute is not already set, then construct the paths to the settings files based on the current directory. |
| 30 | Ensure that all settings files exist by checking their presence using the 'os.path.exists' function. |
| 31 | If any of the settings files are missing, raise a 'FileNotFoundError' with a message indicating the missing file. |
| 32 | Finally, initialize the 'settings' attribute with a Dynaconf instance using the specified settings files and configuration options. |
| 33 | |
| 34 | Parameters: |
| 35 | self: SingletonSettings |
| 36 | The SingletonSettings instance to be initialized. |
| 37 | |
| 38 | Raises: |
| 39 | FileNotFoundError: If any of the specified settings files are not found. |
| 40 | |
| 41 | Returns: |
| 42 | None |
| 43 | """ |
| 44 | if not hasattr(self, "settings"): |
| 45 | # Determine the base directory for bundled app or normal environment |
| 46 | base_dir = getattr(sys, "_MEIPASS", dirname(abspath(__file__))) |
| 47 | |
| 48 | settings_files = [join(base_dir, f) for f in SETTINGS_FILES] |
| 49 | |
| 50 | # Ensure all settings files exist |
| 51 | for file_path in settings_files: |
| 52 | if not exists(file_path): |
| 53 | raise FileNotFoundError(f"Settings file not found: {file_path}") |
| 54 | |
| 55 | self.settings = Dynaconf( |
| 56 | envvar_prefix=False, merge_enabled=True, settings_files=settings_files |
| 57 | ) |
| 58 | |
| 59 | |
| 60 | def get_settings() -> Dynaconf: |