Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarrosop committed Nov 20, 2018
1 parent 1f4f44b commit 1b3b3ba
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 56 deletions.
30 changes: 15 additions & 15 deletions nornir/core/filter.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
class F_OP_BASE(object):
def __init__(self, op1, op2):
def __init__(self, op1: "F_OP_BASE", op2: "F_OP_BASE") -> None:
self.op1 = op1
self.op2 = op2

def __and__(self, other):
def __and__(self, other: "F_OP_BASE") -> "F_OP_BASE":
return AND(self, other)

def __or__(self, other):
def __or__(self, other: "F_OP_BASE") -> "F_OP_BASE":
return OR(self, other)

def __repr__(self):
def __repr__(self) -> str:
return "( {} {} {} )".format(self.op1, self.__class__.__name__, self.op2)


class AND(F_OP_BASE):
def __call__(self, host):
return self.op1(host) and self.op2(host)
def __call__(self, obj: object) -> bool:
return self.op1(obj) and self.op2(obj)


class OR(F_OP_BASE):
def __call__(self, host):
return self.op1(host) or self.op2(host)
def __call__(self, obj: object) -> bool:
return self.op1(obj) or self.op2(obj)


class F(object):
def __init__(self, **kwargs):
self.filters = kwargs

def __call__(self, host):
def __call__(self, obj: object) -> bool:
return all(
F._verify_rules(host, k.split("__"), v) for k, v in self.filters.items()
F._verify_rules(obj, k.split("__"), v) for k, v in self.filters.items()
)

def __and__(self, other):
def __and__(self, other: "F") -> bool:
return AND(self, other)

def __or__(self, other):
def __or__(self, other: "F") -> bool:
return OR(self, other)

def __invert__(self):
def __invert__(self) -> bool:
return NOT_F(**self.filters)

def __repr__(self):
Expand Down Expand Up @@ -80,9 +80,9 @@ def _verify_rules(data, rule, value):


class NOT_F(F):
def __call__(self, host):
def __call__(self, obj):
return not any(
F._verify_rules(host, k.split("__"), v) for k, v in self.filters.items()
F._verify_rules(obj, k.split("__"), v) for k, v in self.filters.items()
)

def __invert__(self):
Expand Down
13 changes: 9 additions & 4 deletions nornir/core/state.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from typing import Any, Dict, Optional, Set


class GlobalState(object):
"""
This class is just a placeholder to share data amongst different
Expand All @@ -9,18 +12,20 @@ class GlobalState(object):

__slots__ = "dry_run", "failed_hosts"

def __init__(self, dry_run=None, failed_hosts=None):
def __init__(
self, dry_run: Optional[bool] = None, failed_hosts: Optional[Set[str]] = None
) -> None:
self.dry_run = dry_run
self.failed_hosts = failed_hosts or set()

def recover_host(self, host):
def recover_host(self, host: str) -> None:
"""Remove ``host`` from list of failed hosts."""
self.failed_hosts.discard(host)

def reset_failed_hosts(self):
def reset_failed_hosts(self) -> None:
"""Reset failed hosts and make all hosts available for future tasks."""
self.failed_hosts = set()

def to_dict(self):
def to_dict(self) -> Dict[str, Any]:
""" Return a dictionary representing the object. """
return self.__dict__
45 changes: 8 additions & 37 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,51 +27,22 @@ max-line-length = 100
#addopts = --cov=nornir --cov-report=term-missing -vs
python_paths = ./


[mypy]
# The mypy configurations: http://bit.ly/2zEl9WI
python_version = 3.6
warn_redundant_casts = True
warn_unused_configs = True
ignore_missing_imports = True

[mypy-nornir.core.configuration]
check_untyped_defs = True
disallow_any_generics = True
# Turn on the next flag once the whole codebase is annotated (Phase 2)
disallow_untyped_calls = True
strict_optional = True
warn_unused_ignores = True
ignore_errors = False

[mypy-nornir.core.connections]
check_untyped_defs = True
disallow_any_generics = True
# Turn on the next flag once the whole codebase is annotated (Phase 2)
# disallow_untyped_calls = True
strict_optional = True
warn_unused_ignores = True
ignore_errors = False

[mypy-nornir.core.deserializer.*]
check_untyped_defs = True
disallow_any_generics = True
# Turn on the next flag once the whole codebase is annotated (Phase 2)
# disallow_untyped_calls = True
strict_optional = True
warn_unused_ignores = True
disallow_untyped_defs = True
disallow_incomplete_defs = True
disallow_untyped_decorators = True
ignore_errors = False

[mypy-nornir.plugins.*]
check_untyped_defs = True
disallow_any_generics = True
# Turn on the next flag once the whole codebase is annotated (Phase 2)
# disallow_untyped_calls = True
ignore_missing_imports = True
strict_optional = True
warn_unused_ignores = True
ignore_errors = False

[mypy-nornir.*]
ignore_errors = True
warn_redundant_casts = True
warn_return_any = True
warn_unused_configs = True

[mypy-tests.*]
ignore_errors = True

0 comments on commit 1b3b3ba

Please sign in to comment.