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

Commit ae69371

Browse files
authored
Fix create config CLI command (#287)
* use simple file load for template CLI command and add a load config template utility * lint * Create a Directory class to hold the lib paths * test fix * fix config test * dummy change to toml
1 parent 1a06f4f commit ae69371

File tree

8 files changed

+32
-33
lines changed

8 files changed

+32
-33
lines changed

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ grpc = ["pinecone-client"]
4747

4848

4949
[tool.poetry.group.dev.dependencies]
50-
jupyter = "^1.0.0"
5150
pytest = "^7.3.2"
51+
jupyter = "^1.0.0"
5252
mypy = "^1.4.1"
5353
flake8 = "^6.1.0"
5454
pytest-html = "^4.1.0"
@@ -104,4 +104,4 @@ skip-checking-raises = true
104104
canopy = "canopy_cli.cli:cli"
105105

106106
[tool.pytest.ini_options]
107-
log_cli = true
107+
log_cli = true
File renamed without changes.

src/canopy/utils/directory.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from pathlib import Path
2+
3+
4+
class Directory:
5+
"""Stores the directory paths for Canopy library"""
6+
7+
ROOT = Path(__file__).parent.parent
8+
CONFIG_TEMPLATES = ROOT.joinpath("config_templates")

src/canopy_cli/cli.py

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import os
2+
import shutil
23
from typing import Dict, Any, Optional, List, Iterable
34

45
import click
5-
import importlib.resources as pkg_resources
66
from prompt_toolkit import prompt
77
import time
8-
8+
from pathlib import Path
99
import requests
1010
import yaml
1111
from dotenv import load_dotenv
@@ -22,6 +22,7 @@
2222
from canopy.chat_engine import ChatEngine
2323
from canopy.models.data_models import Document, UserMessage
2424
from canopy.tokenizer import Tokenizer
25+
from canopy.utils.directory import Directory
2526
from canopy_cli.data_loader import (
2627
load_from_path,
2728
IDsNotUniqueError,
@@ -211,38 +212,27 @@ def health(url):
211212
return
212213

213214

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

219-
if not template.endswith('.yaml'):
220-
template += '.yaml'
221+
if out_path.is_file():
222+
raise CLIError(f"Path expected to be a directory,"
223+
f"but found a file at {out_path}")
221224

222-
if os.path.exists(path):
223-
click.confirm(click.style(f"File {path} already exists. Overwrite?", fg="red"),
225+
if out_path.exists() and any(out_path.iterdir()):
226+
click.confirm(click.style(f"Path {out_path} is not empty. Overwrite?",
227+
fg="red"),
224228
abort=True)
225229

226230
try:
227-
with pkg_resources.open_text('canopy.config', template) as f:
228-
config = yaml.safe_load(f)
229-
except FileNotFoundError:
230-
files = pkg_resources.files('canopy.config')
231-
template_names = [f.name for f in files.iterdir()]
232-
click.echo(f"Template '{template}' not found."
233-
f"Available templates: {template_names}",
234-
err=True)
235-
return
231+
shutil.copytree(Directory.CONFIG_TEMPLATES, out_path, dirs_exist_ok=True)
236232
except Exception as e:
237-
click.echo(f"Failed to load template '{template}'. Reason: {e}"
238-
f"Make sure you pip installed `canopy-sdk`.",
239-
err=True)
240-
return
241-
242-
with open(path, 'w') as dst_file:
243-
yaml.dump(config, dst_file)
233+
raise CLIError(f"Failed to write config template to {out_path}. Reason:\n{e}")
244234

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

247237

248238
@cli.command(

tests/system/utils/test_config.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
from canopy.chat_engine import ChatEngine
77
from canopy.context_engine import ContextEngine
88
from canopy.knowledge_base import KnowledgeBase
9-
10-
DEFAULT_CONFIG_PATH = 'src/canopy/config/default.yaml'
9+
from canopy.utils.directory import Directory
1110

1211

1312
@pytest.fixture(scope='module')
@@ -24,8 +23,10 @@ def temp_index_name():
2423

2524

2625
def test_default_config_matches_code_defaults(temp_index_name):
27-
with open(DEFAULT_CONFIG_PATH) as f:
28-
default_config = yaml.safe_load(f)
26+
27+
with open(Directory.CONFIG_TEMPLATES.joinpath("default.yaml")) as file:
28+
default_config = yaml.safe_load(file)
29+
2930
chat_engine_config = default_config['chat_engine']
3031

3132
loaded_chat_engine = ChatEngine.from_config(chat_engine_config)

0 commit comments

Comments
 (0)