From 44fe70513af826e9c5ed45e237bb919e39c3aed0 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Fri, 12 Aug 2022 01:17:34 +0300 Subject: [PATCH] Handle situation where default config directory or file does not exist - Include khoj_sample.yml in pip package to load default config from - Create khoj config directory if it doesn't exist - Load config from khoj_sample.yml if khoj.yml config doesn't exist --- MANIFEST.in | 1 + src/interface/desktop/configure_screen.py | 7 ++++--- src/utils/yaml.py | 3 +++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/MANIFEST.in b/MANIFEST.in index 595ab4e1..30a81879 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -1,4 +1,5 @@ include Readme.md +include config/khoj_sample.yml graft src/interface/* prune src/interface/web/images* prune docs* diff --git a/src/interface/desktop/configure_screen.py b/src/interface/desktop/configure_screen.py index 86585391..a6cc84f2 100644 --- a/src/interface/desktop/configure_screen.py +++ b/src/interface/desktop/configure_screen.py @@ -11,7 +11,7 @@ from src.interface.desktop.file_browser import FileBrowser from src.utils import constants, state, yaml as yaml_utils from src.utils.cli import cli from src.utils.config import SearchType, ProcessorType -from src.utils.helpers import merge_dicts +from src.utils.helpers import merge_dicts, resolve_absolute_path class ConfigureScreen(QtWidgets.QDialog): @@ -27,8 +27,9 @@ class ConfigureScreen(QtWidgets.QDialog): self.config_file = config_file # Load config from existing config, if exists, else load from default config - self.current_config = yaml_utils.load_config_from_file(self.config_file) - if self.current_config is None: + if resolve_absolute_path(self.config_file).exists(): + self.current_config = yaml_utils.load_config_from_file(self.config_file) + else: self.current_config = yaml_utils.load_config_from_file(constants.app_root_directory / 'config/khoj_sample.yml') self.new_config = self.current_config diff --git a/src/utils/yaml.py b/src/utils/yaml.py index 588acbda..e313c1f1 100644 --- a/src/utils/yaml.py +++ b/src/utils/yaml.py @@ -13,6 +13,9 @@ yaml.emitter.Emitter.process_tag = lambda self, *args, **kwargs: None def save_config_to_file(yaml_config: dict, yaml_config_file: Path): "Write config to YML file" + # Create output directory, if it doesn't exist + yaml_config_file.parent.mkdir(parents=True, exist_ok=True) + with open(get_absolute_path(yaml_config_file), 'w', encoding='utf-8') as config_file: yaml.safe_dump(yaml_config, config_file, allow_unicode=True)