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

Refactor code to avoid use of rich library #4396

Merged
merged 1 commit into from
Dec 4, 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
5 changes: 1 addition & 4 deletions .config/constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,8 @@ linkchecker==10.5.0 # via mkdocs-ansible
markdown==3.7 # via markdown-include, mkdocs, mkdocs-autorefs, mkdocs-htmlproofer-plugin, mkdocs-material, mkdocstrings, pymdown-extensions
markdown-exec==1.9.3 # via mkdocs-ansible
markdown-include==0.8.1 # via mkdocs-ansible
markdown-it-py==3.0.0 # via rich
markupsafe==3.0.2 # via jinja2, mkdocs, mkdocs-autorefs, mkdocstrings
mccabe==0.7.0 # via pylint
mdurl==0.1.2 # via markdown-it-py
mergedeep==1.3.4 # via mkdocs, mkdocs-get-deps
mkdocs==1.6.1 # via mkdocs-ansible, mkdocs-autorefs, mkdocs-gen-files, mkdocs-htmlproofer-plugin, mkdocs-macros-plugin, mkdocs-material, mkdocs-minify-plugin, mkdocs-monorepo-plugin, mkdocstrings
mkdocs-ansible==24.12.0 # via ansible-lint (pyproject.toml)
Expand All @@ -76,7 +74,7 @@ platformdirs==4.3.6 # via black, mkdocs-get-deps, mkdocstrings, pylint, to
pluggy==1.5.0 # via pytest, tox
psutil==6.1.0 # via pytest-xdist, ansible-lint (pyproject.toml)
pycparser==2.22 # via cffi
pygments==2.18.0 # via mkdocs-material, rich
pygments==2.18.0 # via mkdocs-material
pylint==3.3.2 # via ansible-lint (pyproject.toml)
pymdown-extensions==10.12 # via markdown-exec, mkdocs-ansible, mkdocs-material, mkdocstrings
pyproject-api==1.8.0 # via tox
Expand All @@ -92,7 +90,6 @@ pyyaml-env-tag==0.1 # via mkdocs
referencing==0.35.1 # via jsonschema, jsonschema-specifications, types-jsonschema
regex==2024.11.6 # via mkdocs-material
requests==2.32.3 # via linkchecker, mkdocs-htmlproofer-plugin, mkdocs-material
rich==13.9.4 # via ansible-lint (pyproject.toml)
rpds-py==0.22.1 # via jsonschema, referencing
ruamel-yaml==0.18.6 # via ansible-lint (pyproject.toml)
ruamel-yaml-clib==0.2.12 # via ruamel-yaml, ansible-lint (pyproject.toml)
Expand Down
4 changes: 0 additions & 4 deletions .config/requirements-lock.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,14 @@ importlib-metadata==8.5.0 # via ansible-lint (pyproject.toml)
jinja2==3.1.4 # via ansible-core
jsonschema==4.23.0 # via ansible-compat, ansible-lint (pyproject.toml)
jsonschema-specifications==2024.10.1 # via jsonschema
markdown-it-py==3.0.0 # via rich
markupsafe==3.0.2 # via jinja2
mdurl==0.1.2 # via markdown-it-py
mypy-extensions==1.0.0 # via black
packaging==24.2 # via ansible-compat, ansible-core, black, ansible-lint (pyproject.toml)
pathspec==0.12.1 # via black, yamllint, ansible-lint (pyproject.toml)
platformdirs==4.3.6 # via black
pycparser==2.22 # via cffi
pygments==2.18.0 # via rich
pyyaml==6.0.2 # via ansible-compat, ansible-core, yamllint, ansible-lint (pyproject.toml)
referencing==0.35.1 # via jsonschema, jsonschema-specifications
rich==13.9.4 # via ansible-lint (pyproject.toml)
rpds-py==0.22.1 # via jsonschema, referencing
ruamel-yaml==0.18.6 # via ansible-lint (pyproject.toml)
ruamel-yaml-clib==0.2.12 # via ruamel-yaml
Expand Down
1 change: 0 additions & 1 deletion .config/requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ jsonschema>=4.10.0 # MIT, version needed for improved errors
packaging>=21.3 # Apache-2.0,BSD-2-Clause
pathspec>=0.10.3 # Mozilla Public License 2.0 (MPL 2.0)
pyyaml>=5.4.1 # MIT (centos 9 has 5.3.1)
rich>=12.0.0 # MIT
ruamel.yaml>=0.18.5 # MIT
subprocess-tee>=0.4.1 # MIT, used by ansible-compat
yamllint >= 1.30.0 # GPLv3
Expand Down
13 changes: 13 additions & 0 deletions .config/vulture_whitelist.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# type: ignore
_.configured # unused attribute (src/ansiblelint/__main__.py:140)
configured # unused variable (src/ansiblelint/config.py:132)
_.keep_trailing_newline # unused attribute (src/ansiblelint/rules/jinja.py:280)
Expand All @@ -13,3 +14,15 @@
_.compact_seq_map # unused attribute (src/ansiblelint/yaml_utils.py:925)
_.Constructor # unused attribute (src/ansiblelint/yaml_utils.py:946)
_.preserve_quotes # unused attribute (src/ansiblelint/yaml_utils.py:956)
_.BLACK # unused variable (src/ansiblelint/output.py:172)
_.YELLOW # unused variable (src/ansiblelint/output.py:175)
_.CYAN # unused variable (src/ansiblelint/output.py:178)
_.WHITE # unused variable (src/ansiblelint/output.py:179)
_.GREY # unused variable (src/ansiblelint/output.py:180)
_.BRIGHT_RED # unused variable (src/ansiblelint/output.py:181)
_.BRIGHT_GREEN # unused variable (src/ansiblelint/output.py:182)
_.BRIGHT_YELLOW # unused variable (src/ansiblelint/output.py:183)
_.BRIGHT_BLUE # unused variable (src/ansiblelint/output.py:184)
_.BRIGHT_MAGENTA # unused variable (src/ansiblelint/output.py:185)
_.BRIGHT_CYAN # unused variable (src/ansiblelint/output.py:186)
_.BRIGHT_WHITE # unused variable (src/ansiblelint/output.py:187)
1 change: 0 additions & 1 deletion .github/lower-constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ jsonschema==4.10.0 # MIT, version needed for improved errors
packaging==21.3
pathspec==0.10.3
pyyaml==5.4.1
rich==12.0.0
ruamel.yaml==0.18.5 # MIT
subprocess-tee==0.4.1 # MIT, used by ansible-compat
# https://packages.ubuntu.com/noble/python3-wcmatch
Expand Down
3 changes: 0 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ repos:
- pytest-mock
- pytest>=7.2.2
- pip>=22.3.1
- rich>=13.2.0
- ruamel-yaml-clib>=0.2.8
- ruamel-yaml>=0.18.6
- subprocess-tee
Expand Down Expand Up @@ -195,7 +194,6 @@ repos:
- pip>=22.3.1
- pytest-mock
- pytest>=7.2.2
- rich>=13.2.0
- ruamel-yaml-clib>=0.2.8
- ruamel-yaml>=0.18.6
- subprocess-tee
Expand All @@ -222,7 +220,6 @@ repos:
- pytest-mock
- pytest>=7.2.2
- pyyaml
- rich>=13.2.0
- ruamel-yaml-clib>=0.2.7
- ruamel-yaml>=0.18.2
- setuptools # needed for pkg_resources import
Expand Down
4 changes: 4 additions & 0 deletions .sonarlint/connectedMode.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"sonarCloudOrganization": "ansible",
"projectKey": "ansible_ansible-lint"
}
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,9 @@
"evenBetterToml.formatter.arrayTrailingComma": true,
"[toml]": {
"editor.defaultFormatter": "panekj.even-betterer-toml"
},
"sonarlint.connectedMode.project": {
"connectionId": "ansible",
"projectKey": "ansible_ansible-lint"
}
}
1 change: 1 addition & 0 deletions cspell.config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dictionaries:
- bash
- words
- python
enabled: true
ignorePaths:
- cspell.config.yaml
# The requirements file
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ exclude = [
ignore_names = [
"_ANSIBLE_ARGS",
"__line__",
"__rich_console__",
"fixture_*",
"pytest_addoption",
"pytest_collection_modifyitems",
Expand Down
61 changes: 7 additions & 54 deletions src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,10 @@
import site
import sys
from pathlib import Path
from typing import TYPE_CHECKING, Any, TextIO
from typing import TYPE_CHECKING

from ansible_compat.prerun import get_cache_dir
from filelock import BaseFileLock, FileLock, Timeout
from rich.markdown import Markdown
from rich.markup import escape

from ansiblelint.constants import RC, SKIP_SCHEMA_UPDATE

Expand All @@ -61,10 +59,10 @@
from ansiblelint.loaders import load_ignore_txt
from ansiblelint.output import (
console,
console_options,
console_stderr,
reconfigure,
render_yaml,
should_do_markup,
)
from ansiblelint.runner import get_matches
from ansiblelint.skip_utils import normalize_tag
Expand All @@ -86,7 +84,7 @@ class LintLogHandler(logging.Handler):
def emit(self, record: logging.LogRecord) -> None:
try:
msg = self.format(record)
console_stderr.print(f"[dim]{msg}[/dim]", highlight=False)
console_stderr.print(f"[dim]{msg}[/]")
except RecursionError: # See issue 36272
raise
except Exception: # pylint: disable=broad-exception-caught # noqa: BLE001
Expand Down Expand Up @@ -170,7 +168,6 @@ def _do_list(rules: RulesCollection) -> int:
if options.list_rules:
console.print(
rules_as_str(rules),
highlight=False,
)
return 0

Expand Down Expand Up @@ -282,16 +279,15 @@ def main(argv: list[str] | None = None) -> int:
argv = sys.argv
cache_dir_lock = initialize_options(argv[1:])

console_options["force_terminal"] = options.colored
reconfigure(console_options)
reconfigure(colored=options.colored)

if options.version:
deps = get_deps_versions()
msg = f"ansible-lint [repr.number]{__version__}[/] using[dim]"
for k, v in deps.items():
msg += f" {escape(k)}:[repr.number]{v}[/]"
msg += f" {k}:[repr.number]{v}[/]"
msg += "[/]"
console.print(msg, markup=True, highlight=False)
console.print(msg)
msg = get_version_warning()
if msg:
console.print(msg)
Expand Down Expand Up @@ -334,7 +330,7 @@ def main(argv: list[str] | None = None) -> int:
if options.list_profiles:
from ansiblelint.generate_docs import profiles_as_md

console.print(Markdown(profiles_as_md()))
profiles_as_md().display()
return 0

app = get_app(
Expand Down Expand Up @@ -478,48 +474,5 @@ def path_inject(own_location: str = "") -> None:
raise RuntimeError(msg)


# Based on Ansible implementation
def to_bool(value: Any) -> bool: # pragma: no cover
"""Return a bool for the arg."""
if value is None or isinstance(value, bool):
return bool(value)
if isinstance(value, str):
value = value.lower()
return value in ("yes", "on", "1", "true", 1)


def should_do_markup(stream: TextIO = sys.stdout) -> bool: # pragma: no cover
"""Decide about use of ANSI colors."""
py_colors = None

# https://xkcd.com/927/
for env_var in ["PY_COLORS", "CLICOLOR", "FORCE_COLOR", "ANSIBLE_FORCE_COLOR"]:
value = os.environ.get(env_var, None)
if value is not None:
py_colors = to_bool(value)
break

# If deliberately disabled colors
if os.environ.get("NO_COLOR", None):
return False

# User configuration requested colors
if py_colors is not None:
return to_bool(py_colors)

term = os.environ.get("TERM", "")
if "xterm" in term:
return True

if term == "dumb":
return False

# Use tty detection logic as last resort because there are numerous
# factors that can make isatty return a misleading value, including:
# - stdin.isatty() is the only one returning true, even on a real terminal
# - stderr returning false if user user uses a error stream coloring solution
return stream.isatty()


if __name__ == "__main__":
_run_cli_entrypoint()
36 changes: 6 additions & 30 deletions src/ansiblelint/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
from typing import TYPE_CHECKING, Any

from ansible_compat.runtime import Runtime
from rich.markup import escape
from rich.table import Table

from ansiblelint import formatters
from ansiblelint._mockings import _perform_mockings
Expand Down Expand Up @@ -82,8 +80,6 @@
# then print only the matches in JSON
console.print(
self.formatter.format_result(matches),
markup=False,
highlight=False,
)
return

Expand All @@ -97,16 +93,15 @@
)
for match in ignored_matches:
if match.ignored:
# highlight must be off or apostrophes may produce unexpected results
console.print(self.formatter.apply(match), highlight=False)
console.print(self.formatter.apply(match))

Check warning on line 96 in src/ansiblelint/app.py

View check run for this annotation

Codecov / codecov/patch

src/ansiblelint/app.py#L96

Added line #L96 was not covered by tests
if fatal_matches:
_logger.warning(
"Listing %s violation(s) that are fatal",
len(fatal_matches),
)
for match in fatal_matches:
if not match.ignored:
console.print(self.formatter.apply(match), highlight=False)
console.print(self.formatter.apply(match))

# If run under GitHub Actions we also want to emit output recognized by it.
if os.getenv("GITHUB_ACTIONS") == "true" and os.getenv("GITHUB_WORKFLOW"):
Expand All @@ -117,8 +112,6 @@
for match in itertools.chain(fatal_matches, ignored_matches):
console_stderr.print(
formatter.apply(match),
markup=False,
highlight=False,
)

# If sarif_file is set, we also dump the results to a sarif file.
Expand Down Expand Up @@ -316,33 +309,16 @@

stars = ""
if summary.tag_stats:
table = Table(
title="Rule Violation Summary",
collapse_padding=True,
box=None,
show_lines=False,
)
table.add_column("count", justify="right")
table.add_column("tag")
table.add_column("profile")
table.add_column("rule associated tags")
table = "# Rule Violation Summary\n\n"
for tag, stats in summary.tag_stats.items():
table.add_row(
str(stats.count),
f"[link={RULE_DOC_URL}{tag.split('[')[0]}]{escape(tag)}[/link]",
stats.profile,
f"{', '.join(stats.associated_tags)}{' (warning)' if stats.warning else ''}",
style="yellow" if stats.warning else "red",
)
# rate stars for the top 5 profiles (min would not get
table += f"{stats.count:3} [link={RULE_DOC_URL}]{tag.split('[')[0]}[/link] [dim]profile:{profile} tags:{','.join(stats.associated_tags)}[/]\n"
rating = 5 - (len(PROFILES.keys()) - passed_profile_count)
if 0 < rating < 6:
stars = f" Rating: {rating}/5 star"

console_stderr.print(table)
console_stderr.print()
console.print(table, file=sys.stderr)

msg = "[green]Passed[/]" if is_success else "[red][bold]Failed[/][/]"
msg = "[success]Passed[/]" if is_success else "[failed][bold]Failed[/][/]"

msg += f": {summary.failures} failure(s), {summary.warnings} warning(s)"
if summary.fixed:
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ def get_config(arguments: list[str]) -> Options:
log_entries.append(
(
logging.INFO,
f"Identified [filename]{project_dir}[/] as project root due [bold]{method}[/].",
f"Identified [repr.path]{project_dir}[/] as project root due [bold]{method}[/].",
),
)

Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def get_version_warning() -> str:
if current_version > new_version:
msg = "[dim]You are using a pre-release version of ansible-lint.[/]"
elif current_version < new_version:
msg = f"""[warning]A new release of ansible-lint is available: [red]{current_version}[/] → [green][link={html_url}]{new_version}[/][/][/]"""
msg = f"""[warning]A new release of ansible-lint is available: [warning]{current_version}[/] → [success][link={html_url}]{new_version}[/link][/][/]"""
msg += f" Upgrade by running: [info]{pip}[/]"

return msg
Loading
Loading