diff --git a/tests/conftest.py b/tests/conftest.py index d4031cc..ebc4230 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,6 @@ import re import pytest - from webex_skills.dialogue.rules import SimpleDialogueStateRule from webex_skills.models.mindmeld import DialogueState diff --git a/tests/test_crypto.py b/tests/test_crypto.py index 5fe7502..2f83a9c 100644 --- a/tests/test_crypto.py +++ b/tests/test_crypto.py @@ -2,7 +2,6 @@ import json import pytest - from webex_skills.crypto import sign_token, verify_signature diff --git a/tests/test_dialogue.py b/tests/test_dialogue.py index 9d554c2..f1e3436 100644 --- a/tests/test_dialogue.py +++ b/tests/test_dialogue.py @@ -1,5 +1,4 @@ import pytest - from webex_skills.dialogue.manager import SimpleDialogueManager pytestmark = pytest.mark.asyncio diff --git a/webex_skills/cli/config.py b/webex_skills/cli/config.py index 94e8531..069c684 100644 --- a/webex_skills/cli/config.py +++ b/webex_skills/cli/config.py @@ -3,25 +3,28 @@ import typer +CONFIG_DIR = Path(typer.get_app_dir('skills-cli', force_posix=True)) +CONFIG_FILE = CONFIG_DIR / 'config.json' -def get_skill_config(name=None): - app_dir = Path(typer.get_app_dir('skills-cli', force_posix=True)) - config_file = app_dir / 'config.json' - if not app_dir.exists(): - typer.echo('Creating default configuration') - app_dir.mkdir(parents=True) - config = {} - else: - config = json.loads(config_file.read_text(encoding='utf-8')) or {} +def get_skill_config(name: str): + """Return the configuration for a named skill""" + remotes = get_remotes() + return remotes.get(name) - remotes = config.get('remotes', {}) - if not name: - return remotes - remote_config = remotes.get(name) - if not remote_config: +def get_remotes(): + """Returns configuration for all named skills""" + if not CONFIG_FILE.exists(): + return {} + config = json.loads(CONFIG_FILE.read_text(encoding='utf-8')) + return config.get('remotes', {}) + + +def get_app_dir(name: str): + """Returns the app directory path for a named skill""" + config = get_skill_config(name) + if not config: typer.secho(f'No configured remote with the name {name} found', color=typer.colors.RED, err=True) raise typer.Exit(1) - - return remote_config + return config['app_dir'] diff --git a/webex_skills/cli/nlp.py b/webex_skills/cli/nlp.py index 3931e47..9aae038 100644 --- a/webex_skills/cli/nlp.py +++ b/webex_skills/cli/nlp.py @@ -3,7 +3,7 @@ import typer -from .config import get_skill_config +from .config import get_app_dir from .helpers import create_nlp app = typer.Typer(help='Commands for working with NLP models') @@ -11,34 +11,22 @@ # take name to find app path, otherwise default to cwd @app.command() -def build( - name: Optional[str] = typer.Argument( - None, - help="The name of the skill to build.", - ), -): +def build(name: Optional[str] = typer.Argument(None, help="The name of the skill to build.")): """Build nlp models associated with this skill""" app_dir = '.' if name: - config = get_skill_config(name) - app_dir = config['app_dir'] + app_dir = get_app_dir(name) nlp = create_nlp(app_dir) nlp.build() @app.command() -def process( - name: Optional[str] = typer.Argument( - None, - help="The name of the skill to send the query to.", - ), -): +def process(name: Optional[str] = typer.Argument(None, help="The name of the skill to send the query to.")): """Run a query through NLP processing""" app_dir = '.' if name: - config = get_skill_config(name) - app_dir = config['app_dir'] + app_dir = get_app_dir(name) nlp = create_nlp(app_dir) nlp.load() diff --git a/webex_skills/cli/project.py b/webex_skills/cli/project.py index fec7c7c..26ff15a 100644 --- a/webex_skills/cli/project.py +++ b/webex_skills/cli/project.py @@ -7,7 +7,7 @@ import typer from ..crypto import generate_keys -from .config import get_skill_config +from .config import CONFIG_DIR, CONFIG_FILE, get_remotes, get_skill_config from .helpers import create_nlp app = typer.Typer(name='project') @@ -25,18 +25,20 @@ def init( resolve_path=True, ), secret: Optional[str] = typer.Option( - None, help="A secret for encryption. If not provided, one will be" " generated automatically." + None, help="A secret for encryption. If not provided, one will be generated automatically." ), mindmeld: Optional[bool] = typer.Option( False, - help="If flag set, a MindMeld app will be created, otherwise " "it defaults to a simple app", + help="If flag set, a MindMeld app will be created, otherwise it defaults to a simple app", is_flag=True, ), ): """Create a new skill project from a template""" + if not CONFIG_DIR.exists(): + CONFIG_DIR.mkdir(parents=True) # Check for an existing skill name and provide an option to overwrite - if get_skill_config(skill_name): + if CONFIG_FILE.exists() and get_skill_config(skill_name): if not typer.confirm( f'A skill named {skill_name} already exists in your configuration. Would you like to overwrite it?' ): @@ -134,7 +136,7 @@ def _create_project( env_file_path = output_dir / '.env' env_file_path.write_text(env_content) - remotes = get_skill_config() + remotes = get_remotes() remotes[skill_name] = { 'name': skill_name, 'url': "http://localhost:8080/parse", @@ -145,8 +147,5 @@ def _create_project( 'app_dir': str(app_dir), } - config_dir = Path(typer.get_app_dir('skills-cli', force_posix=True)) - config_file = config_dir / 'config.json' - - config_file.write_text(json.dumps({'remotes': remotes}, indent=2), encoding='utf-8') + CONFIG_FILE.write_text(json.dumps({'remotes': remotes}, indent=2), encoding='utf-8') return app_dir diff --git a/webex_skills/cli/remote.py b/webex_skills/cli/remote.py index a4028b1..0364ca1 100644 --- a/webex_skills/cli/remote.py +++ b/webex_skills/cli/remote.py @@ -5,7 +5,7 @@ import typer -from .config import get_skill_config +from .config import get_remotes remote = typer.Typer(name='remote', help='Commands for interacting with running skills') @@ -81,7 +81,7 @@ def ls( name: Optional[str] = typer.Option(None, help="The name of a particular skill to display.") ): # pylint:disable=invalid-name """List configured remote skills""" - remotes = get_skill_config() + remotes = get_remotes() if not remotes: typer.secho('No configured remotes found', color=typer.colors.RED, err=True) raise typer.Exit(1)