add type hints to container dz py code

This commit is contained in:
deeplow 2022-08-22 11:46:27 +01:00
parent f44e6521b6
commit 463ff97b97
No known key found for this signature in database
GPG key ID: 577982871529A52A
2 changed files with 9 additions and 8 deletions

View file

@ -30,7 +30,7 @@ mypy-container:
mypy-dev-scripts: mypy-dev-scripts:
mypy $(MYPY_ARGS) install dev_scripts/* *.py mypy $(MYPY_ARGS) install dev_scripts/* *.py
mypy: mypy-host # mypy-container mypy-dev-scripts ## check type hints with mypy mypy: mypy-host mypy-container # mypy-dev-scripts ## check type hints with mypy
.PHONY: lint .PHONY: lint
lint: lint-black lint-isort mypy ## check the code with various linters lint: lint-black lint-isort mypy ## check the code with various linters

View file

@ -18,19 +18,20 @@ import os
import shutil import shutil
import subprocess import subprocess
import sys import sys
from typing import Dict, Optional
import magic import magic
from PIL import Image from PIL import Image
class DangerzoneConverter: class DangerzoneConverter:
def __init__(self): def __init__(self) -> None:
pass pass
def document_to_pixels(self): def document_to_pixels(self) -> int:
percentage = 0.0 percentage = 0.0
conversions = { conversions: Dict[str, Dict[str, Optional[str]]] = {
# .pdf # .pdf
"application/pdf": {"type": None}, "application/pdf": {"type": None},
# .docx # .docx
@ -312,8 +313,8 @@ class DangerzoneConverter:
return 0 return 0
def pixels_to_pdf(self): def pixels_to_pdf(self) -> int:
percentage = 50.0 percentage: float = 50.0
num_pages = len(glob.glob("/dangerzone/page-*.rgb")) num_pages = len(glob.glob("/dangerzone/page-*.rgb"))
@ -516,12 +517,12 @@ class DangerzoneConverter:
return 0 return 0
def output(self, error, text, percentage): def output(self, error: bool, text: str, percentage: float) -> None:
print(json.dumps({"error": error, "text": text, "percentage": int(percentage)})) print(json.dumps({"error": error, "text": text, "percentage": int(percentage)}))
sys.stdout.flush() sys.stdout.flush()
def main(): def main() -> int:
if len(sys.argv) != 2: if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} [document-to-pixels]|[pixels-to-pdf]") print(f"Usage: {sys.argv[0]} [document-to-pixels]|[pixels-to-pdf]")
return -1 return -1