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

Add property for optional runner #913

Merged
merged 3 commits into from
Jun 14, 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
27 changes: 23 additions & 4 deletions nornir/core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Any, Callable, Dict, Generator, List, Optional, Type

from nornir.core.configuration import Config
from nornir.core.exceptions import PluginNotRegistered
from nornir.core.inventory import Inventory
from nornir.core.plugins.runners import RunnerPlugin
from nornir.core.processor import Processor, Processors
Expand Down Expand Up @@ -43,7 +44,7 @@ def __init__(
self.inventory = inventory
self.config = config or Config()
self.processors = processors or Processors()
self.runner = runner
self._runner = runner

def __enter__(self) -> "Nornir":
return self
Expand All @@ -61,14 +62,16 @@ def with_processors(self, processors: List[Processor]) -> "Nornir":
Given a list of Processor objects return a copy of the nornir object with the processors
assigned to the copy. The original object is left unmodified.
"""
return Nornir(**{**self.__dict__, **{"processors": Processors(processors)}})
return Nornir(
**{**self._clone_parameters(), **{"processors": Processors(processors)}}
)

def with_runner(self, runner: RunnerPlugin) -> "Nornir":
"""
Given a runner return a copy of the nornir object with the runner
assigned to the copy. The original object is left unmodified.
"""
return Nornir(**{**self.__dict__, **{"runner": runner}})
return Nornir(**{**self._clone_parameters(), **{"runner": runner}})

def filter(self, *args: Any, **kwargs: Any) -> "Nornir":
"""
Expand All @@ -77,7 +80,7 @@ def filter(self, *args: Any, **kwargs: Any) -> "Nornir":
Returns:
:obj:`Nornir`: A new object with same configuration as ``self`` but filtered inventory.
"""
b = Nornir(**self.__dict__)
b = Nornir(**self._clone_parameters())
b.inventory = self.inventory.filter(*args, **kwargs)
return b

Expand Down Expand Up @@ -165,6 +168,22 @@ def close_connections_task(task):

self.run(task=close_connections_task, on_good=on_good, on_failed=on_failed)

@property
def runner(self) -> RunnerPlugin:
if self._runner:
return self._runner

raise PluginNotRegistered("Runner plugin not registered")

def _clone_parameters(self) -> Dict[str, Any]:
return {
"data": self.data,
"inventory": self.inventory,
"config": self.config,
"processors": self.processors,
"runner": self._runner,
}

@classmethod
def get_validators(cls) -> Generator[Callable[["Nornir"], "Nornir"], None, None]:
yield cls.validate
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ warn_redundant_casts = True
[mypy-nornir.core]
disallow_untyped_defs = False
disallow_incomplete_defs = False
strict_optional = False

[mypy-tests.*]
ignore_errors = True