Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Fix create config CLI command #287

Merged
merged 7 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/canopy/utils/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
import os
import yaml
from typing import Dict, Any


def load_config_template(name: str) -> Dict[str, Any]:
if not name.endswith(".yaml"):
name += ".yaml"

config_template_path = os.path.join(os.path.dirname(os.path.dirname(__file__)),
"config_templates", name)
with open(config_template_path, "r") as f:
return yaml.safe_load(f)
acatav marked this conversation as resolved.
Show resolved Hide resolved


class ConfigurableMixin:
_DEFAULT_COMPONENTS: Dict[str, type] = {}

Expand Down
42 changes: 16 additions & 26 deletions src/canopy_cli/cli.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
import shutil
from typing import Dict, Any, Optional, List, Iterable

import click
import importlib.resources as pkg_resources
from prompt_toolkit import prompt
import time

Expand Down Expand Up @@ -211,38 +211,28 @@ def health(url):
return


@cli.command(help="Writes a config template YAML file to the specified path.")
@click.argument("path", type=click.Path(), required=True)
@click.option("--template", default="default", help="The name of the template to use.")
def create_config(path, template):
@cli.command(help="Writes the config templates to a directory.")
@click.argument("out_path", type=click.Path(), required=True)
def create_config(out_path):

if not template.endswith('.yaml'):
template += '.yaml'
if os.path.isfile(out_path):
raise CLIError(f"Path expected to be a directory,"
f"but found a file at {out_path}")

if os.path.exists(path):
click.confirm(click.style(f"File {path} already exists. Overwrite?", fg="red"),
if os.path.exists(out_path) and os.path.isdir(out_path) and os.listdir(out_path):
click.confirm(click.style(f"Path {out_path} is not empty. Overwrite?",
fg="red"),
abort=True)

project_root = os.path.dirname(os.path.dirname(__file__))
config_templates_path = os.path.join(project_root, "canopy", "config_templates")

try:
with pkg_resources.open_text('canopy.config', template) as f:
config = yaml.safe_load(f)
except FileNotFoundError:
files = pkg_resources.files('canopy.config')
template_names = [f.name for f in files.iterdir()]
click.echo(f"Template '{template}' not found."
f"Available templates: {template_names}",
err=True)
return
shutil.copytree(config_templates_path, out_path, dirs_exist_ok=True)
except Exception as e:
click.echo(f"Failed to load template '{template}'. Reason: {e}"
f"Make sure you pip installed `canopy-sdk`.",
err=True)
return

with open(path, 'w') as dst_file:
yaml.dump(config, dst_file)
raise CLIError(f"Failed to write config template to {out_path}. Reason:\n{e}")

click.echo(f"Config template '{template}' written to {path}")
click.echo(click.style(f"Config templates written to {out_path}", fg="green"))


@cli.command(
Expand Down
7 changes: 2 additions & 5 deletions tests/system/utils/test_config.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import os

import pytest
import yaml

from canopy.chat_engine import ChatEngine
from canopy.context_engine import ContextEngine
from canopy.knowledge_base import KnowledgeBase

DEFAULT_CONFIG_PATH = 'src/canopy/config/default.yaml'
from canopy.utils.config import load_config_template


@pytest.fixture(scope='module')
Expand All @@ -24,8 +22,7 @@ def temp_index_name():


def test_default_config_matches_code_defaults(temp_index_name):
with open(DEFAULT_CONFIG_PATH) as f:
default_config = yaml.safe_load(f)
default_config = load_config_template("default.yaml")
chat_engine_config = default_config['chat_engine']

loaded_chat_engine = ChatEngine.from_config(chat_engine_config)
Expand Down
Loading