Skip to content

Commit 28460d9

Browse files
committed
Wrapped watch in exception handling so nothing should terminate it except ctrl-c
1 parent 8c7ca87 commit 28460d9

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed

pypdfocr/pypdfocr.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import smtplib
1717
import argparse
18-
import sys, os
18+
import sys, os, traceback
1919
import logging
2020
import shutil, glob
2121
import itertools
@@ -326,9 +326,8 @@ def run_conversion(self, pdf_filename):
326326
:rtype: filename string
327327
"""
328328
print ("Starting conversion of %s" % pdf_filename)
329-
conversion_format = "tiff"
330329
# Make the images for Tesseract
331-
img_dpi, glob_img_filename = self.gs.make_img_from_pdf(pdf_filename, conversion_format)
330+
img_dpi, glob_img_filename = self.gs.make_img_from_pdf(pdf_filename)
332331

333332
fns = glob.glob(glob_img_filename)
334333

@@ -432,9 +431,17 @@ def go(self, argv):
432431

433432
# Do the actual conversion followed by optional filing and email
434433
if self.watch:
435-
py_watcher = PyPdfWatcher(self.watch_dir, self.config.get('watch'))
436-
for pdf_filename in py_watcher.start():
437-
self._convert_and_file_email(pdf_filename)
434+
while True: # Make sure the watcher doesn't terminate
435+
try:
436+
py_watcher = PyPdfWatcher(self.watch_dir, self.config.get('watch'))
437+
for pdf_filename in py_watcher.start():
438+
self._convert_and_file_email(pdf_filename)
439+
except KeyboardInterrupt:
440+
break
441+
except Exception as e:
442+
print traceback.print_exc(e)
443+
py_watcher.stop()
444+
438445
else:
439446
self._convert_and_file_email(self.pdf_filename)
440447

pypdfocr/pypdfocr_gs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def _run_gs(self, options, output_filename, pdf_filename):
171171
error (self.msgs['GS_FAILED'])
172172

173173

174-
def make_img_from_pdf(self, pdf_filename, output_format):
174+
def make_img_from_pdf(self, pdf_filename):
175175
self._get_dpi(pdf_filename) # No need to bother anymore
176176

177177
if not os.path.exists(pdf_filename):

pypdfocr/pypdfocr_watcher.py

+13-15
Original file line numberDiff line numberDiff line change
@@ -37,23 +37,21 @@ def __init__(self, monitor_dir, config):
3737
self.scan_interval = config.get('scan_interval', 3) # If no updates in 3 seconds (or user specified option in config file) process file
3838

3939
def start(self):
40-
try:
41-
while True:
42-
observer = Observer()
43-
observer.schedule(self, self.monitor_dir)
44-
observer.start()
45-
print("Starting to watch for new pdfs in %s" % (self.monitor_dir))
46-
while True:
47-
logging.info("Sleeping for %d seconds" % self.scan_interval)
48-
time.sleep(self.scan_interval)
49-
newFile = self.check_queue()
50-
if newFile:
51-
yield newFile
52-
observer.join()
53-
except KeyboardInterrupt:
54-
return
40+
self.observer = Observer()
41+
self.observer.schedule(self, self.monitor_dir)
42+
self.observer.start()
43+
print("Starting to watch for new pdfs in %s" % (self.monitor_dir))
44+
while True:
45+
logging.info("Sleeping for %d seconds" % self.scan_interval)
46+
time.sleep(self.scan_interval)
47+
newFile = self.check_queue()
48+
if newFile:
49+
yield newFile
50+
self.observer.join()
5551

5652

53+
def stop(self):
54+
self.observer.stop()
5755

5856
def rename_file_with_spaces(self, pdf_filename):
5957
"""

test/test_gs.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_gs_run_nt(self, mock_subprocess, mock_os_name, capsys):
4343
def test_gs_pdf_missing(self, capsys):
4444
p = P.PyGs()
4545
with pytest.raises(SystemExit):
46-
p.make_img_from_pdf("missing123.pdf", "")
46+
p.make_img_from_pdf("missing123.pdf")
4747
out,err = capsys.readouterr()
4848
assert p.msgs['GS_MISSING_PDF'] in out
4949

0 commit comments

Comments
 (0)