Singleton of shared state / functionality throughout the app
| 15 | |
| 16 | |
| 17 | class DangerzoneCore: |
| 18 | """ |
| 19 | Singleton of shared state / functionality throughout the app |
| 20 | """ |
| 21 | |
| 22 | def __init__(self, isolation_provider: IsolationProvider) -> None: |
| 23 | # Initialize terminal colors |
| 24 | colorama.init(autoreset=True) |
| 25 | |
| 26 | # Languages supported by tesseract |
| 27 | with get_resource_path("ocr-languages.json").open("r") as f: |
| 28 | unsorted_ocr_languages = json.load(f) |
| 29 | self.ocr_languages = dict(sorted(unsorted_ocr_languages.items())) |
| 30 | |
| 31 | # Load settings |
| 32 | self.settings = Settings() |
| 33 | self.documents: list[Document] = [] |
| 34 | self.isolation_provider = isolation_provider |
| 35 | |
| 36 | def add_document_from_filename( |
| 37 | self, |
| 38 | input_filename: str, |
| 39 | output_filename: str | None = None, |
| 40 | archive: bool = False, |
| 41 | ) -> None: |
| 42 | doc = Document(input_filename, output_filename, archive=archive) |
| 43 | self.add_document(doc) |
| 44 | |
| 45 | def add_document(self, doc: Document) -> None: |
| 46 | if doc in self.documents: |
| 47 | raise errors.AddedDuplicateDocumentException() |
| 48 | self.documents.append(doc) |
| 49 | |
| 50 | def clear_documents(self) -> None: |
| 51 | log.debug("Removing all documents") |
| 52 | self.documents = [] |
| 53 | |
| 54 | def convert_documents( |
| 55 | self, ocr_lang: str | None, stdout_callback: Callable | None = None |
| 56 | ) -> None: |
| 57 | def convert_doc(document: Document) -> None: |
| 58 | try: |
| 59 | self.isolation_provider.convert( |
| 60 | document, |
| 61 | ocr_lang, |
| 62 | stdout_callback, |
| 63 | ) |
| 64 | |
| 65 | except Exception: |
| 66 | log.exception( |
| 67 | f"Unexpected error occurred while converting '{document}'" |
| 68 | ) |
| 69 | document.mark_as_failed() |
| 70 | |
| 71 | max_jobs = self.isolation_provider.get_max_parallel_conversions() |
| 72 | with concurrent.futures.ThreadPoolExecutor(max_workers=max_jobs) as executor: |
| 73 | executor.map(convert_doc, self.documents) |
| 74 |
no outgoing calls