Stricter parameter checking for many public functions

This commit is contained in:
James R. Barlow
2021-01-31 19:27:25 -08:00
parent 390fdf8c05
commit bccf2f423f
8 changed files with 25 additions and 13 deletions
+1
View File
@@ -166,6 +166,7 @@ class GhostscriptFollower:
def generate_pdfa(
pdf_pages,
output_file: os.PathLike,
*,
compression: str,
pdf_version: str = '1.5',
pdfa_part: str = '2',
+1
View File
@@ -221,6 +221,7 @@ def _generate_null_hocr(output_hocr, output_text, image):
def generate_hocr(
*,
input_file: Path,
output_hocr: Path,
output_text: Path,
+3 -2
View File
@@ -69,7 +69,7 @@ def _setup_unpaper_io(tmpdir: Path, input_file: Path) -> Tuple[Path, Path]:
def run(
input_file: Path, output_file: Path, dpi: DecFloat, mode_args: List[str]
input_file: Path, output_file: Path, *, dpi: DecFloat, mode_args: List[str]
) -> None:
args_unpaper = ['unpaper', '-v', '--dpi', str(round(dpi, 6))] + mode_args
@@ -114,6 +114,7 @@ def validate_custom_args(args: str) -> List[str]:
def clean(
input_file: Path,
output_file: Path,
*,
dpi: DecFloat,
unpaper_args: Optional[List[str]] = None,
):
@@ -130,4 +131,4 @@ def clean(
]
if not unpaper_args:
unpaper_args = default_args
run(input_file, output_file, dpi, unpaper_args)
run(input_file, output_file, dpi=dpi, mode_args=unpaper_args)
+8 -3
View File
@@ -480,7 +480,12 @@ def preprocess_deskew(input_file: Path, page_context: PageContext):
def preprocess_clean(input_file: Path, page_context: PageContext):
output_file = page_context.get_path('pp_clean.png')
dpi = get_page_square_dpi(page_context.pageinfo, page_context.options)
unpaper.clean(input_file, output_file, dpi.x, page_context.options.unpaper_args)
unpaper.clean(
input_file,
output_file,
dpi=dpi.x,
unpaper_args=page_context.options.unpaper_args,
)
return output_file
@@ -616,9 +621,9 @@ def render_hocr_page(hocr: Path, page_context: PageContext):
dpi = get_page_square_dpi(page_context.pageinfo, options)
debug_mode = options.pdf_renderer == 'hocrdebug'
hocrtransform = HocrTransform(hocr, dpi.x) # square
hocrtransform = HocrTransform(hocr_filename=hocr, dpi=dpi.x) # square
hocrtransform.to_pdf(
output_file,
out_filename=output_file,
image_filename=None,
show_bounding_boxes=False if not debug_mode else True,
invisible_text=True if not debug_mode else False,
+1
View File
@@ -45,6 +45,7 @@ class Verbosity(IntEnum):
def configure_logging(
verbosity: Verbosity,
*,
progress_bar_friendly: bool = True,
manage_root_logger: bool = False,
):
+6 -5
View File
@@ -77,7 +77,7 @@ class HocrTransform:
{'': 'ff', '': 'ffi', '': 'ffl', '': 'fi', '': 'fl'}
)
def __init__(self, hocr_filename: Union[str, Path], dpi: float):
def __init__(self, *, hocr_filename: Union[str, Path], dpi: float):
self.dpi = dpi
self.hocr = ElementTree.parse(os.fspath(hocr_filename))
@@ -182,6 +182,7 @@ class HocrTransform:
def to_pdf(
self,
*,
out_filename: Path,
image_filename: Optional[Path] = None,
show_bounding_boxes: bool = False,
@@ -433,10 +434,10 @@ if __name__ == "__main__":
parser.add_argument('outputfile', help='Path to the PDF file to be generated')
args = parser.parse_args()
hocr = HocrTransform(args.hocrfile, args.resolution)
hocr = HocrTransform(hocr_filename=args.hocrfile, dpi=args.resolution)
hocr.to_pdf(
args.outputfile,
args.image,
args.boundingboxes,
out_filename=args.outputfile,
image_filename=args.image,
show_bounding_boxes=args.boundingboxes,
interword_spaces=args.interword_spaces,
)
+4 -2
View File
@@ -53,8 +53,10 @@ def test_mono_image(blank_hocr, outdir):
im.putpixel((n, n), 1)
im.save(outdir / 'mono.tif', format='TIFF')
hocr = hocrtransform.HocrTransform(str(blank_hocr), 300)
hocr.to_pdf(str(outdir / 'mono.pdf'), image_filename=str(outdir / 'mono.tif'))
hocr = hocrtransform.HocrTransform(hocr_filename=str(blank_hocr), dpi=300)
hocr.to_pdf(
out_filename=str(outdir / 'mono.pdf'), image_filename=str(outdir / 'mono.tif')
)
check_pdf(str(outdir / 'mono.pdf'))
+1 -1
View File
@@ -143,7 +143,7 @@ def test_multiple_pngs(resources, outdir):
outputstream=inpdf,
)
def mockquant(input_file, output_file, _quality_min, _quality_max):
def mockquant(input_file, output_file, *args):
with Image.open(input_file) as im:
draw = ImageDraw.Draw(im)
draw.rectangle((0, 0, im.width, im.height), fill=128)