Get pluggy to work with forking workers
This commit is contained in:
@@ -19,6 +19,9 @@ import logging
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
from functools import partial
|
||||
|
||||
from ocrmypdf._plugin_manager import get_plugin_manager
|
||||
|
||||
|
||||
class PDFContext:
|
||||
@@ -59,10 +62,22 @@ class PageContext:
|
||||
self.name = pdf_context.name
|
||||
self.pageno = pageno
|
||||
self.pageinfo = pdf_context.pdfinfo[pageno]
|
||||
self.plugin_manager = pdf_context.plugin_manager
|
||||
|
||||
def get_path(self, name):
|
||||
return os.path.join(self.work_folder, "%06d_%s" % (self.pageno + 1, name))
|
||||
|
||||
def __getstate__(self):
|
||||
state = self.__dict__.copy()
|
||||
del state['plugin_manager']
|
||||
state['construct_plugin_manager'] = partial(get_plugin_manager, self.options)
|
||||
return state
|
||||
|
||||
def __setstate__(self, state):
|
||||
self.__dict__.update(state)
|
||||
self.plugin_manager = self.__dict__['construct_plugin_manager']()
|
||||
del self.__dict__['construct_plugin_manager']
|
||||
|
||||
|
||||
def cleanup_working_files(work_folder, options):
|
||||
if options.keep_temporary_files:
|
||||
|
||||
@@ -521,6 +521,9 @@ def create_ocr_image(image, page_context):
|
||||
im = pix.topil()
|
||||
|
||||
del draw
|
||||
|
||||
im = page_context.plugin_manager.hook.filter_ocr_image(image=im)
|
||||
|
||||
# Pillow requires integer DPI
|
||||
dpi = tuple(round(coord) for coord in im.info['dpi'])
|
||||
im.save(output_file, dpi=dpi)
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
import importlib
|
||||
|
||||
import pluggy
|
||||
|
||||
from ocrmypdf import pluginspec
|
||||
|
||||
|
||||
def get_plugin_manager(options):
|
||||
pm = pluggy.PluginManager('ocrmypdf')
|
||||
pm.add_hookspecs(pluginspec)
|
||||
|
||||
for name in options.plugins:
|
||||
module = importlib.import_module(name)
|
||||
pm.register(module)
|
||||
return pm
|
||||
+2
-11
@@ -29,7 +29,7 @@ from tempfile import mkdtemp
|
||||
import PIL
|
||||
import pluggy
|
||||
|
||||
from ocrmypdf import _pluginspec
|
||||
from ocrmypdf import pluginspec
|
||||
from ocrmypdf._concurrent import exec_progress_pool
|
||||
from ocrmypdf._graft import OcrGrafter
|
||||
from ocrmypdf._jobcontext import PDFContext, cleanup_working_files
|
||||
@@ -59,6 +59,7 @@ from ocrmypdf._pipeline import (
|
||||
triage,
|
||||
validate_pdfinfo_options,
|
||||
)
|
||||
from ocrmypdf._plugin_manager import get_plugin_manager
|
||||
from ocrmypdf._validation import (
|
||||
check_requested_output_file,
|
||||
create_input_file,
|
||||
@@ -292,16 +293,6 @@ def configure_debug_logging(log_filename, prefix=''):
|
||||
return log_file_handler
|
||||
|
||||
|
||||
def get_plugin_manager(options):
|
||||
pm = pluggy.PluginManager('ocrmypdf')
|
||||
pm.add_hookspecs(_pluginspec)
|
||||
|
||||
for name in options.plugins:
|
||||
module = importlib.import_module(name)
|
||||
pm.register(module)
|
||||
return pm
|
||||
|
||||
|
||||
def run_pipeline(options, api=False):
|
||||
# Any changes to options will not take effect for options that are already
|
||||
# bound to function parameters in the pipeline. (For example
|
||||
|
||||
@@ -1,6 +1,11 @@
|
||||
import ocrmypdf
|
||||
from ocrmypdf import hookimpl
|
||||
|
||||
|
||||
@ocrmypdf.hookimpl
|
||||
@hookimpl
|
||||
def prepare(options):
|
||||
raise ValueError('foo')
|
||||
pass
|
||||
|
||||
|
||||
@hookimpl
|
||||
def filter_ocr_image(image):
|
||||
return image
|
||||
|
||||
Reference in New Issue
Block a user