diff --git a/files/runScanner.sh b/files/runScanner.sh index 7cabf45..04383b0 100755 --- a/files/runScanner.sh +++ b/files/runScanner.sh @@ -104,6 +104,8 @@ echo "capabilities:" scanimage -A echo "startup successful" +rm -f /var/log/scanner.log +touch /var/log/scanner.log while true; do tail -f /var/log/scanner.log done diff --git a/script/scanner.py b/script/scanner.py index 8afabd5..5dba2e7 100755 --- a/script/scanner.py +++ b/script/scanner.py @@ -158,9 +158,7 @@ def remove_blank_pages( return page_count = int(pages_line.group(1)) - print( - f" Analyzing {page_count} pages in {input_file} with threshold {remove_blank_threshold}%" - ) + print(f" Analyzing {page_count} pages in {input_file} with threshold {remove_blank_threshold}%") os.chdir(dirname) def non_blank_pages() -> List[str]: @@ -186,22 +184,14 @@ def non_blank_pages() -> List[str]: output.decode(), re.MULTILINE, ) - if ink_coverage_line is None: - ink_coverage = None - else: + if ink_coverage_line is not None: ink_coverage = sum(map(float, ink_coverage_line.groups())) - if ink_coverage is not None and ink_coverage < remove_blank_threshold: - print( - f" Page { - page}: delete (ink coverage: {ink_coverage:.2f}%)" - ) - else: - picked_pages += str(page) - print( - f" Page { - page}: keep (ink coverage: {ink_coverage:.2f}%)" - ) + if ink_coverage < remove_blank_threshold: + print(f" Page {page}: delete (ink coverage: {ink_coverage:.2f}%)") + else: + picked_pages += str(page) + print(f" Page {page}: keep (ink coverage: {ink_coverage:.2f}%)") return picked_pages @@ -224,20 +214,11 @@ def non_blank_pages() -> List[str]: print(f" No blank pages detected in {input_file}") else: os.replace(output_file, input_file) - print( - f" Removed { - removed_pages} blank pages and saved as {input_file}" - ) + print(f" Removed {removed_pages} blank pages and saved as {input_file}") except FileNotFoundError: - print( - f" WARNING: '{ - command[0]}' executable not found. Skipping PDF manipulation." - ) + print(f" WARNING: '{command[0]}' executable not found. Skipping PDF manipulation.") except subprocess.CalledProcessError: - print( - f" ERROR: manipulating { - input_file}. Skipping PDF manipulation." - ) + print(f" ERROR: manipulating {input_file}. Skipping PDF manipulation.") # @@ -334,10 +315,7 @@ def convert_and_post_process( def wait_for_rear_pages_or_convert(job_name: str) -> None: # Wait for 2 minutes in case there is a rear side scan - print( - f" front side: Waiting for 2 minutes before starting file conversion for { - job_name}" - ) + print(f" front side: Waiting for 2 minutes before starting file conversion for {job_name}") time.sleep(120) convert_and_post_process(job_name, "front", None) @@ -391,10 +369,7 @@ def kill_front_processing_from_pid(job_dir: str) -> Optional[int]: try: with open(path, "r") as scan_pid_file: pid = int(scan_pid_file.read().strip()) - print( - f" rear side: Read pid from { - path}, killing front processing job {pid}" - ) + print(f" rear side: Read pid from {path}, killing front processing job {pid}") os.kill(pid, signal.SIGKILL) except FileNotFoundError: print(" rear side: ERROR: scan_pid file {path} not found.") @@ -439,10 +414,7 @@ def scan_front(log: TextIO, device: Optional[str], scanimage_args=[]) -> None: os._exit(0) # Exit child process cleanly elif pid > 0: save_front_processing_pid(job_dir, pid) - print( - f" front side: INFO: Waiting to start conversion process for { - job_name} in process with PID {pid}" - ) + print(f" front side: INFO: Waiting to start conversion process for {job_name} in process with PID {pid}") else: print(f" front side: ERROR: Fork failed ({pid}).") @@ -488,10 +460,7 @@ def scan_rear(log: TextIO, device: Optional[str], scanimage_args=None) -> None: cnt += 1 cnt_formatted = f"{cnt:03d}" os.rename(filename, f"index{cnt_formatted}-1-{filename}") - print( - f" rear side: DEBUG: renamed {filename} to index{ - cnt_formatted}-1-{filename}" - ) + print(f" rear side: DEBUG: renamed {filename} to index{cnt_formatted}-1-{filename}") cnt = 0 for filename in glob.glob("*back*.pnm"): @@ -499,15 +468,12 @@ def scan_rear(log: TextIO, device: Optional[str], scanimage_args=None) -> None: rear_index = number_of_pages - cnt + 1 rear_index_formatted = f"{rear_index:03d}" os.rename(filename, f"index{rear_index_formatted}-2-{filename}") - print( - f" rear side: DEBUG: renamed {filename} to index{ - rear_index_formatted}-2-{filename}" - ) + print(f" rear side: DEBUG: renamed {filename} to index{rear_index_formatted}-2-{filename}") # Convert to PDF remove_blank_threshold_str = os.getenv("REMOVE_BLANK_THRESHOLD") remove_blank_threshold = None - if remove_blank_threshold_str is not None: + if remove_blank_threshold_str is not None and remove_blank_threshold_str != "": remove_blank_threshold = float(remove_blank_threshold_str) pid = os.fork()