Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split get_skill_config into more specific functions. #53

Merged
merged 3 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 28 additions & 16 deletions webex_skills/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,37 @@

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:
typer.secho(f'No configured remote with the name {name} found', color=typer.colors.RED, err=True)
def get_remotes():
"""Returns configuration for all named skills"""
if not CONFIG_FILE.exists():
return {}
config = load_config()
return config.get('remotes', {})


def load_config():
"""Attempts to load the configuration file"""
try:
return json.loads(CONFIG_FILE.read_text(encoding='utf-8')) or {}
except json.JSONDecodeError:
typer.secho('Invalid JSON in configuration file', color=typer.colors.RED, err=True)
raise typer.Exit(1)

return remote_config

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 config['app_dir']
22 changes: 5 additions & 17 deletions webex_skills/cli/nlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,30 @@

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')


# 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()
Expand Down
17 changes: 8 additions & 9 deletions webex_skills/cli/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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?'
):
Expand Down Expand Up @@ -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",
Expand All @@ -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
4 changes: 2 additions & 2 deletions webex_skills/cli/remote.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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)
Expand Down