diff libervia/backend/tools/common/async_process.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 601e72332907
children
line wrap: on
line diff
--- a/libervia/backend/tools/common/async_process.py	Tue Jun 18 12:06:45 2024 +0200
+++ b/libervia/backend/tools/common/async_process.py	Wed Jun 19 18:44:57 2024 +0200
@@ -26,11 +26,13 @@
 from libervia.backend.core.i18n import _
 from libervia.backend.core import exceptions
 from libervia.backend.core.log import getLogger
+
 log = getLogger(__name__)
 
 
 class CommandProtocol(protocol.ProcessProtocol):
     """handle an external command"""
+
     # name of the command (unicode)
     name = None
     # full path to the command (bytes)
@@ -47,8 +49,8 @@
         self._deferred = deferred
         self.data = []
         self.err_data = []
-        self.cmd_args: list[str]|None = None
-        self.cmd_kwargs: dict[str, Any]|None = None
+        self.cmd_args: list[str] | None = None
+        self.cmd_kwargs: dict[str, Any] | None = None
 
     @property
     def command_name(self):
@@ -56,10 +58,11 @@
         if self.name is not None:
             return self.name
         elif self.command is not None:
-            return os.path.splitext(os.path.basename(self.command))[0].decode('utf-8',
-                                                                              'ignore')
+            return os.path.splitext(os.path.basename(self.command))[0].decode(
+                "utf-8", "ignore"
+            )
         else:
-            return ''
+            return ""
 
     def connectionMade(self):
         if self._stdin is not None:
@@ -68,48 +71,45 @@
 
     def outReceived(self, data):
         if self.log:
-            log.info(data.decode('utf-8', 'replace'))
+            log.info(data.decode("utf-8", "replace"))
         self.data.append(data)
 
     def errReceived(self, data):
         if self.log:
-            log.warning(data.decode('utf-8', 'replace'))
+            log.warning(data.decode("utf-8", "replace"))
         self.err_data.append(data)
 
     def processEnded(self, reason):
-        data = b''.join(self.data)
-        if (reason.value.exitCode == 0):
-            log.debug(f'{self.command_name!r} command succeed')
+        data = b"".join(self.data)
+        if reason.value.exitCode == 0:
+            log.debug(f"{self.command_name!r} command succeed")
             # we don't use "replace" on purpose, we want an exception if decoding
             # is not working properly
             self._deferred.callback(data)
         else:
-            err_data = b''.join(self.err_data)
+            err_data = b"".join(self.err_data)
 
             assert self.cmd_args is not None
             assert self.cmd_kwargs is not None
-            msg = (
-                _(
-                    "Can't complete {name} command (error code: {code}):\n"
-                    "Executed command: {command}\n"
-                    "Keyword arguments:\n"
-                    "{command_kw}\n\n"
-                    "stderr:\n{stderr}\n{stdout}\n"
-                )
-                .format(
-                    name = self.command_name,
-                    code = reason.value.exitCode,
-                    command = " ".join(self.cmd_args),
-                    command_kw = "\n".join(
-                        f"    - {k} = {v!r}" for k,v in self.cmd_kwargs.items()
-                    ),
-                    stderr= err_data.decode(errors='replace'),
-                    stdout = "stdout: " + data.decode(errors='replace')
-                    if data else '',
-                )
+            msg = _(
+                "Can't complete {name} command (error code: {code}):\n"
+                "Executed command: {command}\n"
+                "Keyword arguments:\n"
+                "{command_kw}\n\n"
+                "stderr:\n{stderr}\n{stdout}\n"
+            ).format(
+                name=self.command_name,
+                code=reason.value.exitCode,
+                command=" ".join(self.cmd_args),
+                command_kw="\n".join(
+                    f"    - {k} = {v!r}" for k, v in self.cmd_kwargs.items()
+                ),
+                stderr=err_data.decode(errors="replace"),
+                stdout="stdout: " + data.decode(errors="replace") if data else "",
             )
-            self._deferred.errback(Failure(exceptions.CommandException(
-                msg, data, err_data)))
+            self._deferred.errback(
+                Failure(exceptions.CommandException(msg, data, err_data))
+            )
 
     @classmethod
     def run(cls, *args, **kwargs):
@@ -127,10 +127,10 @@
             stdin and stdout will be given as arguments
 
         """
-        stdin = kwargs.pop('stdin', None)
+        stdin = kwargs.pop("stdin", None)
         if stdin is not None:
-            stdin = stdin.encode('utf-8')
-        verbose = kwargs.pop('verbose', False)
+            stdin = stdin.encode("utf-8")
+        verbose = kwargs.pop("verbose", False)
         args = list(args)
         d = defer.Deferred()
         prot = cls(d, stdin=stdin)
@@ -140,7 +140,8 @@
             if not args:
                 raise ValueError(
                     "You must either specify cls.command or use a full path to command "
-                    "to execute as first argument")
+                    "to execute as first argument"
+                )
             command = args.pop(0)
             if prot.name is None:
                 name = os.path.splitext(os.path.basename(command))[0]
@@ -155,10 +156,7 @@
             # FIXME: `None` doesn't seem to work, despite what documentation says, to be
             #    checked and reported upstream if confirmed.
             kwargs["env"] = os.environ
-        reactor.spawnProcess(prot,
-                             command,
-                             cmd_args,
-                             **kwargs)
+        reactor.spawnProcess(prot, command, cmd_args, **kwargs)
         return d