mirror of
https://github.com/khoj-ai/khoj.git
synced 2025-02-17 08:04:21 +00:00
Resolve config_file to absolute right at start on parsing args in cli
- Assume path is absolute in yaml util module while saving, loading file - This follows same convention as jsonl. Which just operates on passed file path, assuming it is of appropriate form. Responsibility to put it in appropriate form is on the caller, for now
This commit is contained in:
parent
44fe70513a
commit
fad2f3a2e7
2 changed files with 9 additions and 6 deletions
|
@ -3,7 +3,7 @@ import argparse
|
|||
import pathlib
|
||||
|
||||
# Internal Packages
|
||||
from src.utils.helpers import resolve_absolute_path
|
||||
from src.utils.helpers import get_absolute_path, resolve_absolute_path
|
||||
from src.utils.yaml import parse_config_from_file
|
||||
|
||||
|
||||
|
@ -20,9 +20,12 @@ def cli(args=None):
|
|||
|
||||
args = parser.parse_args(args)
|
||||
|
||||
if not resolve_absolute_path(args.config_file).exists():
|
||||
# Normalize config_file path to absolute path
|
||||
args.config_file = resolve_absolute_path(args.config_file)
|
||||
|
||||
if not args.config_file.exists():
|
||||
args.config = None
|
||||
else:
|
||||
args.config = parse_config_from_file(args.config_file)
|
||||
|
||||
return args
|
||||
return args
|
||||
|
|
|
@ -5,7 +5,7 @@ from pathlib import Path
|
|||
import yaml
|
||||
|
||||
# Internal Packages
|
||||
from src.utils.helpers import get_absolute_path
|
||||
from src.utils.helpers import get_absolute_path, resolve_absolute_path
|
||||
from src.utils.rawconfig import FullConfig
|
||||
|
||||
# Do not emit tags when dumping to YAML
|
||||
|
@ -16,14 +16,14 @@ def save_config_to_file(yaml_config: dict, yaml_config_file: Path):
|
|||
# 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:
|
||||
with open(yaml_config_file, 'w', encoding='utf-8') as config_file:
|
||||
yaml.safe_dump(yaml_config, config_file, allow_unicode=True)
|
||||
|
||||
|
||||
def load_config_from_file(yaml_config_file: Path) -> dict:
|
||||
"Read config from YML file"
|
||||
config_from_file = None
|
||||
with open(get_absolute_path(yaml_config_file), 'r', encoding='utf-8') as config_file:
|
||||
with open(yaml_config_file, 'r', encoding='utf-8') as config_file:
|
||||
config_from_file = yaml.safe_load(config_file)
|
||||
return config_from_file
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue