diff --git a/EDMC.py b/EDMC.py index b8e95a55..58264fb0 100755 --- a/EDMC.py +++ b/EDMC.py @@ -132,6 +132,8 @@ def main() -> None: # noqa: C901, CCR001 action='store_true', ) + parser.add_argument('--config', help="Define a custom config to load EDMC from") + parser.add_argument('-a', metavar='FILE', help='write ship loadout to FILE in Companion API json format') parser.add_argument('-e', metavar='FILE', help='write ship loadout to FILE in E:D Shipyard plain text format') parser.add_argument('-l', metavar='FILE', help='write ship locations to FILE in CSV format') @@ -175,6 +177,9 @@ def main() -> None: # noqa: C901, CCR001 sys.exit(EXIT_ARGS) edmclogger.set_channels_loglevel(args.loglevel) + if args.config: + config.reload_from_path(args.config) + logger.debug(f'Startup v{appversion()} : Running on Python v{sys.version}') logger.debug(f'''Platform: {sys.platform} argv[0]: {sys.argv[0]} diff --git a/EDMarketConnector.py b/EDMarketConnector.py index aceba378..e7bdf490 100644 --- a/EDMarketConnector.py +++ b/EDMarketConnector.py @@ -102,6 +102,8 @@ if __name__ == '__main__': # noqa: C901 help="Start the application minimized", action="store_true" ) + + parser.add_argument('--config', help="Define a custom config to load EDMC from") ########################################################################### ########################################################################### @@ -204,6 +206,9 @@ if __name__ == '__main__': # noqa: C901 args: argparse.Namespace = parser.parse_args() + if args.config: + config.reload_from_path(args.config) + if args.capi_pretend_down: import config as conf_module logger.info('Pretending CAPI is down') diff --git a/config/__init__.py b/config/__init__.py index 77609a76..6fb3e03f 100644 --- a/config/__init__.py +++ b/config/__init__.py @@ -211,7 +211,6 @@ class Config: self.home_path = pathlib.Path.home() # Set Needed Platform Var for app_dir_path self.app_dir_path = app_path - self.toml_path: pathlib.Path = self.app_dir_path / "config.toml" self.generated: str | None = None self.source: str | None = None @@ -226,7 +225,7 @@ class Config: if plugdir_str is None or not pathlib.Path(plugdir_str).is_dir(): self.set("plugin_dir", str(self.default_plugin_dir_path)) plugdir_str = self.default_plugin_dir - self.plugin_dir_path = pathlib.Path(plugdir_str) + self.plugin_dir_path = pathlib.Path(self.get('plugin_dir')) self.plugin_dir_path.mkdir(exist_ok=True) # Call the rest of the platform var helpers @@ -434,6 +433,44 @@ class Config: """Return a string version of default_journal_dir.""" return str(self.default_journal_dir_path) + def reload_from_path(self, new_config_path: str | pathlib.Path) -> None: + """ + Replace the active config TOML file path and reload all settings. + + This allows the main application (after argparse) to override the + config file used without needing to recreate the Config instance. + """ + new_path = pathlib.Path(new_config_path) + + if not new_path.is_file(): + logger.error(f"Config file not found: {new_config_path}") + return # Use the default config. + + logger.info(f"Reloading config from alternate path: {new_path}") + + # Update path and reload + self.toml_path = new_path + self.generated = None + self.source = None + self.settings = {} + + # Load TOML content from the new file and setup system. + self._load() + self._init_platform() + + # Re-run plugin_dir logic + plugdir_str = self.get_str("plugin_dir") + if not plugdir_str: + plugdir_str = str(self.default_plugin_dir) + self.plugin_dir_path = self.default_plugin_dir_path + self.set("plugin_dir", plugdir_str) + elif not pathlib.Path(plugdir_str).is_dir(): + self.set("plugin_dir", str(self.default_plugin_dir_path)) + plugdir_str = self.default_plugin_dir + + self.plugin_dir_path = pathlib.Path(plugdir_str) + self.plugin_dir_path.mkdir(exist_ok=True) + @staticmethod def _suppress_call( func: Callable[..., _T], @@ -517,6 +554,7 @@ def get_config(*args, **kwargs) -> Config: try: config = WinConfigMinimal() except FileNotFoundError: + logger.error("No Config TOML found, no Windows Registry entry found.") return Config(app_path=app_dir_path) # Nothing to Convert if sys.platform == "linux": # pragma: sys-platform-linux @@ -524,9 +562,11 @@ def get_config(*args, **kwargs) -> Config: try: config = LinuxConfigMinimal() except (FileNotFoundError, TypeError): + logger.error("No Config TOML found, no Linux ini file found.") return Config(app_path=app_dir_path) # Nothing to Convert if config: + logger.info("Oldstyle config found. Converting...") config.write_to_toml(f"{app_dir_path}/config.toml") # type: ignore return Config(app_path=app_dir_path)