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

Issue251 cli init command #306

Merged
merged 10 commits into from
Aug 23, 2019
11 changes: 11 additions & 0 deletions annif/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ def run_show_project(project_id):
click.echo(template.format('Access:', proj.access.name))


@cli.command('clear')
@click.argument('project_id')
@common_options
def run_clear_project(project_id):
"""
Initialize the project to its original, untrained state.
"""
proj = get_project(project_id)
proj.remove_model_data()


@cli.command('loadvoc')
@click.argument('project_id')
@click.argument('subjectfile', type=click.Path(dir_okay=False))
Expand Down
12 changes: 12 additions & 0 deletions annif/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import joblib
from sklearn.feature_extraction.text import TfidfVectorizer
from flask import current_app
from shutil import rmtree
import annif
import annif.analyzer
import annif.corpus
Expand Down Expand Up @@ -217,6 +218,17 @@ def dump(self):
'backend': {'backend_id': self.config['backend']}
}

def remove_model_data(self):
"""remove the data of this project"""
datadir_path = self._datadir_path
if os.path.isdir(datadir_path):
rmtree(datadir_path)
logger.info('Removed model data for project {}.'
.format(self.project_id))
else:
logger.warning('No model data to remove for project {}.'
.format(self.project_id))


def _create_projects(projects_file, datadir, init_projects):
if not os.path.exists(projects_file):
Expand Down
23 changes: 23 additions & 0 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ def test_show_project_nonexistent():
assert failed_result.exception


def test_clear_project(testdatadir):
dirpath = os.path.join(str(testdatadir), 'projects', 'dummy-fi')
fpath = os.path.join(str(dirpath), 'test_clear_project_datafile')
os.makedirs(dirpath)
open(fpath, 'a').close()

assert runner.invoke(
annif.cli.cli,
['clear', 'dummy-fi']).exit_code == 0
assert not os.path.isdir(dirpath)


def test_clear_project_nonexistent_data(testdatadir, caplog):
logger = annif.logger
logger.propagate = True
runner.invoke(
osma marked this conversation as resolved.
Show resolved Hide resolved
annif.cli.cli,
['clear', 'dummy-fi']).exit_code != 0
assert len(caplog.records) == 1
expected_msg = 'No model data to remove for project dummy-fi.'
assert expected_msg == caplog.records[0].message


def test_loadvoc_tsv(testdatadir):
with contextlib.suppress(FileNotFoundError):
os.remove(str(testdatadir.join('projects/tfidf-fi/subjects')))
Expand Down