Skip to content

Commit 0d7c703

Browse files
committed
Make deep merge commonly available
1 parent c643ceb commit 0d7c703

File tree

3 files changed

+26
-25
lines changed

3 files changed

+26
-25
lines changed

src/pdfbaker/__init__.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
combine_pdfs,
55
compress_pdf,
66
convert_svg_to_pdf,
7+
deep_merge,
78
load_pages,
89
)
910
from .render import (
@@ -21,19 +22,20 @@
2122

2223
__all__ = [
2324
# Common functions
24-
"load_pages",
25-
"compress_pdf",
2625
"combine_pdfs",
26+
"compress_pdf",
2727
"convert_svg_to_pdf",
28+
"deep_merge",
29+
"load_pages",
2830
# Render functions
29-
"process_template_data",
3031
"create_env",
32+
"encode_image",
33+
"encode_images",
3134
"highlight",
32-
"process_style",
33-
"process_text_with_jinja",
3435
"process_list_item_texts",
3536
"process_list_items",
37+
"process_style",
38+
"process_template_data",
39+
"process_text_with_jinja",
3640
"space_bullets",
37-
"encode_image",
38-
"encode_images",
3941
]

src/pdfbaker/__main__.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,10 @@
77

88
import yaml
99

10+
from .common import deep_merge
1011
from .render import create_env
1112

1213

13-
def _deep_merge(base, update):
14-
"""Recursively merge two dictionaries.
15-
16-
Values in update will override those in base, except for dictionaries
17-
which will be merged recursively.
18-
"""
19-
merged = base.copy()
20-
21-
for key, value in update.items():
22-
if key in merged and isinstance(merged[key], dict) and isinstance(value, dict):
23-
merged[key] = _deep_merge(merged[key], value)
24-
else:
25-
merged[key] = value
26-
27-
return merged
28-
29-
3014
def _get_config_path(config_path=None):
3115
"""Get and validate the configuration file path."""
3216
if config_path is None:
@@ -149,7 +133,7 @@ def _process_document(doc_name, doc_path, config, build_dir, dist_dir):
149133

150134
bake_module.process_document(
151135
paths=paths,
152-
config=_deep_merge(config, doc_config),
136+
config=deep_merge(config, doc_config),
153137
jinja_env=create_env(paths["templates_dir"]),
154138
)
155139

src/pdfbaker/common.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@
1313
CAIROSVG_AVAILABLE = False
1414

1515

16+
def deep_merge(base, update):
17+
"""Recursively merge two dictionaries.
18+
19+
Values in update will override those in base, except for dictionaries
20+
which will be merged recursively.
21+
"""
22+
merged = base.copy()
23+
for key, value in update.items():
24+
if key in merged and isinstance(merged[key], dict) and isinstance(value, dict):
25+
merged[key] = deep_merge(merged[key], value)
26+
else:
27+
merged[key] = value
28+
return merged
29+
30+
1631
def load_pages(pages_dir):
1732
"""Load page configurations from a specific subdirectory."""
1833
pages = {}

0 commit comments

Comments
 (0)