2017-03-18 15:01:16 +03:00
|
|
|
# coding: utf8
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2017-05-22 13:28:58 +03:00
|
|
|
import plac
|
2017-03-18 15:01:16 +03:00
|
|
|
import platform
|
|
|
|
from pathlib import Path
|
2018-11-30 22:16:14 +03:00
|
|
|
from wasabi import Printer
|
2017-03-18 20:57:45 +03:00
|
|
|
|
2018-04-03 16:50:31 +03:00
|
|
|
from ._messages import Messages
|
2018-12-01 06:55:48 +03:00
|
|
|
from ..compat import path2str, basestring_, unicode_
|
2017-03-18 17:14:48 +03:00
|
|
|
from .. import util
|
2018-04-03 16:50:31 +03:00
|
|
|
from .. import about
|
2017-03-18 15:01:16 +03:00
|
|
|
|
|
|
|
|
2017-05-22 13:28:58 +03:00
|
|
|
@plac.annotations(
|
2018-11-30 22:16:14 +03:00
|
|
|
model=("Optional shortcut link of model", "positional", None, str),
|
|
|
|
markdown=("Generate Markdown for GitHub issues", "flag", "md", str),
|
|
|
|
silent=("Don't print anything (just return)", "flag", "s"),
|
|
|
|
)
|
2018-04-29 02:59:44 +03:00
|
|
|
def info(model=None, markdown=False, silent=False):
|
2018-11-30 22:16:14 +03:00
|
|
|
"""
|
|
|
|
Print info about spaCy installation. If a model shortcut link is
|
2017-05-22 13:28:58 +03:00
|
|
|
speficied as an argument, print model information. Flag --markdown
|
|
|
|
prints details in Markdown for easy copy-pasting to GitHub issues.
|
|
|
|
"""
|
2018-11-30 22:16:14 +03:00
|
|
|
msg = Printer()
|
2017-03-18 15:01:16 +03:00
|
|
|
if model:
|
2017-05-28 01:22:00 +03:00
|
|
|
if util.is_package(model):
|
|
|
|
model_path = util.get_package_path(model)
|
|
|
|
else:
|
|
|
|
model_path = util.get_data_path() / model
|
2018-11-30 22:16:14 +03:00
|
|
|
meta_path = model_path / "meta.json"
|
2017-05-28 01:22:00 +03:00
|
|
|
if not meta_path.is_file():
|
2018-11-30 22:16:14 +03:00
|
|
|
msg.fail(Messages.M020, meta_path, exits=1)
|
2017-06-04 21:44:15 +03:00
|
|
|
meta = util.read_json(meta_path)
|
2017-03-18 15:01:16 +03:00
|
|
|
if model_path.resolve() != model_path:
|
2018-11-30 22:16:14 +03:00
|
|
|
meta["link"] = path2str(model_path)
|
|
|
|
meta["source"] = path2str(model_path.resolve())
|
2017-03-18 15:01:16 +03:00
|
|
|
else:
|
2018-11-30 22:16:14 +03:00
|
|
|
meta["source"] = path2str(model_path)
|
2018-04-29 02:59:44 +03:00
|
|
|
if not silent:
|
2018-11-30 22:16:14 +03:00
|
|
|
title = "Info about model '{}'".format(model)
|
|
|
|
model_meta = {
|
|
|
|
k: v for k, v in meta.items() if k not in ("accuracy", "speed")
|
|
|
|
}
|
|
|
|
if markdown:
|
2018-12-01 06:55:48 +03:00
|
|
|
print_markdown(model_meta, title=title)
|
2018-11-30 22:16:14 +03:00
|
|
|
else:
|
|
|
|
msg.table(model_meta, title=title)
|
2018-04-29 02:59:44 +03:00
|
|
|
return meta
|
2018-11-30 22:16:14 +03:00
|
|
|
data = {
|
|
|
|
"spaCy version": about.__version__,
|
|
|
|
"Location": path2str(Path(__file__).parent.parent),
|
|
|
|
"Platform": platform.platform(),
|
|
|
|
"Python version": platform.python_version(),
|
|
|
|
"Models": list_models(),
|
|
|
|
}
|
2018-04-29 02:59:44 +03:00
|
|
|
if not silent:
|
2018-11-30 22:16:14 +03:00
|
|
|
title = "Info about spaCy"
|
|
|
|
if markdown:
|
2018-12-01 06:55:48 +03:00
|
|
|
print_markdown(data, title=title)
|
2018-11-30 22:16:14 +03:00
|
|
|
else:
|
|
|
|
msg.table(data, title=title)
|
2018-04-29 02:59:44 +03:00
|
|
|
return data
|
2017-03-18 15:01:16 +03:00
|
|
|
|
|
|
|
|
|
|
|
def list_models():
|
2017-05-08 00:25:29 +03:00
|
|
|
def exclude_dir(dir_name):
|
|
|
|
# exclude common cache directories and hidden directories
|
2018-11-30 22:16:14 +03:00
|
|
|
exclude = ("cache", "pycache", "__pycache__")
|
|
|
|
return dir_name in exclude or dir_name.startswith(".")
|
|
|
|
|
2017-03-18 15:01:16 +03:00
|
|
|
data_path = util.get_data_path()
|
2017-04-14 17:48:02 +03:00
|
|
|
if data_path:
|
|
|
|
models = [f.parts[-1] for f in data_path.iterdir() if f.is_dir()]
|
2018-11-30 22:16:14 +03:00
|
|
|
return ", ".join([m for m in models if not exclude_dir(m)])
|
|
|
|
return "-"
|
2018-12-01 06:55:48 +03:00
|
|
|
|
|
|
|
|
|
|
|
def print_markdown(data, title=None):
|
|
|
|
"""Print data in GitHub-flavoured Markdown format for issues etc.
|
|
|
|
|
|
|
|
data (dict or list of tuples): Label/value pairs.
|
|
|
|
title (unicode or None): Title, will be rendered as headline 2.
|
|
|
|
"""
|
2018-12-01 06:59:12 +03:00
|
|
|
markdown = []
|
|
|
|
for key, value in data.items():
|
|
|
|
if isinstance(value, basestring_) and Path(value).exists():
|
|
|
|
continue
|
|
|
|
markdown.append("* **{}:** {}".format(key, unicode_(value)))
|
2018-12-01 06:55:48 +03:00
|
|
|
if title:
|
|
|
|
print("\n## {}".format(title))
|
|
|
|
print("\n{}\n".format("\n".join(markdown)))
|