WIP: Make the dummy provider less... dummy

This commit is contained in:
Alex Pyrgiotis 2024-09-30 12:45:51 +03:00
parent f9dfec6112
commit 76b0de4169
No known key found for this signature in database
GPG key ID: B6C15EBA0357C9AA

View file

@ -6,6 +6,8 @@ import sys
import time import time
from typing import Callable, Optional from typing import Callable, Optional
from ..document import Document
from ..conversion.common import DangerzoneConverter
from ..document import Document from ..document import Document
from ..util import get_resource_path from ..util import get_resource_path
from .base import IsolationProvider, terminate_process_group from .base import IsolationProvider, terminate_process_group
@ -13,6 +15,18 @@ from .base import IsolationProvider, terminate_process_group
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
def dummy_script() -> None:
sys.stdin.read()
pages = 2
width = height = 9
DangerzoneConverter._write_int(pages)
for page in range(pages):
DangerzoneConverter._write_int(width)
DangerzoneConverter._write_int(height)
DangerzoneConverter._write_bytes(width * height * 3 * b"A")
class Dummy(IsolationProvider): class Dummy(IsolationProvider):
"""Dummy Isolation Provider (FOR TESTING ONLY) """Dummy Isolation Provider (FOR TESTING ONLY)
@ -32,50 +46,19 @@ class Dummy(IsolationProvider):
def install(self) -> bool: def install(self) -> bool:
return True return True
def convert(
self,
document: Document,
ocr_lang: Optional[str],
progress_callback: Optional[Callable] = None,
) -> None:
self.progress_callback = None
log.debug("Dummy converter started:")
log.debug(
f" - document: {os.path.basename(document.input_filename)} ({document.id})"
)
log.debug(f" - ocr : {ocr_lang}")
log.debug("\n(simulating conversion)")
success = True
progress = [
[False, "Converting to PDF using GraphicsMagick", 0.0],
[False, "Separating document into pages", 3.0],
[False, "Converting page 1/1 to pixels", 5.0],
[False, "Converted document to pixels", 50.0],
[False, "Converting page 1/1 from pixels to PDF", 50.0],
[False, "Merging 1 pages into a single PDF", 95.0],
[False, "Compressing PDF", 97.0],
[False, "Safe PDF created", 100.0],
]
for error, text, percentage in progress:
self.print_progress(document, error, text, percentage) # type: ignore [arg-type]
if error:
success = False
if success:
shutil.copy(
get_resource_path("dummy_document.pdf"), document.output_filename
)
document.mark_as_safe()
if document.archive_after_conversion:
document.archive()
def start_doc_to_pixels_proc(self, document: Document) -> subprocess.Popen: def start_doc_to_pixels_proc(self, document: Document) -> subprocess.Popen:
dummy_cmd = ["python3", "-c", "print('The cake is a lie')"] cmd = [
"python3",
"-c",
"from dangerzone.isolation_provider.dummy import dummy_script;"
" dummy_script()"
]
return subprocess.Popen( return subprocess.Popen(
dummy_cmd, cmd,
stdin=subprocess.PIPE, stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=self.proc_stderr, stderr=self.proc_stderr,
start_new_session=True, start_new_session=True
) )
def terminate_doc_to_pixels_proc( def terminate_doc_to_pixels_proc(