Skip to content
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

Use extension loader to load renderers #542

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/man/precli.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
## SYNOPSIS

```
precli [-h] [-d] [-r] [--enable ENABLE | --disable DISABLE] [--json] [--plain]
[--markdown] [--gist] [-o OUTPUT] [--no-color] [-q] [--version]
precli [-h] [-d] [-r] [--enable ENABLE | --disable DISABLE] [--json | --plain | --markdown]
[--gist] [-o OUTPUT] [--no-color] [-q] [--version]
[targets ...]
```

Expand Down
52 changes: 23 additions & 29 deletions precli/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@
from rich.progress import TextColumn

import precli
from precli.core import loader
from precli.core.artifact import Artifact
from precli.core.run import Run
from precli.renderers.detailed import Detailed
from precli.renderers.json import Json
from precli.renderers.markdown import Markdown
from precli.renderers.plain import Plain


BUSL_URL = "https://spdx.org/licenses/BUSL-1.1.html"
Expand Down Expand Up @@ -76,22 +73,29 @@ def setup_arg_parser():
type=str,
help="comma-separated list of rule IDs or names to disable",
)
parser.add_argument(
render_grp = parser.add_mutually_exclusive_group()
render_grp.add_argument(
"--json",
dest="json",
action="store_true",
dest="renderer",
action="store_const",
const="json",
default="detailed",
help="render the output as formatted JSON",
)
parser.add_argument(
render_grp.add_argument(
"--plain",
dest="plain",
action="store_true",
dest="renderer",
action="store_const",
const="plain",
default="detailed",
help="render the output in plain, tabular text",
)
parser.add_argument(
render_grp.add_argument(
"--markdown",
dest="markdown",
action="store_true",
dest="renderer",
action="store_const",
const="markdown",
default="detailed",
help="render the output in markdown format",
)
parser.add_argument(
Expand Down Expand Up @@ -351,22 +355,12 @@ def main():
run = Run(enabled, disabled, artifacts, console, debug)
run.invoke()

if args.json is True:
renderer = "json"
json = Json(console)
json.render(run)
elif args.plain is True:
renderer = "plain"
plain = Plain(console)
plain.render(run)
elif args.markdown is True:
renderer = "markdown"
markdown = Markdown(console)
markdown.render(run)
else:
renderer = "detailed"
detailed = Detailed(console)
detailed.render(run)
# Render the results
render_ext = loader.load_extension(
group="precli.renderers", name=args.renderer
)
renderer = render_ext(console)
renderer.render(run)

if file.name != sys.stdout.name:
console.print(f"Output written to file: {file.name}")
Expand Down
17 changes: 10 additions & 7 deletions precli/core/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
from importlib.metadata import entry_points


def load_parsers() -> dict:
parsers = {}
def load_extension(group: str, name: str = ""):
if not name:
extensions = {}

discovered_plugins = entry_points(group="precli.parsers")
for plugin in discovered_plugins:
parser = plugin.load()()
parsers[parser.lexer] = parser
for entry_point in entry_points(group=group):
extension = entry_point.load()()
extensions[entry_point.name] = extension

return parsers
return extensions
else:
(entry_point,) = entry_points(group=group, name=name)
return entry_point.load()
2 changes: 1 addition & 1 deletion precli/core/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

LOG = logging.getLogger(__name__)
PROGRESS_THRESHOLD = 50
parsers = loader.load_parsers()
parsers = loader.load_extension(group="precli.parsers")


def parse_file(
Expand Down
13 changes: 13 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ project_urls =
console_scripts =
precli = precli.cli.main:main

precli.renderers =
# precli/renderers/detailed.py
detailed = precli.renderers.detailed:Detailed

# precli/renderers/json.py
json = precli.renderers.json:Json

# precli/renderers/markdown.py
markdown = precli.renderers.markdown:Markdown

# precli/renderers/plain.py
plain = precli.renderers.plain:Plain

precli.parsers =
# precli/parsers/go.py
go = precli.parsers.go:Go
Expand Down