Skip to content

Commit

Permalink
fix and deprecate dict() function (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbarrosop committed Jan 24, 2019
1 parent 968806d commit 701136f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 18 deletions.
16 changes: 13 additions & 3 deletions nornir/core/deserializer/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ class Config:
class ConnectionOptions(BaseAttributes):
extras: Optional[Dict[str, Any]]

@classmethod
def serialize(cls, i: inventory.ConnectionOptions) -> "ConnectionOptions":
return ConnectionOptions(
hostname=i.hostname,
port=i.port,
username=i.username,
password=i.password,
platform=i.platform,
extras=i.extras,
)


class InventoryElement(BaseAttributes):
groups: List[str] = []
Expand Down Expand Up @@ -77,9 +88,8 @@ def serialize(cls, e: Union[inventory.Host, inventory.Group]) -> "InventoryEleme
for f in cls.__fields__:
d[f] = object.__getattribute__(e, f)
d["groups"] = list(d["groups"])

d["connection_options"] = {
k: {f: getattr(v, f) for f in v.__recursive_slots__()}
k: ConnectionOptions.serialize(v)
for k, v in d["connection_options"].items()
}
return InventoryElement(**d)
Expand All @@ -96,7 +106,7 @@ def serialize(cls, defaults: inventory.Defaults) -> "InventoryElement":
d[f] = getattr(defaults, f)

d["connection_options"] = {
k: {f: getattr(v, f) for f in v.__recursive_slots__()}
k: ConnectionOptions.serialize(v)
for k, v in d["connection_options"].items()
}
return Defaults(**d)
Expand Down
27 changes: 16 additions & 11 deletions nornir/core/inventory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import warnings
from collections import UserList
from typing import Any, Dict, List, Optional, Set, Union

from nornir.core import deserializer
from nornir.core.configuration import Config
from nornir.core.connections import ConnectionPlugin, Connections
from nornir.core.exceptions import ConnectionAlreadyOpen, ConnectionNotOpen
Expand All @@ -23,17 +25,14 @@ def __init__(
self.password = password
self.platform = platform

def __recursive_slots__(self):
s = self.__slots__
for b in self.__class__.__bases__:
if hasattr(b, "__recursive_slots__"):
s += b().__recursive_slots__()
elif hasattr(b, "__slots__"):
s += b.__slots__
return s

def dict(self):
return {k: object.__getattribute__(self, k) for k in self.__recursive_slots__()}
w = f"{self.dict.__qualname__} is deprecated, use nornir.core.deserializer instead"
warnings.warn(w)
return (
getattr(deserializer.inventory, self.__class__.__name__)
.serialize(self)
.dict()
)


class ConnectionOptions(BaseAttributes):
Expand Down Expand Up @@ -291,10 +290,16 @@ def get_connection(self, connection: str, configuration: Config) -> Any:
An already established connection
"""
if connection not in self.connections:
conn = self.get_connection_parameters(connection)
self.open_connection(
connection=connection,
configuration=configuration,
**self.get_connection_parameters(connection).dict(),
hostname=conn.hostname,
port=conn.port,
username=conn.username,
password=conn.password,
platform=conn.platform,
extras=conn.extras,
)
return self.connections[connection].connection

Expand Down
8 changes: 4 additions & 4 deletions tests/core/test_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def test_to_dict(self):
def test_get_connection_parameters(self):
inv = deserializer.Inventory.deserialize(**inv_dict)
p1 = inv.hosts["dev1.group_1"].get_connection_parameters("dummy")
assert p1.dict() == {
assert deserializer.ConnectionOptions.serialize(p1).dict() == {
"port": 22,
"hostname": "dummy_from_host",
"username": "root",
Expand All @@ -163,7 +163,7 @@ def test_get_connection_parameters(self):
"extras": {"blah": "from_host"},
}
p2 = inv.hosts["dev1.group_1"].get_connection_parameters("asd")
assert p2.dict() == {
assert deserializer.ConnectionOptions.serialize(p2).dict() == {
"port": 22,
"hostname": "dev1.group_1",
"username": "root",
Expand All @@ -172,7 +172,7 @@ def test_get_connection_parameters(self):
"extras": {},
}
p3 = inv.hosts["dev2.group_1"].get_connection_parameters("dummy")
assert p3.dict() == {
assert deserializer.ConnectionOptions.serialize(p3).dict() == {
"port": 22,
"hostname": "dummy_from_parent_group",
"username": "root",
Expand All @@ -181,7 +181,7 @@ def test_get_connection_parameters(self):
"extras": {"blah": "from_group"},
}
p4 = inv.hosts["dev3.group_2"].get_connection_parameters("dummy")
assert p4.dict() == {
assert deserializer.ConnectionOptions.serialize(p4).dict() == {
"port": 22,
"hostname": "dummy_from_defaults",
"username": "root",
Expand Down

0 comments on commit 701136f

Please sign in to comment.