Skip to content

Commit 8a73c03

Browse files
committed
Fix scan codes
1 parent bdf8bd6 commit 8a73c03

File tree

2 files changed

+29
-22
lines changed

2 files changed

+29
-22
lines changed

scan_to_paperless/process.py

+25-19
Original file line numberDiff line numberDiff line change
@@ -1795,7 +1795,7 @@ async def finalize(
17951795
print(f"Uploaded {temporary_pdf.name} with title {title}")
17961796

17971797

1798-
def _process_code(name: str) -> None:
1798+
async def _process_code(name: str) -> bool:
17991799
"""Detect ad add a page with the QR codes."""
18001800
pdf_filename = os.path.join(os.environ.get("SCAN_CODES_FOLDER", "/scan-codes"), name)
18011801

@@ -1807,27 +1807,32 @@ def _process_code(name: str) -> None:
18071807
return
18081808

18091809
try:
1810-
_LOG.info("Processing codes for %s", pdf_filename)
1811-
from scan_to_paperless import code # pylint: disable=import-outside-toplevel
1812-
1813-
code.add_codes(
1814-
pdf_filename,
1815-
destination_filename,
1816-
dpi=float(os.environ.get("SCAN_CODES_DPI", 200)),
1817-
pdf_dpi=float(os.environ.get("SCAN_CODES_PDF_DPI", 72)),
1818-
font_name=os.environ.get("SCAN_CODES_FONT_NAME", "Helvetica-Bold"),
1819-
font_size=float(os.environ.get("SCAN_CODES_FONT_SIZE", 16)),
1820-
margin_top=float(os.environ.get("SCAN_CODES_MARGIN_TOP", 0)),
1821-
margin_left=float(os.environ.get("SCAN_CODES_MARGIN_LEFT", 2)),
1822-
)
1823-
if os.path.exists(destination_filename):
1824-
# Remove the source file on success
1825-
os.remove(pdf_filename)
1826-
_LOG.info("Down processing codes for %s", pdf_filename)
1810+
if os.path.exists(pdf_filename):
1811+
_LOG.info("Processing codes for %s", pdf_filename)
1812+
from scan_to_paperless import code # pylint: disable=import-outside-toplevel
1813+
1814+
code.add_codes(
1815+
pdf_filename,
1816+
destination_filename,
1817+
dpi=float(os.environ.get("SCAN_CODES_DPI", 200)),
1818+
pdf_dpi=float(os.environ.get("SCAN_CODES_PDF_DPI", 72)),
1819+
font_name=os.environ.get("SCAN_CODES_FONT_NAME", "Helvetica-Bold"),
1820+
font_size=float(os.environ.get("SCAN_CODES_FONT_SIZE", 16)),
1821+
margin_top=float(os.environ.get("SCAN_CODES_MARGIN_TOP", 0)),
1822+
margin_left=float(os.environ.get("SCAN_CODES_MARGIN_LEFT", 2)),
1823+
)
1824+
if os.path.exists(destination_filename):
1825+
# Remove the source file on success
1826+
os.remove(pdf_filename)
1827+
_LOG.info("Down processing codes for %s", pdf_filename)
1828+
return True
18271829

18281830
except Exception as exception:
18291831
_LOG.exception("Error while processing %s: %s", pdf_filename, str(exception))
18301832

1833+
await asyncio.sleep(1)
1834+
return False
1835+
18311836

18321837
def is_sources_present(images: list[str], root_folder: str) -> bool:
18331838
"""Are sources present for the next step."""
@@ -2034,7 +2039,8 @@ async def _task(status: scan_to_paperless.status.Status) -> None:
20342039
status.set_global_status(f"Process code '{name}'...")
20352040
status.set_current_folder(name)
20362041
try:
2037-
_process_code(name)
2042+
if not await _process_code(name):
2043+
status.update_scan_codes()
20382044
except Exception as exception:
20392045
print(exception)
20402046
trace = traceback.format_exc()

scan_to_paperless/status.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ def set_status(
164164

165165
def _init(self) -> None:
166166
"""Scan for changes for waiting documents."""
167-
self._update_scan_codes()
167+
self.update_scan_codes()
168168
self.write()
169169
self._update_consume()
170170
self.write()
@@ -325,7 +325,8 @@ def get_next_job(self) -> tuple[str | None, JobType, process_schema.Step | None]
325325

326326
return None, JobType.NONE, None
327327

328-
def _update_scan_codes(self) -> None:
328+
def update_scan_codes(self) -> None:
329+
"""Update the list of files witch one we should scan the codes."""
329330
self._codes = [
330331
f[len(self._codes_folder) :]
331332
for f in glob.glob(os.path.join(self._codes_folder, "**"), recursive=True)
@@ -358,7 +359,7 @@ async def _watch_scan_codes(self) -> None:
358359
asyncinotify.Mask.CLOSE_WRITE | asyncinotify.Mask.DELETE | asyncinotify.Mask.MOVE,
359360
)
360361
async for _ in inotify:
361-
self._update_scan_codes()
362+
self.update_scan_codes()
362363
self.write()
363364

364365
def _update_consume(self) -> None:

0 commit comments

Comments
 (0)