Skip to content

Commit

Permalink
WIP: Make the dummy provider less... dummy
Browse files Browse the repository at this point in the history
  • Loading branch information
apyrgio committed Sep 30, 2024
1 parent 174df34 commit 0fba8b1
Showing 1 changed file with 30 additions and 39 deletions.
69 changes: 30 additions & 39 deletions dangerzone/isolation_provider/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,27 @@
import time
from typing import Callable, Optional

from ..document import Document
from ..conversion.common import DangerzoneConverter
from ..document import Document
from ..util import get_resource_path
from .base import IsolationProvider

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):
"""Dummy Isolation Provider (FOR TESTING ONLY)
Expand All @@ -28,53 +42,30 @@ def __init__(self) -> None:
+ "called in a non-testing system."
)

super().__init__()

def install(self) -> bool:
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
# time.sleep(0.2)
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:
return subprocess.Popen("True", start_new_session=True)
cmd = [
"python3",
"-c",
"from dangerzone.isolation_provider.dummy import dummy_script;"
" dummy_script()"
]
return subprocess.Popen(
cmd,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=self.proc_stderr,
start_new_session=True
)

def terminate_doc_to_pixels_proc(
self, document: Document, p: subprocess.Popen
) -> None:
pass
self.terminate_process_group(p)

def get_max_parallel_conversions(self) -> int:
return 1

0 comments on commit 0fba8b1

Please sign in to comment.