Skip to content

Commit e35f3f4

Browse files
committed
Minor tidy-up
1 parent 6630561 commit e35f3f4

5 files changed

Lines changed: 32 additions & 29 deletions

File tree

src/pdfbaker/baker.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121

2222
DEFAULT_CONFIG = {
23+
# Default to directories relative to the config file
2324
"documents_dir": ".",
2425
"pages_dir": "pages",
2526
"templates_dir": "templates",
@@ -59,7 +60,7 @@ def __init__(
5960
self.baker = baker
6061
self.baker.log_debug_section("Loading main configuration: %s", config_file)
6162
super().__init__(base_config, config_file)
62-
self.baker.log_trace(self.pprint())
63+
self.baker.log_trace(self.pretty())
6364
if "documents" not in self:
6465
raise ConfigurationError(
6566
'Key "documents" missing - is this the main configuration file?'

src/pdfbaker/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def render(self) -> dict[str, Any]:
119119
"Check for circular references in your configuration."
120120
)
121121

122-
def pprint(self, max_string=60) -> str:
122+
def pretty(self, max_string=60) -> str:
123123
"""Pretty print a configuration dictionary (for debugging)."""
124124
truncated = _truncate_strings(self, max_string)
125125
return pprint.pformat(truncated, indent=2)

src/pdfbaker/document.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ def __init__(
6161
else:
6262
self.name = config.stem
6363
self.directory = config.parent
64-
self.document.log_trace(self.pprint())
64+
self.document.log_trace(self.pretty())
6565
self.document.log_debug_section(
6666
'Merging document config for "%s"...', self.name
6767
)
6868
super().__init__(base_config, config)
69-
self.document.log_trace(self.pprint())
69+
self.document.log_trace(self.pretty())
7070
self.document.log_debug_subsection("Document config for %s:", self.name)
7171
if "pages" not in self:
7272
raise ConfigurationError(
@@ -81,7 +81,7 @@ def __init__(
8181
self.pages.append(page)
8282
self.build_dir = self.resolve_path(self["build_dir"])
8383
self.dist_dir = self.resolve_path(self["dist_dir"])
84-
self.document.log_trace(self.pprint())
84+
self.document.log_trace(self.pretty())
8585

8686
def __init__(
8787
self,

src/pdfbaker/page.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ def __init__(
3636
"""Initialize page configuration (needs a template)."""
3737
self.page = page
3838
self.page.log_debug_subsection("Loading page config: %s", config)
39-
self.page.log_trace(self.pprint())
39+
self.page.log_trace(self.pretty())
4040
# FIXME: config is usually pages/mypage.yaml
4141
self.name = "TBC"
4242
self.page.log_debug_section('Initializing document "%s"...', self.name)
4343
self.page.log_debug_subsection("Merging config for %s:", self.name)
4444
super().__init__(base_config, config)
45-
self.page.log_trace(self.pprint())
45+
self.page.log_trace(self.pretty())
4646
if "template" not in self:
4747
raise ConfigurationError(
4848
f'Page "{self.name}" in document '

src/pdfbaker/pdf.py

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -175,25 +175,27 @@ def convert_svg_to_pdf(
175175
Raises:
176176
SVGConversionError: If SVG conversion fails, includes the backend used and cause
177177
"""
178-
try:
179-
if backend == "inkscape":
180-
try:
181-
_run_subprocess_logged(
182-
[
183-
"inkscape",
184-
f"--export-filename={pdf_path}",
185-
str(svg_path),
186-
]
187-
)
188-
except subprocess.SubprocessError as exc:
189-
raise SVGConversionError(svg_path, backend, str(exc)) from exc
190-
else:
191-
try:
192-
with open(svg_path, "rb") as svg_file:
193-
svg2pdf(file_obj=svg_file, write_to=str(pdf_path))
194-
except Exception as exc:
195-
raise SVGConversionError(svg_path, backend, str(exc)) from exc
196-
197-
return pdf_path
198-
except Exception as exc:
199-
raise SVGConversionError(svg_path, backend, str(exc)) from exc
178+
if backend == "inkscape":
179+
try:
180+
_run_subprocess_logged(
181+
[
182+
"inkscape",
183+
f"--export-filename={pdf_path}",
184+
str(svg_path),
185+
]
186+
)
187+
except subprocess.SubprocessError as exc:
188+
raise SVGConversionError(svg_path, backend, str(exc)) from exc
189+
else:
190+
if backend != "cairosvg":
191+
logger.warning(
192+
"Unknown svg2pdf backend: %s - falling back to cairosvg",
193+
backend,
194+
)
195+
try:
196+
with open(svg_path, "rb") as svg_file:
197+
svg2pdf(file_obj=svg_file, write_to=str(pdf_path))
198+
except Exception as exc:
199+
raise SVGConversionError(svg_path, backend, str(exc)) from exc
200+
201+
return pdf_path

0 commit comments

Comments
 (0)