From 3cf5c8a4e9ae9533975d3aa464fecee2a55c1655 Mon Sep 17 00:00:00 2001 From: Tiago Nobrega Date: Fri, 20 Oct 2023 11:59:06 -0300 Subject: [PATCH] refactor: drop _Dispatcher (#106) Update the minimum version of craft-cli to 2.3.0, in which the standard Dispatcher class has the ``parsed_args()`` method which we can use instead. --- craft_application/application.py | 20 +++++--------------- pyproject.toml | 2 +- tests/unit/test_application.py | 6 ++---- 3 files changed, 8 insertions(+), 20 deletions(-) diff --git a/craft_application/application.py b/craft_application/application.py index 7f9e21ef..5bc5c3e2 100644 --- a/craft_application/application.py +++ b/craft_application/application.py @@ -16,7 +16,6 @@ """Main application classes for a craft-application.""" from __future__ import annotations -import argparse import functools import importlib import os @@ -44,15 +43,6 @@ ) -class _Dispatcher(craft_cli.Dispatcher): - """Application command dispatcher.""" - - @property - def parsed_args(self) -> argparse.Namespace: - """The map of parsed command-line arguments.""" - return self._parsed_command_args or argparse.Namespace() - - @dataclass(frozen=True) class AppFeatures: """Specific features that can be enabled/disabled per-application.""" @@ -247,7 +237,7 @@ def run_managed(self, platform: str | None, build_for: str | None) -> None: def configure(self, global_args: dict[str, Any]) -> None: """Configure the application using any global arguments.""" - def _get_dispatcher(self) -> _Dispatcher: + def _get_dispatcher(self) -> craft_cli.Dispatcher: """Configure this application. Should be called by the run method. Side-effect: This method may exit the process. @@ -262,7 +252,7 @@ def _get_dispatcher(self) -> _Dispatcher: streaming_brief=True, ) - dispatcher = _Dispatcher( + dispatcher = craft_cli.Dispatcher( self.app.name, self.command_groups, summary=str(self.app.summary), @@ -327,11 +317,11 @@ def run(self) -> int: # noqa: PLR0912 (too many branches due to error handling) } ), ) - platform = getattr(dispatcher.parsed_args, "platform", None) - build_for = getattr(dispatcher.parsed_args, "build_for", None) + platform = getattr(dispatcher.parsed_args(), "platform", None) + build_for = getattr(dispatcher.parsed_args(), "build_for", None) self._configure_services(platform, build_for) - if not command.run_managed(dispatcher.parsed_args): + if not command.run_managed(dispatcher.parsed_args()): # command runs in the outer instance craft_cli.emit.debug(f"Running {self.app.name} {command.name} on host") if command.always_load_project: diff --git a/pyproject.toml b/pyproject.toml index fcecd636..bf2e2413 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ name = "craft-application" description = "A framework for *craft applications." dynamic = ["version", "readme"] dependencies = [ - "craft-cli>=2.0.0", + "craft-cli>=2.3.0", "craft-parts>=1.21.1", "craft-providers>=1.14.0,<2.0", "pydantic>=1.10,<2.0", diff --git a/tests/unit/test_application.py b/tests/unit/test_application.py index a73d41dd..2c02b104 100644 --- a/tests/unit/test_application.py +++ b/tests/unit/test_application.py @@ -80,10 +80,8 @@ def app(app_metadata, fake_services): @pytest.fixture() def mock_dispatcher(monkeypatch): - dispatcher = mock.Mock(spec_set=application._Dispatcher) - monkeypatch.setattr( - "craft_application.application._Dispatcher", mock.Mock(return_value=dispatcher) - ) + dispatcher = mock.Mock(spec_set=craft_cli.Dispatcher) + monkeypatch.setattr("craft_cli.Dispatcher", mock.Mock(return_value=dispatcher)) return dispatcher