Skip to content

Commit 8ed6dac

Browse files
committed
feat: enhance concat_pdfs function with caching and improved key ordering
1 parent 9c5effc commit 8ed6dac

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

pdfdol/util.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ def concat_pdf_files(pdf_filepaths: Iterable[str], save_filepath=DFLT_SAVE_PDF_N
255255
Path(save_filepath).write_bytes(combined_pdf_bytes)
256256

257257

258+
from dol import cache_iter
259+
260+
258261
# TODO: Generalize to allow pdf_source to be a mapping of any keys to pdf bytes (not necessarily filepaths)
259262
def concat_pdfs(
260263
pdf_source: Union[Iterable[bytes], Mapping[str, bytes]],
@@ -289,7 +292,7 @@ def concat_pdfs(
289292
>>> pdf_bytes = concat_pdfs(s, key_order=sorted) # doctest: +SKIP
290293
291294
"""
292-
_inputs = locals()
295+
_inputs = dict(locals())
293296
if isinstance(pdf_source, Mapping):
294297
filter_extensions = kwargs.get(
295298
"filter_pdf_extension", filter_extensions
@@ -301,9 +304,13 @@ def concat_pdfs(
301304
if key_order is not None:
302305
if callable(key_order):
303306
keys = sorted(pdf_source.keys(), key=key_order)
304-
else:
307+
elif isinstance(key_order, bool):
308+
reverse = not key_order
309+
keys = sorted(pdf_source.keys(), reverse=reverse)
310+
elif isinstance(key_order, Iterable):
305311
keys = key_order
306-
pdf_source = {k: pdf_source[k] for k in keys}
312+
pdf_source = cache_iter(pdf_source, keys_cache=keys)
313+
# pdf_source = {k: pdf_source[k] for k in keys}
307314

308315
_pdf_source = wrap_kvs(pdf_source, postget=key_and_bytes_to_pdf_bytes)
309316
pdf_bytes = _pdf_source.values()
@@ -328,7 +335,7 @@ def concat_pdfs(
328335
save_filepath = os.path.join(parent, rootdir_name + ".pdf")
329336
if os.path.isfile(save_filepath):
330337
raise ValueError(
331-
f"File {save_filepath} already exists. Specify your save_filepath"
338+
f"File {save_filepath} already exists. Specify your save_filepath "
332339
"explicitly if you want to overwrite it."
333340
)
334341
else:

0 commit comments

Comments
 (0)