[elbe-devel] [PATCH] elbepack: soapclient: automatically connect on service usage
Thomas Weißschuh
thomas.weissschuh at linutronix.de
Mon Aug 26 11:24:35 CEST 2024
Establish the connection when the service is used the first time,
instead of forcing the users to call .connect() manually first.
This also fixes the misleading error
'AttributeError: 'ElbeSoapClient' object has no attribute 'service'
which would happen in this case as .service is only set in .connect().
Keep the .connect() method so users still can force a connection at a
specific point.
Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
elbepack/soapclient.py | 18 +++++++++++-------
1 file changed, 11 insertions(+), 7 deletions(-)
diff --git a/elbepack/soapclient.py b/elbepack/soapclient.py
index 8b8a685e2c1b..af5b302464c2 100644
--- a/elbepack/soapclient.py
+++ b/elbepack/soapclient.py
@@ -5,6 +5,7 @@
import binascii
import fnmatch
+import functools
import logging
import os
import socket
@@ -47,7 +48,8 @@ class ElbeSoapClient:
self.host = host
self.port = port
- def connect(self):
+ @functools.cached_property
+ def service(self):
control = None
current_retries = 0
@@ -61,14 +63,16 @@ class ElbeSoapClient:
raise
time.sleep(1)
- # Make sure, that client.service still maps
- # to the service object.
- self.service = control.service
-
- ElbeVersionMismatch.check(elbe_version, self.service.get_version())
+ ElbeVersionMismatch.check(elbe_version, control.service.get_version())
# We have a Connection, now login
- self.service.login(self._user, self._passwd)
+ control.service.login(self._user, self._passwd)
+
+ return control.service
+
+ def connect(self):
+ # Make the implicit connection explicit
+ self.service
@classmethod
def from_args(cls, args):
---
base-commit: 92c2ef338b89517a5a1306aeb8021cc60c5dbf2f
change-id: 20240826-soapclient-automatic-service-d4f3be2709dd
Best regards,
--
Thomas Weißschuh <thomas.weissschuh at linutronix.de>
More information about the elbe-devel
mailing list