Skip to content

Commit

Permalink
Refactor get_app (#2656)
Browse files Browse the repository at this point in the history
Change implementation of get_app to make it avoid initializing
the app more than once.
  • Loading branch information
ssbarnea authored Nov 21, 2022
1 parent 0a4053a commit 7be2420
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/ansiblelint/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ def main(argv: list[str] | None = None) -> int: # noqa: C901
if options.listrules or options.listtags:
return _do_list(rules)

app = get_app(offline=options.offline)
app = get_app()
if isinstance(options.tags, str):
options.tags = options.tags.split(",")
result = _get_matches(rules, options)
Expand Down
7 changes: 4 additions & 3 deletions src/ansiblelint/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,10 @@ def _sanitize_list_options(tag_list: list[str]) -> list[str]:
return sorted(set(tags))


@lru_cache(maxsize=1)
def get_app(offline: bool = False) -> App:
"""Return the application instance."""
@lru_cache
def get_app() -> App:
"""Return the application instance, caching the return value."""
offline = default_options.offline
app = App(options=default_options)
# Make linter use the cache dir from compat
default_options.cache_dir = app.runtime.cache_dir
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/rules/syntax_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def _get_ansible_syntax_check_matches(lintable: Lintable) -> list[MatchError]:
# To reduce noisy warnings like
# CryptographyDeprecationWarning: Blowfish has been deprecated
# https://github.com/paramiko/paramiko/issues/2038
env = get_app(offline=True).runtime.environ.copy()
env = get_app().runtime.environ.copy()
env["PYTHONWARNINGS"] = "ignore"

run = subprocess.run(
Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/testing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, collection: RulesCollection) -> None:
# Emulate command line execution initialization as without it Ansible module
# would be loaded with incomplete module/role/collection list.
if not self.app:
self.app = get_app(offline=True)
self.app = get_app()

self.collection = collection

Expand Down
2 changes: 1 addition & 1 deletion src/ansiblelint/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ def _rolepath(basedir: str, role: str) -> str | None:
path_dwim(basedir, os.path.join("..", role)),
]

for loc in get_app(offline=True).runtime.config.default_roles_path:
for loc in get_app().runtime.config.default_roles_path:
loc = os.path.expanduser(loc)
possible_paths.append(path_dwim(loc, role))

Expand Down

0 comments on commit 7be2420

Please sign in to comment.