From ec3e8106620ef80b848dcb4664a99f4c0dfdc073 Mon Sep 17 00:00:00 2001 From: ines Date: Sat, 18 Mar 2017 15:14:48 +0100 Subject: [PATCH] Add directory cli and set up command line interface --- spacy/__init__.py | 2 +- spacy/__main__.py | 68 ++++++++++++++++++++++++++++++++++++ spacy/cli/__init__.py | 3 ++ spacy/{ => cli}/download.py | 15 ++------ spacy/{ => cli}/info.py | 21 ++--------- spacy/{ => cli}/link.py | 15 +------- spacy/deprecated.py | 4 +-- spacy/tests/test_download.py | 2 +- 8 files changed, 81 insertions(+), 49 deletions(-) create mode 100644 spacy/__main__.py create mode 100644 spacy/cli/__init__.py rename spacy/{ => cli}/download.py (88%) rename spacy/{ => cli}/info.py (73%) rename spacy/{ => cli}/link.py (76%) diff --git a/spacy/__init__.py b/spacy/__init__.py index 6b6a34524..a6bc5c1cd 100644 --- a/spacy/__init__.py +++ b/spacy/__init__.py @@ -5,7 +5,7 @@ import json from pathlib import Path from .util import set_lang_class, get_lang_class, parse_package_meta from .deprecated import resolve_model_name -from .info import info +from .cli.info import info from . import en from . import de diff --git a/spacy/__main__.py b/spacy/__main__.py new file mode 100644 index 000000000..b252c4a44 --- /dev/null +++ b/spacy/__main__.py @@ -0,0 +1,68 @@ +# coding: utf8 +from __future__ import unicode_literals, print_function + +import plac +from spacy.cli import download as cli_download +from spacy.cli import link as cli_link +from spacy.cli import info as cli_info + + +class CLI(object): + """Command-line interface for spaCy""" + + commands = ('download', 'link', 'info') + + @plac.annotations( + model=("model to download (shortcut or model name)", "positional", None, str), + direct=("force direct download. Needs model name with version and won't " + "perform compatibility check", "flag", "d", bool) + ) + def download(self, model=None, direct=False): + """ + Download compatible model from default download path using pip. Model + can be shortcut, model name or, if --direct flag is set, full model name + with version. + """ + + cli_download(model, direct) + + + @plac.annotations( + origin=("package name or local path to model", "positional", None, str), + link_name=("Name of shortuct link to create", "positional", None, str), + force=("Force overwriting of existing link", "flag", "f", bool) + ) + def link(self, origin, link_name, force=False): + """ + Create a symlink for models within the spacy/data directory. Accepts + either the name of a pip package, or the local path to the model data + directory. Linking models allows loading them via spacy.load(link_name). + """ + + cli_link(origin, link_name, force) + + + @plac.annotations( + model=("optional: shortcut link of model", "positional", None, str), + markdown=("generate Markdown for GitHub issues", "flag", "md", str) + ) + def info(self, model=None, markdown=False): + """ + Print info about spaCy installation. If a model shortcut link is + speficied as an argument, print model information. Flag --markdown + prints details in Markdown for easy copy-pasting to GitHub issues. + """ + + cli_info(model, markdown) + + + def __missing__(self, name): + print("\n Command %r does not exist\n" % name) + + +if __name__ == '__main__': + import plac + import sys + cli = CLI() + sys.argv[0] = 'spacy' + plac.Interpreter.call(CLI) diff --git a/spacy/cli/__init__.py b/spacy/cli/__init__.py new file mode 100644 index 000000000..2c45b471a --- /dev/null +++ b/spacy/cli/__init__.py @@ -0,0 +1,3 @@ +from .download import download +from .info import info +from .link import link diff --git a/spacy/download.py b/spacy/cli/download.py similarity index 88% rename from spacy/download.py rename to spacy/cli/download.py index b4034ea61..e3c77c27a 100644 --- a/spacy/download.py +++ b/spacy/cli/download.py @@ -2,21 +2,14 @@ from __future__ import unicode_literals import pip -import plac import requests import os from .link import link_package -from . import about -from . import util +from .. import about +from .. import util -@plac.annotations( - model=("Model to download", "positional", None, str), - direct=("Force direct download", "flag", "d", bool) -) def download(model=None, direct=False): - """Download compatible model from default download path using pip.""" - check_error_depr(model) if direct: @@ -76,7 +69,3 @@ def check_error_depr(model): "or pip install. For more info on this, see the documentation: " "{d}".format(d=about.__docs__), title="Deprecated command") - - -if __name__ == '__main__': - plac.call(download) diff --git a/spacy/info.py b/spacy/cli/info.py similarity index 73% rename from spacy/info.py rename to spacy/cli/info.py index 12b8b9be0..004931ad5 100644 --- a/spacy/info.py +++ b/spacy/cli/info.py @@ -1,25 +1,14 @@ # coding: utf8 from __future__ import unicode_literals -import plac import platform import sys from pathlib import Path -from . import about -from . import util - - -@plac.annotations( - model=("Model to download", "positional", None, str), - markdown=("Generate Markdown for GitHub issues", "flag", "md", str) -) -def main(model=None, markdown=False): - info(model, markdown) +from .. import about +from .. import util def info(model=None, markdown=False): - """Print info about spaCy installation and models for debugging.""" - if model: data = util.parse_package_meta(util.get_data_path(), model, require=True) model_path = Path(__file__).parent / util.get_data_path() / model @@ -48,7 +37,7 @@ def print_info(data, title, markdown): def get_spacy_data(): return { 'spaCy version': about.__version__, - 'Location': str(Path(__file__).parent), + 'Location': str(Path(__file__).parent.parent), 'Platform': platform.platform(), 'Python version': platform.python_version(), 'Installed models': ', '.join(list_models()) @@ -58,7 +47,3 @@ def get_spacy_data(): def list_models(): data_path = util.get_data_path() return [f.parts[-1] for f in data_path.iterdir() if f.is_dir()] - - -if __name__ == '__main__': - plac.call(main) diff --git a/spacy/link.py b/spacy/cli/link.py similarity index 76% rename from spacy/link.py rename to spacy/cli/link.py index ee520c1df..f92b2295f 100644 --- a/spacy/link.py +++ b/spacy/cli/link.py @@ -5,20 +5,11 @@ import io import os import pip import site -import plac from pathlib import Path -from . import util +from .. import util -@plac.annotations( - origin=("Package name or path to model", "positional", None, str), - link_name=("Name of link", "positional", None, str), - force=("Force overwriting existing link", "flag", "f", bool) -) def link(origin, link_name, force=False): - """Create a symlink for models within the spacy/data directory. Accepts - either the name of a pip package, or the local path to the model data - directory. Linking models allows loading them via spacy.load(link_name).""" if is_package(origin): link_package(origin, link_name, force) else: @@ -68,7 +59,3 @@ def is_package(origin): if package.project_name.replace('-', '_') == origin: return True return False - - -if __name__ == '__main__': - plac.call(link) diff --git a/spacy/deprecated.py b/spacy/deprecated.py index 30be24942..c57cc2d7e 100644 --- a/spacy/deprecated.py +++ b/spacy/deprecated.py @@ -1,8 +1,8 @@ from pathlib import Path from . import about from . import util -from .download import download -from .link import link +from .cli import download +from .cli import link try: diff --git a/spacy/tests/test_download.py b/spacy/tests/test_download.py index 728cacc41..c5591bb8a 100644 --- a/spacy/tests/test_download.py +++ b/spacy/tests/test_download.py @@ -1,7 +1,7 @@ # coding: utf-8 from __future__ import unicode_literals -from ..download import download, get_compatibility, get_version, check_error_depr +from ..cli.download import download, get_compatibility, get_version, check_error_depr import pytest