mirror of
https://github.com/freedomofpress/dangerzone.git
synced 2025-04-29 10:12:38 +02:00
type hints for container.py & handle no stdout
We added the following check as well: + if stdout_callback and p.stdout is not None: Because, according to the subprocess docs[1]: > If the stdout argument was not PIPE, this attribute is None. In this case, it should not need to confirm that p.stdout is not None in the mypy static analysis. However it still complained. So we made mypy the favor and confirmed this was the case. [1]: https://docs.python.org/3/library/subprocess.html#subprocess.Popen.stdout
This commit is contained in:
parent
78daf75638
commit
f99131e30c
1 changed files with 15 additions and 4 deletions
|
@ -5,6 +5,8 @@ import platform
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import tempfile
|
import tempfile
|
||||||
|
from collections.abc import Callable
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
import appdirs
|
import appdirs
|
||||||
|
|
||||||
|
@ -28,7 +30,7 @@ log = logging.getLogger(__name__)
|
||||||
container_name = "dangerzone.rocks/dangerzone"
|
container_name = "dangerzone.rocks/dangerzone"
|
||||||
|
|
||||||
|
|
||||||
def exec(args, stdout_callback=None):
|
def exec(args: List[str], stdout_callback: Callable[[str], None] = None) -> int:
|
||||||
args_str = " ".join(pipes.quote(s) for s in args)
|
args_str = " ".join(pipes.quote(s) for s in args)
|
||||||
log.info("> " + args_str)
|
log.info("> " + args_str)
|
||||||
|
|
||||||
|
@ -41,7 +43,7 @@ def exec(args, stdout_callback=None):
|
||||||
universal_newlines=True,
|
universal_newlines=True,
|
||||||
startupinfo=startupinfo,
|
startupinfo=startupinfo,
|
||||||
) as p:
|
) as p:
|
||||||
if stdout_callback:
|
if stdout_callback and p.stdout is not None:
|
||||||
for line in p.stdout:
|
for line in p.stdout:
|
||||||
stdout_callback(line)
|
stdout_callback(line)
|
||||||
|
|
||||||
|
@ -49,7 +51,11 @@ def exec(args, stdout_callback=None):
|
||||||
return p.returncode
|
return p.returncode
|
||||||
|
|
||||||
|
|
||||||
def exec_container(command, extra_args=[], stdout_callback=None):
|
def exec_container(
|
||||||
|
command: List[str],
|
||||||
|
extra_args: List[str] = [],
|
||||||
|
stdout_callback: Callable[[str], None] = None,
|
||||||
|
) -> int:
|
||||||
if container_tech == "podman":
|
if container_tech == "podman":
|
||||||
container_runtime = shutil.which("podman")
|
container_runtime = shutil.which("podman")
|
||||||
if container_runtime is None:
|
if container_runtime is None:
|
||||||
|
@ -84,7 +90,12 @@ def exec_container(command, extra_args=[], stdout_callback=None):
|
||||||
return exec(args, stdout_callback)
|
return exec(args, stdout_callback)
|
||||||
|
|
||||||
|
|
||||||
def convert(input_filename, output_filename, ocr_lang, stdout_callback):
|
def convert(
|
||||||
|
input_filename: str,
|
||||||
|
output_filename: str,
|
||||||
|
ocr_lang: Optional[str],
|
||||||
|
stdout_callback: Callable[[str], None],
|
||||||
|
) -> bool:
|
||||||
success = False
|
success = False
|
||||||
|
|
||||||
if ocr_lang:
|
if ocr_lang:
|
||||||
|
|
Loading…
Reference in a new issue