Skip to content

Commit a035b6f

Browse files
committed
Use cached_property from functools
Now we have a minimum supported python version which has functools.cached_property
1 parent df54994 commit a035b6f

File tree

10 files changed

+26
-54
lines changed

10 files changed

+26
-54
lines changed

testinfra/backend/paramiko.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
)
2323
)
2424

25+
import functools
26+
2527
import paramiko.ssh_exception
2628

2729
from testinfra.backend import base
28-
from testinfra.utils import cached_property
2930

3031

3132
class IgnorePolicy(paramiko.MissingHostKeyPolicy):
@@ -83,7 +84,7 @@ def _load_ssh_config(self, client, cfg, ssh_config, ssh_config_dir="~/.ssh"):
8384
new_ssh_config.parse(f)
8485
self._load_ssh_config(client, cfg, new_ssh_config, ssh_config_dir)
8586

86-
@cached_property
87+
@functools.cached_property
8788
def client(self):
8889
client = paramiko.SSHClient()
8990
client.set_missing_host_key_policy(paramiko.WarningPolicy())

testinfra/host.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212

13+
import functools
1314
import os
1415

1516
import testinfra.backend
1617
import testinfra.modules
17-
from testinfra.utils import cached_property
1818

1919

2020
class Host:
@@ -28,7 +28,7 @@ def __init__(self, backend):
2828
def __repr__(self):
2929
return "<testinfra.host.Host {}>".format(self.backend.get_pytest_id())
3030

31-
@cached_property
31+
@functools.cached_property
3232
def has_command_v(self):
3333
"""Return True if `command -v` is available"""
3434
return self.run("command -v command").rc == 0

testinfra/modules/blockdevice.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212

13+
import functools
14+
1315
from testinfra.modules.base import Module
14-
from testinfra.utils import cached_property
1516

1617

1718
class BlockDevice(Module):
@@ -124,7 +125,7 @@ def __repr__(self):
124125

125126

126127
class LinuxBlockDevice(BlockDevice):
127-
@cached_property
128+
@functools.cached_property
128129
def _data(self):
129130
header = ["RO", "RA", "SSZ", "BSZ", "StartSec", "Size", "Device"]
130131
command = "blockdev --report %s"

testinfra/modules/interface.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212

13+
import functools
1314
import json
1415
import re
1516

1617
from testinfra.modules.base import Module
17-
from testinfra.utils import cached_property
1818

1919

2020
class Interface(Module):
@@ -107,7 +107,7 @@ def default(cls, family=None):
107107

108108

109109
class LinuxInterface(Interface):
110-
@cached_property
110+
@functools.cached_property
111111
def _ip(self):
112112
ip_cmd = self.find_command("ip")
113113
if self.family is not None:

testinfra/modules/service.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212

13+
import functools
14+
1315
from testinfra.modules.base import Module
14-
from testinfra.utils import cached_property
1516

1617

1718
class Service(Module):
@@ -60,7 +61,7 @@ def is_masked(self):
6061
"""
6162
raise NotImplementedError
6263

63-
@cached_property
64+
@functools.cached_property
6465
def systemd_properties(self):
6566
"""Properties of the service (unit).
6667
@@ -112,7 +113,7 @@ def __repr__(self):
112113

113114

114115
class SysvService(Service):
115-
@cached_property
116+
@functools.cached_property
116117
def _service_command(self):
117118
return self.find_command("service")
118119

@@ -181,7 +182,7 @@ def is_masked(self):
181182
cmd = self.run_test("systemctl is-enabled %s", self.name)
182183
return cmd.stdout.strip() == "masked"
183184

184-
@cached_property
185+
@functools.cached_property
185186
def systemd_properties(self):
186187
out = self.check_output("systemctl show %s", self.name)
187188
out_d = {}
@@ -221,7 +222,7 @@ def is_running(self):
221222

222223

223224
class OpenRCService(SysvService):
224-
@cached_property
225+
@functools.cached_property
225226
def _service_command(self):
226227
return self.find_command("rc-service")
227228

testinfra/modules/socket.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212

