Rename filters to plugins
This commit is contained in:
@@ -29,7 +29,7 @@ from pikepdf.models.metadata import encode_pdf_date
|
||||
|
||||
from . import PROGRAM_NAME, VERSION, leptonica
|
||||
|
||||
from ._filters import load_filter
|
||||
from ._plugins import load_plugin
|
||||
from .exceptions import (
|
||||
DpiError,
|
||||
EncryptedPdfError,
|
||||
@@ -524,7 +524,7 @@ def create_ocr_image(image, page_context):
|
||||
im = pix.topil()
|
||||
|
||||
if options.filter_ocr_image:
|
||||
filt = load_filter(options.filter_ocr_image)
|
||||
filt = load_plugin(options.filter_ocr_image)
|
||||
im = filt(im)
|
||||
|
||||
del draw
|
||||
|
||||
@@ -62,21 +62,21 @@ def _load_function_from_pyfile(location):
|
||||
return fn
|
||||
|
||||
|
||||
def load_filter(filt):
|
||||
if callable(filt):
|
||||
return filt
|
||||
def load_plugin(plugin):
|
||||
if callable(plugin):
|
||||
return plugin
|
||||
|
||||
if not isinstance(filt, str):
|
||||
if not isinstance(plugin, str):
|
||||
raise TypeError()
|
||||
|
||||
if '::' not in filt:
|
||||
filt = _load_function_from_module(filt)
|
||||
if '::' not in plugin:
|
||||
plugin = _load_function_from_module(plugin)
|
||||
else:
|
||||
filt = _load_function_from_pyfile(filt)
|
||||
plugin = _load_function_from_pyfile(plugin)
|
||||
|
||||
return filt
|
||||
return plugin
|
||||
|
||||
|
||||
def check_filter_loadable(filt):
|
||||
load_filter(filt)
|
||||
return filt
|
||||
def check_plugin_loadable(plugin):
|
||||
load_plugin(plugin)
|
||||
return plugin
|
||||
+2
-2
@@ -18,7 +18,7 @@
|
||||
import argparse
|
||||
|
||||
from . import PROGRAM_NAME, VERSION
|
||||
from ._filters import check_filter_loadable
|
||||
from ._plugins import check_plugin_loadable
|
||||
|
||||
|
||||
def numeric(basetype, min_=None, max_=None):
|
||||
@@ -469,7 +469,7 @@ advanced.add_argument(
|
||||
|
||||
filters = parser.add_argument_group("Filters", argparse.SUPPRESS)
|
||||
filters.add_argument(
|
||||
'--filter-ocr-image', help=argparse.SUPPRESS, type=check_filter_loadable
|
||||
'--filter-ocr-image', help=argparse.SUPPRESS, type=check_plugin_loadable
|
||||
)
|
||||
|
||||
debugging = parser.add_argument_group(
|
||||
|
||||
@@ -23,7 +23,7 @@ import pytest
|
||||
|
||||
from ocrmypdf import ocrmypdf
|
||||
from ocrmypdf.filters import invert, whiteout
|
||||
from ocrmypdf._filters import load_filter
|
||||
from ocrmypdf._plugins import load_plugin
|
||||
|
||||
|
||||
os_environ = pytest.helpers.os_environ
|
||||
@@ -35,28 +35,28 @@ def filter_42():
|
||||
|
||||
|
||||
def test_pyfile():
|
||||
obj = load_filter(f'{__file__}::filter_42')
|
||||
obj = load_plugin(f'{__file__}::filter_42')
|
||||
assert obj() == 42
|
||||
|
||||
|
||||
def test_pyfile_notexist():
|
||||
with pytest.raises(FileNotFoundError):
|
||||
load_filter('thisfile.doesnot.exist.py::filter_42')
|
||||
load_plugin('thisfile.doesnot.exist.py::filter_42')
|
||||
|
||||
|
||||
def test_pyfile_noobject():
|
||||
with pytest.raises(AttributeError):
|
||||
load_filter(f'{__file__}::no_function_with_this_name')
|
||||
load_plugin(f'{__file__}::no_function_with_this_name')
|
||||
|
||||
|
||||
def test_module():
|
||||
obj = load_filter(f'os.getuid')
|
||||
obj = load_plugin(f'os.getuid')
|
||||
assert obj() == os.getuid()
|
||||
|
||||
|
||||
def test_module_notexist():
|
||||
with pytest.raises(ModuleNotFoundError):
|
||||
load_filter('thismodule.doesnot.exist')
|
||||
load_plugin('thismodule.doesnot.exist')
|
||||
|
||||
|
||||
def test_filter_from_cmdline(resources, outdir):
|
||||
|
||||
Reference in New Issue
Block a user