Add directory cli and set up command line interface

This commit is contained in:
ines 2017-03-18 15:14:48 +01:00
parent 387e34a3c5
commit ec3e810662
8 changed files with 81 additions and 49 deletions

View File

@ -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

68
spacy/__main__.py Normal file
View File

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

3
spacy/cli/__init__.py Normal file
View File

@ -0,0 +1,3 @@
from .download import download
from .info import info
from .link import link

View File

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

View File

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

View File

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

View File

@ -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:

View File

@ -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