-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement
linksmith inventory
and linksmith output-formats
... based on `sphobjinv` and others.
- Loading branch information
Showing
27 changed files
with
794 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
*.inv binary |
This file contains 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 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 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,19 @@ | ||
# Backlog | ||
|
||
## Iteration +1 | ||
- Docs: Based on sphobjinv. | ||
- Response caching to buffer subsequent invocations | ||
- Add output flavor, like `--details=compact,full`. | ||
**Full details**, well, should display **full URLs**, ready for | ||
navigational consumption (clicking). | ||
- Improve HTML output. (sticky breadcrumb/navbar, etc.) | ||
|
||
## Iteration +2 | ||
sphobjinv call delegation ftw. | ||
``` | ||
# Shorthand command ... | ||
anansi suggest matplotlib draw | ||
# ... for: | ||
sphobjinv suggest -u https://matplotlib.org/stable/ draw | ||
``` |
This file contains 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 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,12 @@ | ||
(setup)= | ||
# Setup | ||
|
||
Up until published on PyPI, please install the package that way. Thank you. | ||
|
||
```bash | ||
pip install 'linksmith @ git+https://github.com/tech-writing/linksmith.git' | ||
``` | ||
|
||
:::{note} | ||
This command will need an installation of Git on your system. | ||
::: |
This file contains 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,48 @@ | ||
(usage)= | ||
# Usage | ||
|
||
Linksmith provides the `linksmith` command line program. It harbours | ||
different subsystems, accessible by using corresponding subcommands, | ||
like `linksmith inventory`. | ||
|
||
:::{warning} | ||
Here be dragons. Please note the program is pre-alpha, and a work in | ||
progress, so everything may change while we go. | ||
::: | ||
|
||
|
||
## Output Formats | ||
Display all the available output formats at a glance. | ||
```shell | ||
linksmith output-formats | ||
``` | ||
|
||
|
||
## Sphinx Inventories | ||
The `linksmith inventory` subsystem supports working with Sphinx inventories, | ||
it is heavily based on `sphinx.ext.intersphinx` and `sphobjinv`. | ||
|
||
:::{rubric} Single Inventory | ||
::: | ||
Refer to `objects.inv` on the local filesystem or on a remote location. | ||
```shell | ||
linksmith inventory /path/to/objects.inv | ||
``` | ||
```shell | ||
linksmith inventory https://linksmith.readthedocs.io/en/latest/objects.inv | ||
``` | ||
|
||
```shell | ||
linksmith inventory \ | ||
https://linksmith.readthedocs.io/en/latest/objects.inv \ | ||
--format=markdown+table | ||
``` | ||
|
||
:::{rubric} Multiple Inventories | ||
::: | ||
Refer to multiple `objects.inv` resources. | ||
```shell | ||
linksmith inventory \ | ||
https://github.com/crate/crate-docs/raw/main/registry/sphinx-inventories.txt \ | ||
--format=html+table | ||
``` |
This file contains 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,33 @@ | ||
import json | ||
|
||
import rich_click as click | ||
from pueblo.util.cli import boot_click | ||
|
||
from linksmith.settings import help_config | ||
|
||
from .model import OutputFormatRegistry | ||
from .sphinx.cli import cli as inventory_cli | ||
|
||
|
||
@click.group() | ||
@click.rich_config(help_config=help_config) | ||
@click.option("--verbose", is_flag=True, required=False, help="Turn on logging") | ||
@click.option("--debug", is_flag=True, required=False, help="Turn on logging with debug level") | ||
@click.version_option() | ||
@click.pass_context | ||
def cli(ctx: click.Context, verbose: bool, debug: bool): | ||
return boot_click(ctx, verbose, debug) | ||
|
||
|
||
@click.command() | ||
@click.rich_config(help_config=help_config) | ||
@click.pass_context | ||
def output_formats(ctx: click.Context): # noqa: ARG001 | ||
""" | ||
Display available output format aliases. | ||
""" | ||
print(json.dumps(sorted(OutputFormatRegistry.aliases()), indent=2)) | ||
|
||
|
||
cli.add_command(output_formats, name="output-formats") | ||
cli.add_command(inventory_cli, name="inventory") |
This file contains 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,70 @@ | ||
import dataclasses | ||
import io | ||
import typing as t | ||
from enum import auto | ||
from pathlib import Path | ||
|
||
from linksmith.util.python import AutoStrEnum | ||
|
||
|
||
class OutputFormat(AutoStrEnum): | ||
TEXT_INSPECT = auto() | ||
TEXT_PLAIN = auto() | ||
MARKDOWN = auto() | ||
MARKDOWN_TABLE = auto() | ||
RESTRUCTUREDTEXT = auto() | ||
HTML = auto() | ||
HTML_TABLE = auto() | ||
JSON = auto() | ||
YAML = auto() | ||
|
||
|
||
@dataclasses.dataclass | ||
class OutputFormatRule: | ||
format: OutputFormat | ||
aliases: t.List[str] | ||
|
||
|
||
class OutputFormatRegistry: | ||
rules = [ | ||
OutputFormatRule(format=OutputFormat.TEXT_INSPECT, aliases=["text"]), | ||
OutputFormatRule(format=OutputFormat.TEXT_PLAIN, aliases=["text+plain"]), | ||
OutputFormatRule(format=OutputFormat.MARKDOWN, aliases=["markdown", "md"]), | ||
OutputFormatRule(format=OutputFormat.MARKDOWN_TABLE, aliases=["markdown+table", "md+table"]), | ||
OutputFormatRule(format=OutputFormat.RESTRUCTUREDTEXT, aliases=["restructuredtext", "rst"]), | ||
OutputFormatRule(format=OutputFormat.HTML, aliases=["html", "html+table"]), | ||
OutputFormatRule(format=OutputFormat.JSON, aliases=["json"]), | ||
OutputFormatRule(format=OutputFormat.YAML, aliases=["yaml"]), | ||
] | ||
|
||
@classmethod | ||
def resolve(cls, format_: str) -> OutputFormat: | ||
for rule in cls.rules: | ||
if format_ in rule.aliases: | ||
return rule.format | ||
raise NotImplementedError(f"Output format not implemented: {format_}") | ||
|
||
@classmethod | ||
def aliases(cls) -> t.List[str]: | ||
data = [] | ||
for rule in cls.rules: | ||
data += rule.aliases | ||
return data | ||
|
||
|
||
class ResourceType(AutoStrEnum): | ||
BUFFER = auto() | ||
PATH = auto() | ||
URL = auto() | ||
|
||
@classmethod | ||
def detect(cls, location): | ||
if isinstance(location, io.IOBase): | ||
return cls.BUFFER | ||
path = Path(location) | ||
if path.exists(): | ||
return cls.PATH | ||
elif location.startswith("http://") or location.startswith("https://"): | ||
return cls.URL | ||
else: | ||
raise NotImplementedError(f"Resource type not implemented: {location}") |
This file contains 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,10 @@ | ||
import rich_click as click | ||
|
||
help_config = click.RichHelpConfiguration( | ||
use_markdown=True, | ||
width=100, | ||
style_option="bold white", | ||
style_argument="dim cyan", | ||
style_command="bold yellow", | ||
style_errors_suggestion_command="bold magenta", | ||
) |
Empty file.
This file contains 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,47 @@ | ||
import typing as t | ||
|
||
import rich_click as click | ||
from click import ClickException | ||
|
||
from linksmith.settings import help_config | ||
from linksmith.sphinx.core import inventories_to_text, inventory_to_text | ||
|
||
|
||
@click.command() | ||
@click.rich_config(help_config=help_config) | ||
@click.argument("infiles", nargs=-1) | ||
@click.option("--format", "format_", type=str, default="text", help="Output format") | ||
@click.pass_context | ||
def cli(ctx: click.Context, infiles: t.List[str], format_: str): | ||
""" | ||
Decode one or multiple intersphinx inventories and output in different formats. | ||
Use `linksmith output-formats` to learn about available output formats. | ||
Examples: | ||
Refer to `objects.inv` on the local filesystem or on a remote location: | ||
```bash | ||
linksmith inventory /path/to/objects.inv --format=html | ||
linksmith inventory https://linksmith.readthedocs.io/en/latest/objects.inv --format=markdown | ||
``` | ||
Refer to **multiple** `objects.inv` resources: | ||
```bash | ||
linksmith inventory https://github.com/crate/crate-docs/raw/main/registry/sphinx-inventories.txt | ||
``` | ||
""" | ||
if not infiles: | ||
raise click.ClickException("No input") | ||
for infile in infiles: | ||
try: | ||
if infile.endswith(".inv"): | ||
inventory_to_text(infile, format_=format_) | ||
elif infile.endswith(".txt"): | ||
inventories_to_text(infile, format_=format_) | ||
else: | ||
raise NotImplementedError(f"Unknown input file type: {infile}") | ||
except Exception as ex: | ||
if ctx.parent and ctx.parent.params.get("debug"): | ||
raise | ||
raise ClickException(f"{ex.__class__.__name__}: {ex}") |
Oops, something went wrong.