Merge pull request #6651 from svlandeg/bugfix/cli_info

This commit is contained in:
Ines Montani 2021-01-05 13:44:26 +11:00 committed by GitHub
commit c4993f16d0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 48 additions and 19 deletions

View File

@ -1,10 +1,10 @@
from typing import Optional, Dict, Any, Union from typing import Optional, Dict, Any, Union, List
import platform import platform
from pathlib import Path from pathlib import Path
from wasabi import Printer, MarkdownRenderer from wasabi import Printer, MarkdownRenderer
import srsly import srsly
from ._util import app, Arg, Opt from ._util import app, Arg, Opt, string_to_list
from .. import util from .. import util
from .. import about from .. import about
@ -15,20 +15,22 @@ def info_cli(
model: Optional[str] = Arg(None, help="Optional loadable spaCy pipeline"), model: Optional[str] = Arg(None, help="Optional loadable spaCy pipeline"),
markdown: bool = Opt(False, "--markdown", "-md", help="Generate Markdown for GitHub issues"), markdown: bool = Opt(False, "--markdown", "-md", help="Generate Markdown for GitHub issues"),
silent: bool = Opt(False, "--silent", "-s", "-S", help="Don't print anything (just return)"), silent: bool = Opt(False, "--silent", "-s", "-S", help="Don't print anything (just return)"),
exclude: Optional[str] = Opt("labels", "--exclude", "-e", help="Comma-separated keys to exclude from the print-out"),
# fmt: on # fmt: on
): ):
""" """
Print info about spaCy installation. If a pipeline is speficied as an argument, Print info about spaCy installation. If a pipeline is specified as an argument,
print its meta information. Flag --markdown prints details in Markdown for easy print its meta information. Flag --markdown prints details in Markdown for easy
copy-pasting to GitHub issues. copy-pasting to GitHub issues.
DOCS: https://nightly.spacy.io/api/cli#info DOCS: https://nightly.spacy.io/api/cli#info
""" """
info(model, markdown=markdown, silent=silent) exclude = string_to_list(exclude)
info(model, markdown=markdown, silent=silent, exclude=exclude)
def info( def info(
model: Optional[str] = None, *, markdown: bool = False, silent: bool = True model: Optional[str] = None, *, markdown: bool = False, silent: bool = True, exclude: List[str]
) -> Union[str, dict]: ) -> Union[str, dict]:
msg = Printer(no_print=silent, pretty=not silent) msg = Printer(no_print=silent, pretty=not silent)
if model: if model:
@ -42,13 +44,13 @@ def info(
data["Pipelines"] = ", ".join( data["Pipelines"] = ", ".join(
f"{n} ({v})" for n, v in data["Pipelines"].items() f"{n} ({v})" for n, v in data["Pipelines"].items()
) )
markdown_data = get_markdown(data, title=title) markdown_data = get_markdown(data, title=title, exclude=exclude)
if markdown: if markdown:
if not silent: if not silent:
print(markdown_data) print(markdown_data)
return markdown_data return markdown_data
if not silent: if not silent:
table_data = dict(data) table_data = {k: v for k, v in data.items() if k not in exclude}
msg.table(table_data, title=title) msg.table(table_data, title=title)
return raw_data return raw_data
@ -82,7 +84,7 @@ def info_model(model: str, *, silent: bool = True) -> Dict[str, Any]:
if util.is_package(model): if util.is_package(model):
model_path = util.get_package_path(model) model_path = util.get_package_path(model)
else: else:
model_path = model model_path = Path(model)
meta_path = model_path / "meta.json" meta_path = model_path / "meta.json"
if not meta_path.is_file(): if not meta_path.is_file():
msg.fail("Can't find pipeline meta.json", meta_path, exits=1) msg.fail("Can't find pipeline meta.json", meta_path, exits=1)
@ -96,7 +98,7 @@ def info_model(model: str, *, silent: bool = True) -> Dict[str, Any]:
} }
def get_markdown(data: Dict[str, Any], title: Optional[str] = None) -> str: def get_markdown(data: Dict[str, Any], title: Optional[str] = None, exclude: List[str] = None) -> str:
"""Get data in GitHub-flavoured Markdown format for issues etc. """Get data in GitHub-flavoured Markdown format for issues etc.
data (dict or list of tuples): Label/value pairs. data (dict or list of tuples): Label/value pairs.
@ -108,7 +110,15 @@ def get_markdown(data: Dict[str, Any], title: Optional[str] = None) -> str:
md.add(md.title(2, title)) md.add(md.title(2, title))
items = [] items = []
for key, value in data.items(): for key, value in data.items():
if isinstance(value, str) and Path(value).exists(): if exclude and key in exclude:
continue
if isinstance(value, str):
try:
existing_path = Path(value).exists()
except:
# invalid Path, like a URL string
existing_path = False
if existing_path:
continue continue
items.append(f"{md.bold(f'{key}:')} {value}") items.append(f"{md.bold(f'{key}:')} {value}")
md.add(md.list(items)) md.add(md.list(items))

