From 4347e6d39bcd5cba49821541216897a30ffac535 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Wed, 30 Dec 2020 12:05:58 +0100 Subject: [PATCH 1/4] fixes for CLI info command --- spacy/cli/info.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/spacy/cli/info.py b/spacy/cli/info.py index 2f2515278..36c3c063d 100644 --- a/spacy/cli/info.py +++ b/spacy/cli/info.py @@ -1,10 +1,10 @@ -from typing import Optional, Dict, Any, Union +from typing import Optional, Dict, Any, Union, List import platform from pathlib import Path from wasabi import Printer, MarkdownRenderer import srsly -from ._util import app, Arg, Opt +from ._util import app, Arg, Opt, string_to_list from .. import util from .. import about @@ -15,6 +15,7 @@ def info_cli( model: Optional[str] = Arg(None, help="Optional loadable spaCy pipeline"), 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)"), + exclude: Optional[str] = Opt("labels", "--exclude", "-e", help="Comma-separated keys to exclude from the print-out"), # fmt: on ): """ @@ -24,11 +25,12 @@ def info_cli( 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( - 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]: msg = Printer(no_print=silent, pretty=not silent) if model: @@ -42,13 +44,13 @@ def info( data["Pipelines"] = ", ".join( 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 not silent: print(markdown_data) return markdown_data 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) return raw_data @@ -82,7 +84,7 @@ def info_model(model: str, *, silent: bool = True) -> Dict[str, Any]: if util.is_package(model): model_path = util.get_package_path(model) else: - model_path = model + model_path = Path(model) meta_path = model_path / "meta.json" if not meta_path.is_file(): 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. data (dict or list of tuples): Label/value pairs. @@ -108,8 +110,16 @@ def get_markdown(data: Dict[str, Any], title: Optional[str] = None) -> str: md.add(md.title(2, title)) 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 items.append(f"{md.bold(f'{key}:')} {value}") md.add(md.list(items)) return f"\n{md.text}\n" From 712a78b74a53b78f14d58d8a706dfaa20390cdc9 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Wed, 30 Dec 2020 12:35:26 +0100 Subject: [PATCH 2/4] add simple unit test --- spacy/cli/info.py | 2 +- spacy/tests/test_cli.py | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/spacy/cli/info.py b/spacy/cli/info.py index 36c3c063d..19a380eb9 100644 --- a/spacy/cli/info.py +++ b/spacy/cli/info.py @@ -19,7 +19,7 @@ def info_cli( # 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 copy-pasting to GitHub issues. diff --git a/spacy/tests/test_cli.py b/spacy/tests/test_cli.py index 59c2e14ec..9661e54ce 100644 --- a/spacy/tests/test_cli.py +++ b/spacy/tests/test_cli.py @@ -13,6 +13,19 @@ import srsly import os from .util import make_tempdir +from .. import info +from ..lang.nl import Dutch + + +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"] + assert raw_data["source"] == str(tmp_dir) def test_cli_converters_conllu_to_docs(): From c74ab6a31370ae8259530cd8eed62692d2773f69 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Wed, 30 Dec 2020 12:40:12 +0100 Subject: [PATCH 3/4] fix imports --- spacy/tests/test_cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spacy/tests/test_cli.py b/spacy/tests/test_cli.py index 9661e54ce..3442e0f6e 100644 --- a/spacy/tests/test_cli.py +++ b/spacy/tests/test_cli.py @@ -3,7 +3,9 @@ from click import NoSuchOption 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.schemas import ProjectConfigSchema, RecommendationSchema, validate +from spacy.lang.nl import Dutch from spacy.util import ENV_VARS +from spacy.cli import info 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 load_project_config, substitute_project_variables @@ -13,8 +15,6 @@ import srsly import os from .util import make_tempdir -from .. import info -from ..lang.nl import Dutch def test_cli_info(): From d5ff0fecf8317e311494615699d693d1146e7aa2 Mon Sep 17 00:00:00 2001 From: svlandeg Date: Wed, 30 Dec 2020 14:01:13 +0100 Subject: [PATCH 4/4] add docs --- spacy/tests/test_cli.py | 1 - website/docs/api/cli.md | 25 ++++++++++++++++--------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/spacy/tests/test_cli.py b/spacy/tests/test_cli.py index 3442e0f6e..bfbee677a 100644 --- a/spacy/tests/test_cli.py +++ b/spacy/tests/test_cli.py @@ -25,7 +25,6 @@ def test_cli_info(): raw_data = info(tmp_dir, exclude=[""]) assert raw_data["lang"] == "nl" assert raw_data["components"] == ["textcat"] - assert raw_data["source"] == str(tmp_dir) def test_cli_converters_conllu_to_docs(): diff --git a/website/docs/api/cli.md b/website/docs/api/cli.md index 5a6143a38..e99166af8 100644 --- a/website/docs/api/cli.md +++ b/website/docs/api/cli.md @@ -61,20 +61,27 @@ markup to copy-paste into [GitHub issues](https://github.com/explosion/spaCy/issues). ```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 -$ python -m spacy info [model] [--markdown] [--silent] +$ python -m spacy info [model] [--markdown] [--silent] [--exclude] ``` -| Name | Description | -| ------------------------------------------------ | ----------------------------------------------------------------------------------------- | -| `model` | A trained pipeline, i.e. package name or path (optional). ~~Optional[str] \(positional)~~ | -| `--markdown`, `-md` | Print information as Markdown. ~~bool (flag)~~ | -| `--silent`, `-s` 2.0.12 | Don't print anything, just return the values. ~~bool (flag)~~ | -| `--help`, `-h` | Show help message and available arguments. ~~bool (flag)~~ | -| **PRINTS** | Information about your spaCy installation. | +| Name | Description | +| ------------------------------------------------ | --------------------------------------------------------------------------------------------- | +| `model` | A trained pipeline, i.e. package name or path (optional). ~~Optional[str] \(positional)~~ | +| `--markdown`, `-md` | Print information as Markdown. ~~bool (flag)~~ | +| `--silent`, `-s` 2.0.12 | 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)~~ | +| **PRINTS** | Information about your spaCy installation. | ## validate {#validate new="2" tag="command"}