13+
import functools
1314
import socket
1415
from typing import List, Optional, Tuple
1516

1617
from testinfra.modules.base import Module
17-
from testinfra.utils import cached_property
1818

1919

2020
def parse_socketspec(socketspec):
21-
2221
protocol, address = socketspec.split("://", 1)
2322

2423
if protocol not in ("udp", "tcp", "unix"):
@@ -314,7 +313,7 @@ def _iter_sockets(self, listening):
314313

315314

316315
class BSDSocket(Socket):
317-
@cached_property
316+
@functools.cached_property
318317
def _command(self):
319318
return self.find_command("netstat")
320319

@@ -333,7 +332,6 @@ def _iter_sockets(self, listening):
333332
# FreeBSD: tcp4/tcp6
334333
# OpeNBSD/NetBSD: tcp/tcp6
335334
if splitted[0] in ("tcp", "udp", "udp4", "tcp4", "tcp6", "udp6"):
336-
337335
address = splitted[3]
338336
if address == "*.*":
339337
# On OpenBSD 6.3 (issue #338)

testinfra/modules/sysctl.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212

13+
import functools
14+
1315
from testinfra.modules.base import InstanceModule
14-
from testinfra.utils import cached_property
1516

1617

1718
class Sysctl(InstanceModule):
@@ -23,7 +24,7 @@ class Sysctl(InstanceModule):
2324
20
2425
"""
2526

26-
@cached_property
27+
@functools.cached_property
2728
def _sysctl_command(self):
2829
return self.find_command("sysctl")
2930

testinfra/modules/systeminfo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
# See the License for the specific language governing permissions and
1111
# limitations under the License.
1212

13+
import functools
1314
import re
1415

1516
from testinfra.modules.base import InstanceModule
16-
from testinfra.utils import cached_property
1717

1818

1919
class SystemInfo(InstanceModule):
2020
"""Return system information"""
2121

22-
@cached_property
22+
@functools.cached_property
2323
def sysinfo(self):
2424
sysinfo = {
2525
"type": None,

testinfra/utils/__init__.py

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +0,0 @@
1-
# Licensed under the Apache License, Version 2.0 (the "License");
2-
# you may not use this file except in compliance with the License.
3-
# You may obtain a copy of the License at
4-
#
5-
# http://www.apache.org/licenses/LICENSE-2.0
6-
#
7-
# Unless required by applicable law or agreed to in writing, software
8-
# distributed under the License is distributed on an "AS IS" BASIS,
9-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10-
# See the License for the specific language governing permissions and
11-
# limitations under the License.
12-
13-
14-
class cached_property:
15-
"""A cached property computed only once per instance
16-
17-
A property that is only computed once per instance and then replaces itself
18-
with an ordinary attribute. Deleting the attribute resets the property.
19-
Source: https://github.com/bottlepy/bottle/commit/fa7733e075da0d790d809aa3d2f53071897e6f76
20-
""" # noqa
21-
22-
def __init__(self, func):
23-
self.__doc__ = func.__doc__
24-
self.func = func
25-
26-
def __get__(self, obj, cls):
27-
if obj is None:
28-
return self
29-
value = obj.__dict__[self.func.__name__] = self.func(obj)
30-
return value

testinfra/utils/ansible_runner.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@
1212

1313
import configparser
1414
import fnmatch
15+
import functools
1516
import ipaddress
1617
import json
1718
import os
1819
import tempfile
1920
from typing import Any, Dict, List, Optional, Union
2021

2122
import testinfra
22-
from testinfra.utils import cached_property
2323

2424
__all__ = ["AnsibleRunner"]
2525

@@ -270,11 +270,11 @@ def get_hosts(self, pattern="all"):
270270
result.add(host)
271271
return sorted(result)
272272

273-
@cached_property
273+
@functools.cached_property
274274
def inventory(self):
275275
return get_ansible_inventory(self.ansible_config, self.inventory_file)
276276

277-
@cached_property
277+
@functools.cached_property
278278
def ansible_config(self):
279279
return get_ansible_config()
280280

0 commit comments

Comments
 (0)