View File

@ -3,7 +3,9 @@ from click import NoSuchOption
from spacy.training import docs_to_json, offsets_to_biluo_tags from spacy.training import docs_to_json, offsets_to_biluo_tags
from spacy.training.converters import iob_to_docs, conll_ner_to_docs, conllu_to_docs from spacy.training.converters import iob_to_docs, conll_ner_to_docs, conllu_to_docs
from spacy.schemas import ProjectConfigSchema, RecommendationSchema, validate from spacy.schemas import ProjectConfigSchema, RecommendationSchema, validate
from spacy.lang.nl import Dutch
from spacy.util import ENV_VARS from spacy.util import ENV_VARS
from spacy.cli import info
from spacy.cli.init_config import init_config, RECOMMENDATIONS from spacy.cli.init_config import init_config, RECOMMENDATIONS
from spacy.cli._util import validate_project_commands, parse_config_overrides from spacy.cli._util import validate_project_commands, parse_config_overrides
from spacy.cli._util import load_project_config, substitute_project_variables from spacy.cli._util import load_project_config, substitute_project_variables
@ -15,6 +17,16 @@ import os
from .util import make_tempdir from .util import make_tempdir
def test_cli_info():
nlp = Dutch()
nlp.add_pipe("textcat")
with make_tempdir() as tmp_dir:
nlp.to_disk(tmp_dir)
raw_data = info(tmp_dir, exclude=[""])
assert raw_data["lang"] == "nl"
assert raw_data["components"] == ["textcat"]
def test_cli_converters_conllu_to_docs(): def test_cli_converters_conllu_to_docs():
# from NorNE: https://github.com/ltgoslo/norne/blob/3d23274965f513f23aa48455b28b1878dad23c05/ud/nob/no_bokmaal-ud-dev.conllu # from NorNE: https://github.com/ltgoslo/norne/blob/3d23274965f513f23aa48455b28b1878dad23c05/ud/nob/no_bokmaal-ud-dev.conllu
lines = [ lines = [

View File

@ -61,18 +61,25 @@ markup to copy-paste into
[GitHub issues](https://github.com/explosion/spaCy/issues). [GitHub issues](https://github.com/explosion/spaCy/issues).
```cli ```cli
$ python -m spacy info [--markdown] [--silent] $ python -m spacy info [--markdown] [--silent] [--exclude]
``` ```
> #### Example
>
> ```cli
> $ python -m spacy info en_core_web_lg --markdown
> ```
```cli ```cli
$ python -m spacy info [model] [--markdown] [--silent] $ python -m spacy info [model] [--markdown] [--silent] [--exclude]
``` ```
| Name | Description | | Name | Description |
| ------------------------------------------------ | ----------------------------------------------------------------------------------------- | | ------------------------------------------------ | --------------------------------------------------------------------------------------------- |
| `model` | A trained pipeline, i.e. package name or path (optional). ~~Optional[str] \(positional)~~ | | `model` | A trained pipeline, i.e. package name or path (optional). ~~Optional[str] \(positional)~~ |
| `--markdown`, `-md` | Print information as Markdown. ~~bool (flag)~~ | | `--markdown`, `-md` | Print information as Markdown. ~~bool (flag)~~ |
| `--silent`, `-s` <Tag variant="new">2.0.12</Tag> | Don't print anything, just return the values. ~~bool (flag)~~ | | `--silent`, `-s` <Tag variant="new">2.0.12</Tag> | Don't print anything, just return the values. ~~bool (flag)~~ |
| `--exclude`, `-e` | Comma-separated keys to exclude from the print-out. Defaults to `"labels"`. ~~Optional[str]~~ |
| `--help`, `-h` | Show help message and available arguments. ~~bool (flag)~~ | | `--help`, `-h` | Show help message and available arguments. ~~bool (flag)~~ |
| **PRINTS** | Information about your spaCy installation. | | **PRINTS** | Information about your spaCy installation. |