Python code modernization, 4/n (#1162)

* Address attribute-defined-outside-init.

* Address broad-exception-raised.

* Address broad-exception-caught.

* Address consider-iterating-dictionary.

* Address consider-using-dict-comprehension.

* Address consider-using-f-string.

* Address consider-using-in.

* Address consider-using-max-builtin.

* Address some consider-using-with.

* Address invalid-name.

* Address keyword-arg-before-vararg.

* Address line-too-long.

* Address no-else-continue.

* Address no-else-raise.

* Address no-else-return.

* Remove broken dead code.

* Make consider-using-f-string changes compatible with older Python versions.

* Python 3.11 and earlier apparently do not like multi-line f-strings.
This commit is contained in:
Felix Fontein
2025-10-11 23:06:50 +02:00
committed by GitHub
parent 33c8a49191
commit cad22de628
59 changed files with 556 additions and 630 deletions
+11 -11
View File
@@ -42,7 +42,7 @@ class Context:
description=None,
):
if not name:
raise Exception("Name not provided")
raise ValueError("Name not provided")
self.name = name
self.context_type = None
self.orchestrator = orchestrator
@@ -136,7 +136,7 @@ class Context:
metadata = json.load(f)
except (OSError, KeyError, ValueError) as e:
# unknown format
raise Exception(
raise RuntimeError(
f"Detected corrupted meta file for context {name} : {e}"
) from e
@@ -157,7 +157,7 @@ class Context:
def _load_certs(self):
certs = {}
tls_dir = get_tls_dir(self.name)
for endpoint in self.endpoints.keys():
for endpoint in self.endpoints:
if not os.path.isdir(os.path.join(tls_dir, endpoint)):
continue
ca_cert = None
@@ -238,11 +238,11 @@ class Context:
return self.context_type is None
@property
def Name(self):
def Name(self): # pylint: disable=invalid-name
return self.name
@property
def Host(self):
def Host(self): # pylint: disable=invalid-name
if not self.orchestrator or self.orchestrator == "swarm":
endpoint = self.endpoints.get("docker", None)
if endpoint:
@@ -252,27 +252,27 @@ class Context:
return self.endpoints[self.orchestrator].get("Host", None)
@property
def Orchestrator(self):
def Orchestrator(self): # pylint: disable=invalid-name
return self.orchestrator
@property
def Metadata(self):
def Metadata(self): # pylint: disable=invalid-name
meta = {}
if self.orchestrator:
meta = {"StackOrchestrator": self.orchestrator}
return {"Name": self.name, "Metadata": meta, "Endpoints": self.endpoints}
@property
def TLSConfig(self):
def TLSConfig(self): # pylint: disable=invalid-name
key = self.orchestrator
if not key or key == "swarm":
key = "docker"
if key in self.tls_cfg.keys():
if key in self.tls_cfg:
return self.tls_cfg[key]
return None
@property
def TLSMaterial(self):
def TLSMaterial(self): # pylint: disable=invalid-name
certs = {}
for endpoint, tls in self.tls_cfg.items():
cert, key = tls.cert
@@ -280,5 +280,5 @@ class Context:
return {"TLSMaterial": certs}
@property
def Storage(self):
def Storage(self): # pylint: disable=invalid-name
return {"Storage": {"MetadataPath": self.meta_path, "TLSPath": self.tls_path}}