Skip to content

Commit 2bd3239

Browse files
juiwenchenubmarco
authored andcommitted
add CLI for needextend bridge
1 parent 5a33aac commit 2bd3239

3 files changed

Lines changed: 102 additions & 16 deletions

File tree

src/sphinx_codelinks/cmd.py

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from os import linesep
55
from pathlib import Path
66
import tomllib
7-
from typing import Annotated, cast
7+
from typing import Annotated, TypeAlias, cast
88

99
import typer
1010

@@ -22,6 +22,8 @@
2222
SourceAnalyseConfigType,
2323
SrcDiscoverConfigType4Analyse,
2424
)
25+
from sphinx_codelinks.logger import logger
26+
from sphinx_codelinks.needextend_bridge import convert_marked_content
2527
from sphinx_codelinks.source_discover.config import (
2628
SourceDiscoverConfig,
2729
SourceDiscoverConfigType,
@@ -34,6 +36,30 @@
3436
)
3537

3638

39+
OptVerbose: TypeAlias = Annotated[ # noqa: UP040 # has to be TypeAlias
40+
bool,
41+
typer.Option(
42+
...,
43+
"-v",
44+
"--verbose",
45+
is_flag=True,
46+
help="Show debug information",
47+
rich_help_panel="Logging",
48+
),
49+
]
50+
OptQuiet: TypeAlias = Annotated[ # noqa: UP040 # has to be TypeAlias
51+
bool,
52+
typer.Option(
53+
...,
54+
"-q",
55+
"--quiet",
56+
is_flag=True,
57+
help="Only show errors and warnings",
58+
rich_help_panel="Logging",
59+
),
60+
]
61+
62+
3763
@app.command(no_args_is_help=True)
3864
def analyse(
3965
config: Annotated[
@@ -212,6 +238,49 @@ def discover(
212238
typer.echo(file_path)
213239

214240

241+
@app.command(no_args_is_help=True)
242+
def bridge(
243+
jsonpath: Annotated[
244+
Path,
245+
typer.Argument(
246+
...,
247+
help="Path of the JSON file which contains the extracted markers",
248+
show_default=False,
249+
dir_okay=False,
250+
file_okay=True,
251+
exists=True,
252+
resolve_path=True,
253+
),
254+
],
255+
outdir: Annotated[
256+
Path,
257+
typer.Option(
258+
"--outdir",
259+
"-o",
260+
help="The output directory for needextend.rst",
261+
show_default=True,
262+
dir_okay=True,
263+
file_okay=False,
264+
exists=True,
265+
),
266+
] = Path.cwd(), # noqa: B008 # to show default value in this CLI
267+
remote_url_field: Annotated[
268+
str,
269+
typer.Option(
270+
"--remote-url-field",
271+
"-r",
272+
help="The field name for the remote url",
273+
show_default=True,
274+
),
275+
] = "remote_url", # to show default value in this CLI
276+
verbose: OptVerbose = False,
277+
quiet: OptQuiet = False,
278+
) -> None:
279+
"""Generate needextend.rst from the extracted obj in JSON."""
280+
logger.configure(verbose, quiet)
281+
convert_marked_content(jsonpath, outdir, remote_url_field)
282+
283+
215284
def load_config_from_toml(
216285
toml_file: Path, project: str | None = None
217286
) -> SrcTraceProjectConfigFileType:
Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,31 @@ def field_names(cls) -> set[str]:
4040
"schema": {
4141
"type": "object",
4242
"properties": {
43-
"row": {"type": "integer"},
44-
"column": {"type": "integer"},
43+
"start": {
44+
"type": "object",
45+
"properties": {
46+
"row": {"type": "integer"},
47+
"column": {"type": "integer"},
48+
},
49+
"required": ["row", "column"],
50+
},
51+
"end": {
52+
"type": "object",
53+
"properties": {
54+
"row": {"type": "integer"},
55+
"column": {"type": "integer"},
56+
},
57+
"required": ["row", "column"],
58+
},
4559
},
46-
"required": ["row", "column"],
60+
"required": ["start", "end"],
4761
"additionalProperties": False,
4862
}
4963
}
5064
)
5165
"""Coordinate of the marked content in a file"""
5266

53-
tagged_scope: str | None = field(
54-
metadata={"schema": {"type": "string", "nullable": True}}
55-
)
67+
tagged_scope: str | None = field(metadata={"schema": {"type": ["string", "null"]}})
5668
"""The scoped tagged by the marked content"""
5769

5870
type: MarkedContentType = field(
@@ -66,20 +78,20 @@ def field_names(cls) -> set[str]:
6678
"""Type of the marked content."""
6779

6880
marker: str | None = field(
69-
default=None, metadata={"schema": {"type": "string", "nullable": True}}
81+
default=None, metadata={"schema": {"type": ["string", "null"]}}
7082
)
7183
"""Marker of the marked content."""
7284

7385
need_ids: list[str] | None = field(
7486
default=None,
75-
metadata={"schema": {"type": "array", "items": {"type": "string"}}},
87+
metadata={"schema": {"type": ["array", "null"], "items": {"type": "string"}}},
7688
)
7789
"""Need id refs which is associated to the need items in the documentation."""
7890
need: dict[str, str | list[str]] | None = field(
7991
default=None,
8092
metadata={
8193
"schema": {
82-
"type": "object",
94+
"type": ["object", "null"],
8395
"properties": {"title": {"type": "string"}, "id": {"title": "string"}},
8496
"required": ["title", "id"],
8597
"additionalProperties": True,
@@ -89,7 +101,7 @@ def field_names(cls) -> set[str]:
89101
"""Definition of a need item"""
90102

91103
rst: str | None = field(
92-
default=None, metadata={"schema": {"type": "string", "nullable": True}}
104+
default=None, metadata={"schema": {"type": ["string", "null"]}}
93105
)
94106
"""Extracted rst text."""
95107

@@ -151,7 +163,7 @@ class MarkedObjType(TypedDict):
151163
rst: str | None
152164

153165

154-
def convert_makred_content(
166+
def convert_marked_content(
155167
jsonpath: Path, outdir: Path, remote_url_field: str = "remote_url"
156168
) -> None:
157169
"""Convert marked objects extracted by anaylse CLI to needextend in RST"""
@@ -172,7 +184,7 @@ def convert_makred_content(
172184
obj_warnings.extend(schema.check_schema())
173185
obj_warnings.extend(schema.check_conditional_required_fields())
174186
if obj_warnings:
175-
obj_warnings.appendleft(f"Marked object {obj} has the following warnings:")
187+
obj_warnings.appendleft(f"{obj} has the following warnings:")
176188
warnings.extend(list(obj_warnings))
177189

178190
if warnings:
@@ -193,3 +205,5 @@ def convert_makred_content(
193205
needextend_path = outdir / "needextend.rst"
194206
with needextend_path.open("w") as f:
195207
f.writelines(needextend_texts)
208+
209+
logger.info(f"Generated needextend.rst in {needextend_path}")

tests/test_bridge.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
from pathlib import Path
22

3-
from sphinx_codelinks.bridge import convert_makred_content
3+
from sphinx_codelinks.needextend_bridge import convert_marked_content
44

55

6-
def test_bridge():
6+
def test_bridge(tmp_path):
77
jsonpath = Path(__file__).parent / "data" / "analyse" / "marked_content.json"
8-
convert_makred_content(jsonpath, outdir=Path(__file__).parent.parent / "output")
8+
convert_marked_content(jsonpath, outdir=tmp_path)
9+
needextend_path = tmp_path / "needextend.rst"
10+
11+
assert needextend_path.exists()

0 commit comments

Comments
 (0)