Skip to content

Commit

Permalink
refactor: drop _Dispatcher
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tigarmo committed Oct 19, 2023
1 parent 8c6c047 commit 0f7d8f2
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 20 deletions.
20 changes: 5 additions & 15 deletions craft_application/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
"""Main application classes for a craft-application."""
from __future__ import annotations

import argparse
import functools
import importlib
import os
Expand Down Expand Up @@ -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."""
Expand Down Expand Up @@ -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.
Expand All @@ -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),
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 2 additions & 4 deletions tests/unit/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down

0 comments on commit 0f7d8f2

Please sign in to comment.