-
Notifications
You must be signed in to change notification settings - Fork 3
✨ Write needextend rst from need-id-refs markers #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8557e7d
init impl
juiwenchen 71e0a3a
add CLI for needextend bridge
juiwenchen 32da3d3
make need id valid and update TC
juiwenchen 56c8286
created a demo
juiwenchen 5f84930
removed json
juiwenchen 42d0176
for TC
juiwenchen 3a62190
adapted new output model
juiwenchen d31f2ed
changed bridge to write
juiwenchen 6e55a28
removed option types
juiwenchen 27f7fdc
improve msg for validation
juiwenchen a8417ed
fixed n:1 mapping of need_ids and urls
juiwenchen 5b91099
updated TCs
juiwenchen 3a94b6b
updated rye script
juiwenchen b3add9e
update TC for other platforms
juiwenchen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,9 @@ | ||
| from collections import deque | ||
| import json | ||
| from os import linesep | ||
| from pathlib import Path | ||
| import tomllib | ||
| from typing import Annotated, cast | ||
| from typing import Annotated, TypeAlias, cast | ||
|
|
||
| import typer | ||
|
|
||
|
|
@@ -13,6 +14,8 @@ | |
| CodeLinksProjectConfigType, | ||
| generate_project_configs, | ||
| ) | ||
| from sphinx_codelinks.logger import logger | ||
| from sphinx_codelinks.needextend_write import MarkedObjType, convert_marked_content | ||
| from sphinx_codelinks.source_discover.config import ( | ||
| CommentType, | ||
| SourceDiscoverConfig, | ||
|
|
@@ -23,6 +26,33 @@ | |
| app = typer.Typer( | ||
| no_args_is_help=True, context_settings={"help_option_names": ["-h", "--help"]} | ||
| ) | ||
| write_app = typer.Typer( | ||
| help="Export marked content to other formats", no_args_is_help=True | ||
| ) | ||
| app.add_typer(write_app, name="write", rich_help_panel="Sub-menus") | ||
|
|
||
| OptVerbose: TypeAlias = Annotated[ # noqa: UP040 # has to be TypeAlias | ||
| bool, | ||
| typer.Option( | ||
| ..., | ||
| "-v", | ||
| "--verbose", | ||
| is_flag=True, | ||
| help="Show debug information", | ||
| rich_help_panel="Logging", | ||
| ), | ||
| ] | ||
| OptQuiet: TypeAlias = Annotated[ # noqa: UP040 # has to be TypeAlias | ||
| bool, | ||
| typer.Option( | ||
| ..., | ||
| "-q", | ||
| "--quiet", | ||
| is_flag=True, | ||
| help="Only show errors and warnings", | ||
| rich_help_panel="Logging", | ||
| ), | ||
| ] | ||
|
|
||
|
|
||
| @app.command(no_args_is_help=True) | ||
|
|
@@ -190,6 +220,79 @@ def discover( | |
| typer.echo(file_path) | ||
|
|
||
|
|
||
| @write_app.command("rst", no_args_is_help=True) | ||
| def write_rst( # noqa: PLR0913 # for CLI, so it takes as many as it requires | ||
| jsonpath: Annotated[ | ||
| Path, | ||
| typer.Argument( | ||
| ..., | ||
| help="Path of the JSON file which contains the extracted markers", | ||
| show_default=False, | ||
| dir_okay=False, | ||
| file_okay=True, | ||
| exists=True, | ||
| resolve_path=True, | ||
| ), | ||
| ], | ||
| outpath: Annotated[ | ||
| Path, | ||
| typer.Option( | ||
| "--outpath", | ||
| "-o", | ||
| help="The output path for generated rst file", | ||
| show_default=True, | ||
| dir_okay=False, | ||
| file_okay=True, | ||
| exists=False, | ||
| ), | ||
| ] = Path("needextend.rst"), | ||
| remote_url_field: Annotated[ | ||
| str, | ||
| typer.Option( | ||
| "--remote-url-field", | ||
| "-r", | ||
| help="The field name for the remote url", | ||
| show_default=True, | ||
| ), | ||
| ] = "remote_url", # to show default value in this CLI | ||
| title: Annotated[ | ||
| str | None, | ||
| typer.Option( | ||
| "--title", | ||
| "-t", | ||
| help="Give the title to the generated RST file", | ||
| show_default=True, | ||
| ), | ||
| ] = None, # to show default value in this CLI | ||
| verbose: OptVerbose = False, | ||
| quiet: OptQuiet = False, | ||
| ) -> None: | ||
| """Generate needextend.rst from the extracted obj in JSON.""" | ||
| logger.configure(verbose, quiet) | ||
| try: | ||
| with jsonpath.open("r") as f: | ||
| marked_content = json.load(f) | ||
| except Exception as e: | ||
| raise typer.BadParameter( | ||
| f"Failed to load marked content from {jsonpath}: {e}" | ||
| ) from e | ||
|
|
||
| marked_objs: list[MarkedObjType] = [ | ||
| obj for objs in marked_content.values() for obj in objs | ||
| ] | ||
|
|
||
| needextend_texts, errors = convert_marked_content( | ||
| marked_objs, remote_url_field, title | ||
| ) | ||
| if errors: | ||
| raise typer.BadParameter( | ||
| f"Errors occurred during conversion: {linesep.join(errors)}" | ||
| ) | ||
| with outpath.open("w") as f: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. btw, this can also fail easily - permission issues and stuff |
||
| f.writelines(needextend_texts) | ||
| typer.echo(f"Generated {outpath}") | ||
|
|
||
|
|
||
| def load_config_from_toml(toml_file: Path) -> CodeLinksConfigType: | ||
| try: | ||
| with toml_file.open("rb") as f: | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| from rich.console import Console | ||
| from rich.text import Text | ||
| import typer | ||
|
|
||
|
|
||
| class Logger: | ||
| __slots__ = ("console", "err_console", "quiet", "verbose") | ||
|
|
||
| def __init__(self, *, verbose: bool = False, quiet: bool = False) -> None: | ||
| self.verbose = verbose | ||
| self.quiet = quiet | ||
| self.console = Console() | ||
| self.err_console = Console(stderr=True) | ||
|
|
||
| def configure(self, verbose: bool = False, quiet: bool = False) -> None: | ||
| self.verbose = verbose | ||
| self.quiet = quiet | ||
|
|
||
| def debug( | ||
| self, | ||
| *msg: str | Text, | ||
| style: str | None = typer.colors.BRIGHT_BLACK, | ||
| highlight: bool = False, | ||
| markup: bool = False, | ||
| console: Console | None = None, | ||
| ) -> None: | ||
| """Print a debug message. | ||
|
|
||
| Will only be shown if verbose mode is enabled and not in quiet mode. | ||
| """ | ||
| if self.verbose and not self.quiet: | ||
| (console or self.console).print( | ||
| *msg, style=style, highlight=highlight, markup=markup | ||
| ) | ||
|
|
||
| def info( | ||
| self, | ||
| *msg: str | Text, | ||
| style: str | None = None, | ||
| highlight: bool = False, | ||
| markup: bool = False, | ||
| no_wrap: bool | None = None, | ||
| console: Console | None = None, | ||
| ) -> None: | ||
| """Print an informational message. | ||
|
|
||
| Will be suppressed in quiet mode. | ||
| """ | ||
| if not self.quiet: | ||
| (console or self.console).print( | ||
| *msg, style=style, highlight=highlight, markup=markup, no_wrap=no_wrap | ||
| ) | ||
|
|
||
| def result( | ||
| self, | ||
| *msg: str | Text, | ||
| style: str | None = None, | ||
| highlight: bool = False, | ||
| markup: bool = False, | ||
| console: Console | None = None, | ||
| ) -> None: | ||
| """Print a result message, like info but ignores quiet mode.""" | ||
| (console or self.console).print( | ||
| *msg, style=style, highlight=highlight, markup=markup | ||
| ) | ||
|
|
||
| def warning( | ||
| self, | ||
| *msg: str | Text, | ||
| style: str | None = typer.colors.YELLOW, | ||
| highlight: bool = False, | ||
| markup: bool = False, | ||
| console: Console | None = None, | ||
| ) -> None: | ||
| """Print a warning message.""" | ||
| (console or self.console).print( | ||
| *msg, style=style, highlight=highlight, markup=markup | ||
| ) | ||
|
|
||
| def error( | ||
| self, | ||
| *msg: str | Text, | ||
| style: str | None = typer.colors.RED, | ||
| highlight: bool = False, | ||
| markup: bool = False, | ||
| console: Console | None = None, | ||
| ) -> None: | ||
| """Print an error message.""" | ||
| (console or self.err_console).print( | ||
| *msg, style=style, highlight=highlight, markup=markup | ||
| ) | ||
|
|
||
|
|
||
| logger = Logger() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
have you head of https://docs.astral.sh/ruff/rules/blind-except/#blind-except-ble001 ?