From 9346d1f970cb8079aaf006904fdfc306d2d6375b Mon Sep 17 00:00:00 2001 From: "James R. Barlow" Date: Mon, 9 Oct 2023 02:30:30 -0700 Subject: [PATCH] Simplify deep nested with-block --- src/ocrmypdf/_pipeline.py | 62 +++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 32 deletions(-) diff --git a/src/ocrmypdf/_pipeline.py b/src/ocrmypdf/_pipeline.py index 9c875ef3..43031e73 100644 --- a/src/ocrmypdf/_pipeline.py +++ b/src/ocrmypdf/_pipeline.py @@ -926,7 +926,9 @@ def metadata_fixup(working_file: Path, context: PdfContext) -> Path: with pikepdf.open(context.origin) as original, pikepdf.open(working_file) as pdf: docinfo = get_docinfo(original, context) - with pdf.open_metadata() as meta_pdf: + with original.open_metadata( + set_pikepdf_as_editor=False, update_docinfo=False, strict=False + ) as meta_original, pdf.open_metadata() as meta_pdf: meta_pdf.load_from_docinfo( docinfo, delete_missing=False, raise_failure=False ) @@ -934,37 +936,33 @@ def metadata_fixup(working_file: Path, context: PdfContext) -> Path: # ensure consistency with Ghostscript. if 'xmp:CreateDate' not in meta_pdf: meta_pdf['xmp:CreateDate'] = meta_pdf.get('xmp:ModifyDate', '') - - with original.open_metadata( - set_pikepdf_as_editor=False, update_docinfo=False, strict=False - ) as meta_original: - if meta_pdf.get('dc:title') == 'Untitled': - # Ghostscript likes to set title to Untitled if omitted from input. - # Reverse this, because PDF/A TechNote 0003:Metadata in PDF/A-1 - # and the XMP Spec do not make this recommendation. - if 'dc:title' not in meta_original: - del meta_pdf['dc:title'] - # If the user explicitly specified an empty string for any of the - # following, they should be unset and not reported as missing in - # the output pdf. Note that some metadata fields use differing names - # between PDF-A and PDF. - for meta in [meta_pdf, meta_original]: - if options.title == '' and 'dc:title' in meta: - del meta['dc:title'] # PDF-A and PDF - if options.author == '': - if 'dc:creator' in meta: - del meta['dc:creator'] # PDF-A (Not xmp:CreatorTool) - if 'pdf:Author' in meta: - del meta['pdf:Author'] # PDF - if options.subject == '': - if 'dc:description' in meta: - del meta['dc:description'] # PDF-A - if 'dc:subject' in meta: - del meta['dc:subject'] # PDF - if options.keywords == '' and 'pdf:Keywords' in meta: - del meta['pdf:Keywords'] # PDF-A and PDF - meta_missing = set(meta_original.keys()) - set(meta_pdf.keys()) - report_on_metadata(meta_missing) + if meta_pdf.get('dc:title') == 'Untitled': + # Ghostscript likes to set title to Untitled if omitted from input. + # Reverse this, because PDF/A TechNote 0003:Metadata in PDF/A-1 + # and the XMP Spec do not make this recommendation. + if 'dc:title' not in meta_original: + del meta_pdf['dc:title'] + # If the user explicitly specified an empty string for any of the + # following, they should be unset and not reported as missing in + # the output pdf. Note that some metadata fields use differing names + # between PDF-A and PDF. + for meta in [meta_pdf, meta_original]: + if options.title == '' and 'dc:title' in meta: + del meta['dc:title'] # PDF-A and PDF + if options.author == '': + if 'dc:creator' in meta: + del meta['dc:creator'] # PDF-A (Not xmp:CreatorTool) + if 'pdf:Author' in meta: + del meta['pdf:Author'] # PDF + if options.subject == '': + if 'dc:description' in meta: + del meta['dc:description'] # PDF-A + if 'dc:subject' in meta: + del meta['dc:subject'] # PDF + if options.keywords == '' and 'pdf:Keywords' in meta: + del meta['pdf:Keywords'] # PDF-A and PDF + meta_missing = set(meta_original.keys()) - set(meta_pdf.keys()) + report_on_metadata(meta_missing) optimizing = context.plugin_manager.hook.is_optimization_enabled( context=context