robotframework-doctestlibrary 0.28.2__tar.gz → 0.29.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/DocumentRepresentation.py +185 -23
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/VisualTest.py +441 -415
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/PKG-INFO +1 -1
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/pyproject.toml +1 -1
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/Ai/__init__.py +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/Ai/prompts.py +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/Ai/renderers.py +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/Ai/responses.py +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/Downloader.py +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/IgnoreAreaManager.py +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/Ocr.py +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/PdfStructureComparator.py +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/PdfStructureModels.py +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/PdfTest.py +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/PrintJob.py +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/PrintJobTests.py +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/__init__.py +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/config.py +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/llm/__init__.py +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/llm/client.py +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/llm/config.py +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/llm/exceptions.py +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/llm/prompts.py +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/DocTest/llm/types.py +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/LICENSE +0 -0
- {robotframework_doctestlibrary-0.28.2 → robotframework_doctestlibrary-0.29.0}/README.md +0 -0
|
@@ -5,9 +5,10 @@ import numpy as np
|
|
|
5
5
|
import json
|
|
6
6
|
import logging
|
|
7
7
|
import re
|
|
8
|
+
from collections import OrderedDict
|
|
8
9
|
from pathlib import Path
|
|
9
10
|
from skimage import metrics
|
|
10
|
-
from typing import List, Dict, Optional, Tuple, Union, Literal
|
|
11
|
+
from typing import List, Dict, Optional, Tuple, Union, Literal, Iterator
|
|
11
12
|
from concurrent.futures import ThreadPoolExecutor
|
|
12
13
|
from pytesseract import Output
|
|
13
14
|
from DocTest.IgnoreAreaManager import IgnoreAreaManager
|
|
@@ -42,6 +43,20 @@ class Page:
|
|
|
42
43
|
self.is_pdf = False
|
|
43
44
|
self.ocr_tokens: List[str] = []
|
|
44
45
|
|
|
46
|
+
def release_resources(self):
|
|
47
|
+
"""Release heavy in-memory artifacts to assist streaming workflows."""
|
|
48
|
+
self.image = None
|
|
49
|
+
self.ocr_text_data = None
|
|
50
|
+
self.pdf_text_data = None
|
|
51
|
+
self.pdf_text_dict = None
|
|
52
|
+
self.pdf_text_blocks = None
|
|
53
|
+
self.pdf_text_words = None
|
|
54
|
+
self.text = ""
|
|
55
|
+
self.barcodes = []
|
|
56
|
+
self.pixel_ignore_areas = []
|
|
57
|
+
self._structure_cache.clear()
|
|
58
|
+
self.ocr_tokens = []
|
|
59
|
+
|
|
45
60
|
def apply_ocr(self, ocr_engine: str = OCR_ENGINE_DEFAULT, tesseract_config: str = TESSERACT_CONFIG, confidence: int = DEFAULT_CONFIDENCE):
|
|
46
61
|
"""Perform OCR on the page image."""
|
|
47
62
|
# re-scale the image to a standard resolution for OCR if needed
|
|
@@ -749,7 +764,20 @@ class Page:
|
|
|
749
764
|
return self.image[y:y+h, x:x+w]
|
|
750
765
|
|
|
751
766
|
class DocumentRepresentation:
|
|
752
|
-
def __init__(
|
|
767
|
+
def __init__(
|
|
768
|
+
self,
|
|
769
|
+
file_path: str,
|
|
770
|
+
dpi: int = DEFAULT_DPI,
|
|
771
|
+
ocr_engine: Literal['tesseract', 'east'] = OCR_ENGINE_DEFAULT,
|
|
772
|
+
tesseract_config: str = TESSERACT_CONFIG,
|
|
773
|
+
ignore_area_file: Union[str, dict, list] = None,
|
|
774
|
+
ignore_area: Union[str, dict, list] = None,
|
|
775
|
+
force_ocr: bool = False,
|
|
776
|
+
contains_barcodes: bool = False,
|
|
777
|
+
stream_pages: bool = False,
|
|
778
|
+
page_cache_size: int = 2,
|
|
779
|
+
**kwargs,
|
|
780
|
+
):
|
|
753
781
|
self.file_path = Path(file_path)
|
|
754
782
|
self.dpi = dpi
|
|
755
783
|
self.contains_barcodes = contains_barcodes
|
|
@@ -762,24 +790,36 @@ class DocumentRepresentation:
|
|
|
762
790
|
self.tmp_directory = tempfile.gettempdir()
|
|
763
791
|
self.ignore_printfactory_envelope=kwargs.get('ignore_printfactory_envelope', False)
|
|
764
792
|
self.printing_papersize=kwargs.get('printing_papersize', 'a4')
|
|
793
|
+
self.stream_pages = bool(stream_pages and self.file_path.suffix.lower() == '.pdf')
|
|
794
|
+
self.page_cache_size = max(1, page_cache_size)
|
|
795
|
+
self._page_cache: 'OrderedDict[int, Page]' = OrderedDict()
|
|
796
|
+
self._pdf_document = None
|
|
797
|
+
self.page_count: int = 0
|
|
798
|
+
self._force_ocr_for_ignore_areas = force_ocr
|
|
799
|
+
|
|
765
800
|
self.load_document()
|
|
766
801
|
self.create_abstract_ignore_areas(ignore_area_file, ignore_area)
|
|
767
802
|
self.create_pixel_based_ignore_areas(force_ocr)
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
if self.contains_barcodes:
|
|
803
|
+
|
|
804
|
+
if self.contains_barcodes and not self.stream_pages:
|
|
771
805
|
self.identify_barcodes()
|
|
772
806
|
|
|
773
807
|
def load_document(self):
|
|
774
808
|
"""Load the document, either as an image or a multi-page PDF, into Page objects."""
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
809
|
+
suffix = self.file_path.suffix.lower()
|
|
810
|
+
if suffix == '.pdf':
|
|
811
|
+
if self.stream_pages:
|
|
812
|
+
self._prepare_pdf_stream()
|
|
813
|
+
else:
|
|
814
|
+
self._load_pdf()
|
|
815
|
+
elif suffix == '.pcl':
|
|
778
816
|
self._load_pcl()
|
|
779
|
-
elif
|
|
817
|
+
elif suffix == '.ps':
|
|
780
818
|
self._load_ps()
|
|
781
819
|
else:
|
|
782
820
|
self._load_image()
|
|
821
|
+
if not self.stream_pages:
|
|
822
|
+
self.page_count = len(self.pages)
|
|
783
823
|
|
|
784
824
|
def _load_image(self):
|
|
785
825
|
"""Load a single image file as a Page object."""
|
|
@@ -810,9 +850,115 @@ class DocumentRepresentation:
|
|
|
810
850
|
page_obj.pdf_text_words = page.get_text("words", sort=True)
|
|
811
851
|
page_obj.pdf_text_blocks = page.get_text("blocks", sort=True)
|
|
812
852
|
self.pages.append(page_obj)
|
|
853
|
+
self.page_count = len(self.pages)
|
|
854
|
+
except ImportError:
|
|
855
|
+
raise ImportError("PyMuPDF (fitz) is required for PDF processing.")
|
|
856
|
+
|
|
857
|
+
def _prepare_pdf_stream(self):
|
|
858
|
+
try:
|
|
859
|
+
import fitz
|
|
860
|
+
fitz.TOOLS.set_aa_level(0)
|
|
861
|
+
self._pdf_document = fitz.open(str(self.file_path))
|
|
862
|
+
self.page_count = len(self._pdf_document)
|
|
813
863
|
except ImportError:
|
|
814
864
|
raise ImportError("PyMuPDF (fitz) is required for PDF processing.")
|
|
815
865
|
|
|
866
|
+
def _render_pdf_page(self, page_index: int) -> Page:
|
|
867
|
+
if self._pdf_document is None:
|
|
868
|
+
self._prepare_pdf_stream()
|
|
869
|
+
|
|
870
|
+
page = self._pdf_document.load_page(page_index)
|
|
871
|
+
pix = page.get_pixmap(dpi=self.dpi)
|
|
872
|
+
img_data = pix.tobytes("png")
|
|
873
|
+
image = cv2.imdecode(np.frombuffer(img_data, np.uint8), cv2.IMREAD_COLOR)
|
|
874
|
+
page_obj = Page(image, page_number=page_index + 1, dpi=self.dpi)
|
|
875
|
+
page_obj.is_pdf = True
|
|
876
|
+
page_obj.pdf_text_data = page.get_text("text", sort=True)
|
|
877
|
+
page_obj.pdf_text_dict = page.get_text("dict", sort=True)
|
|
878
|
+
page_obj.pdf_text_words = page.get_text("words", sort=True)
|
|
879
|
+
page_obj.pdf_text_blocks = page.get_text("blocks", sort=True)
|
|
880
|
+
|
|
881
|
+
self._apply_ignore_areas_to_page(page_obj, self._force_ocr_for_ignore_areas)
|
|
882
|
+
if self.contains_barcodes:
|
|
883
|
+
page_obj.identify_barcodes()
|
|
884
|
+
return page_obj
|
|
885
|
+
|
|
886
|
+
def get_page(self, page_index: int) -> Page:
|
|
887
|
+
if not self.stream_pages:
|
|
888
|
+
return self.pages[page_index]
|
|
889
|
+
if page_index < 0 or page_index >= self.page_count:
|
|
890
|
+
raise IndexError("Page index out of range")
|
|
891
|
+
cached = self._page_cache.get(page_index)
|
|
892
|
+
if cached is not None:
|
|
893
|
+
self._page_cache.move_to_end(page_index)
|
|
894
|
+
return cached
|
|
895
|
+
page = self._render_pdf_page(page_index)
|
|
896
|
+
self._page_cache[page_index] = page
|
|
897
|
+
self._evict_page_cache()
|
|
898
|
+
return page
|
|
899
|
+
|
|
900
|
+
def release_page(self, page_index: int):
|
|
901
|
+
if not self.stream_pages:
|
|
902
|
+
return
|
|
903
|
+
page = self._page_cache.pop(page_index, None)
|
|
904
|
+
if page is not None:
|
|
905
|
+
page.release_resources()
|
|
906
|
+
|
|
907
|
+
def _evict_page_cache(self):
|
|
908
|
+
while len(self._page_cache) > self.page_cache_size:
|
|
909
|
+
index, page = self._page_cache.popitem(last=False)
|
|
910
|
+
page.release_resources()
|
|
911
|
+
|
|
912
|
+
def iter_pages(self, release: bool = None) -> Iterator[Page]:
|
|
913
|
+
if release is None:
|
|
914
|
+
release = self.stream_pages
|
|
915
|
+
for idx in range(self.page_count):
|
|
916
|
+
page = self.get_page(idx)
|
|
917
|
+
yield page
|
|
918
|
+
if release:
|
|
919
|
+
self.release_page(idx)
|
|
920
|
+
|
|
921
|
+
def iter_page_pairs(
|
|
922
|
+
self,
|
|
923
|
+
other: 'DocumentRepresentation',
|
|
924
|
+
release: bool = None,
|
|
925
|
+
) -> Iterator[Tuple[Page, Page]]:
|
|
926
|
+
if self.page_count != other.page_count:
|
|
927
|
+
raise ValueError("Documents have different number of pages.")
|
|
928
|
+
if release is None:
|
|
929
|
+
release = self.stream_pages or other.stream_pages
|
|
930
|
+
for idx in range(self.page_count):
|
|
931
|
+
ref_page = self.get_page(idx)
|
|
932
|
+
cand_page = other.get_page(idx)
|
|
933
|
+
yield ref_page, cand_page
|
|
934
|
+
if release:
|
|
935
|
+
self.release_page(idx)
|
|
936
|
+
other.release_page(idx)
|
|
937
|
+
|
|
938
|
+
def close(self):
|
|
939
|
+
if self.stream_pages:
|
|
940
|
+
while self._page_cache:
|
|
941
|
+
_, page = self._page_cache.popitem()
|
|
942
|
+
page.release_resources()
|
|
943
|
+
if self._pdf_document is not None:
|
|
944
|
+
self._pdf_document.close()
|
|
945
|
+
self._pdf_document = None
|
|
946
|
+
|
|
947
|
+
def __enter__(self):
|
|
948
|
+
return self
|
|
949
|
+
|
|
950
|
+
def __exit__(self, exc_type, exc, tb):
|
|
951
|
+
self.close()
|
|
952
|
+
|
|
953
|
+
def __len__(self):
|
|
954
|
+
return self.page_count
|
|
955
|
+
|
|
956
|
+
def __del__(self): # pragma: no cover - defensive cleanup
|
|
957
|
+
try:
|
|
958
|
+
self.close()
|
|
959
|
+
except Exception:
|
|
960
|
+
pass
|
|
961
|
+
|
|
816
962
|
def _load_pcl(self):
|
|
817
963
|
import subprocess
|
|
818
964
|
import shutil
|
|
@@ -933,6 +1079,12 @@ class DocumentRepresentation:
|
|
|
933
1079
|
def get_barcodes(self):
|
|
934
1080
|
"""Return a list of barcodes detected in the document."""
|
|
935
1081
|
barcodes = []
|
|
1082
|
+
if self.stream_pages:
|
|
1083
|
+
for idx in range(self.page_count):
|
|
1084
|
+
page = self.get_page(idx)
|
|
1085
|
+
barcodes.extend(page.barcodes)
|
|
1086
|
+
self.release_page(idx)
|
|
1087
|
+
return barcodes
|
|
936
1088
|
for page in self.pages:
|
|
937
1089
|
barcodes.extend(page.barcodes)
|
|
938
1090
|
return barcodes
|
|
@@ -969,12 +1121,14 @@ class DocumentRepresentation:
|
|
|
969
1121
|
|
|
970
1122
|
def compare_with(self, other_doc: 'DocumentRepresentation') -> bool:
|
|
971
1123
|
"""Compare this document with another document."""
|
|
972
|
-
if
|
|
1124
|
+
if self.page_count != other_doc.page_count:
|
|
973
1125
|
raise ValueError("Documents have different number of pages.")
|
|
974
1126
|
|
|
975
1127
|
# Compare page by page
|
|
976
|
-
for page_self, page_other in
|
|
977
|
-
|
|
1128
|
+
for page_self, page_other in self.iter_page_pairs(other_doc, release=True):
|
|
1129
|
+
result = page_self.compare_with(page_other)
|
|
1130
|
+
similar = result[0] if isinstance(result, tuple) and result else bool(result)
|
|
1131
|
+
if not similar:
|
|
978
1132
|
return False
|
|
979
1133
|
return True
|
|
980
1134
|
|
|
@@ -989,19 +1143,21 @@ class DocumentRepresentation:
|
|
|
989
1143
|
|
|
990
1144
|
def create_pixel_based_ignore_areas(self, force_ocr: bool = False):
|
|
991
1145
|
"""Apply ignore areas to each page of the document."""
|
|
1146
|
+
if self.stream_pages:
|
|
1147
|
+
self._force_ocr_for_ignore_areas = force_ocr
|
|
1148
|
+
return
|
|
992
1149
|
|
|
993
1150
|
for page in self.pages:
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1151
|
+
self._apply_ignore_areas_to_page(page, force_ocr)
|
|
1152
|
+
|
|
1153
|
+
def _apply_ignore_areas_to_page(self, page: Page, force_ocr: bool = False):
|
|
1154
|
+
if not self.abstract_ignore_areas:
|
|
1155
|
+
return
|
|
1156
|
+
for ignore_area in self.abstract_ignore_areas:
|
|
1157
|
+
if ignore_area.get('type') in ['pattern', 'line_pattern', 'word_pattern']:
|
|
1158
|
+
if (force_ocr or not page.pdf_text_data) and not page.ocr_performed:
|
|
1159
|
+
page.apply_ocr(ocr_engine=self.ocr_engine)
|
|
1160
|
+
page._process_ignore_area(ignore_area)
|
|
1005
1161
|
|
|
1006
1162
|
def get_text_from_area(self, area: Dict, force_ocr: bool = False):
|
|
1007
1163
|
"""Extract text content from a specific area of the document."""
|
|
@@ -1037,6 +1193,12 @@ class DocumentRepresentation:
|
|
|
1037
1193
|
|
|
1038
1194
|
def identify_barcodes(self):
|
|
1039
1195
|
"""Detect barcodes in all pages."""
|
|
1196
|
+
if self.stream_pages:
|
|
1197
|
+
for idx in range(self.page_count):
|
|
1198
|
+
page = self.get_page(idx)
|
|
1199
|
+
page.identify_barcodes()
|
|
1200
|
+
self.release_page(idx)
|
|
1201
|
+
return
|
|
1040
1202
|
for page in self.pages:
|
|
1041
1203
|
page.identify_barcodes()
|
|
1042
1204
|
|