Skip to content

Commit 855d204

Browse files
committed
Use logging instead of print()
1 parent 74edd92 commit 855d204

2 files changed

Lines changed: 20 additions & 12 deletions

File tree

src/pdfbaker/__main__.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Main entry point for the document generator."""
22

33
import importlib
4+
import logging
45
import os
56
import sys
67
from pathlib import Path
@@ -10,19 +11,22 @@
1011
from .common import deep_merge
1112
from .render import create_env
1213

14+
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
15+
logger = logging.getLogger(__name__)
16+
1317

1418
def _get_config_path(config_path=None):
1519
"""Get and validate the configuration file path."""
1620
if config_path is None:
1721
if len(sys.argv) < 2:
18-
print("Error: Config file path is required")
19-
print("Usage: python -m pdfbaker <config_file_path>")
22+
logger.error("Config file path is required")
23+
logger.error("Usage: python -m pdfbaker <config_file_path>")
2024
return None
2125
config_path = sys.argv[1]
2226

2327
config_path = Path(config_path).resolve()
2428
if not config_path.exists():
25-
print(f"Error: Configuration file not found: {config_path}")
29+
logger.error("Configuration file not found: %s", config_path)
2630
return None
2731

2832
return config_path
@@ -64,17 +68,17 @@ def _get_document_paths(base_dir, documents):
6468
def _validate_document_path(doc_name, doc_path):
6569
"""Validate that a document has all required files."""
6670
if not doc_path.is_dir():
67-
print(f'Warning: Directory missing for document "{doc_name}" ' f"at {doc_path}")
71+
logger.warning('Directory missing for document "%s" at %s', doc_name, doc_path)
6872
return False
6973

7074
bake_path = doc_path / "bake.py"
7175
if not bake_path.exists():
72-
print(f'Warning: bake.py missing for document "{doc_name}"')
76+
logger.warning('bake.py missing for document "%s"', doc_name)
7377
return False
7478

7579
config_yml_path = doc_path / "config.yml"
7680
if not config_yml_path.exists():
77-
print(f'Warning: config.yml missing for document "{doc_name}"')
81+
logger.warning('config.yml missing for document "%s"', doc_name)
7882
return False
7983

8084
return bake_path, config_yml_path
@@ -110,10 +114,10 @@ def _process_document(doc_name, doc_path, config, build_dir, dist_dir):
110114
"""Process an individual document."""
111115
validation_result = _validate_document_path(doc_name, doc_path)
112116
if not validation_result:
113-
print(f'Warning: Document "{doc_name}" at {doc_path} is invalid - skipping')
117+
logger.warning('Document "%s" at %s is invalid - skipping', doc_name, doc_path)
114118
return
115119

116-
print(f'Processing document "{doc_name}" from {doc_path}...')
120+
logger.info('Processing document "%s" from %s...', doc_name, doc_path)
117121
bake_path, config_yml_path = validation_result
118122
bake_module = _load_document_bake_module(doc_name, bake_path)
119123
with open(config_yml_path, encoding="utf-8") as f:
@@ -152,7 +156,7 @@ def main(config_path=None):
152156
for doc_name, doc_path in document_paths.items():
153157
_process_document(doc_name, doc_path, config, build_dir, dist_dir)
154158

155-
print("Done.")
159+
logger.info("Done.")
156160
return 0
157161

158162

src/pdfbaker/common.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
"""Common functionality for document generation."""
22

3+
import logging
34
import subprocess
45

56
import pypdf
67
import yaml
78
from cairosvg import svg2pdf
89

10+
logger = logging.getLogger(__name__)
11+
912

1013
def deep_merge(base, update):
1114
"""Recursively merge two dictionaries.
@@ -70,9 +73,10 @@ def combine_pdfs(pdf_files, output_file):
7073
except KeyError as exc:
7174
# PDF has broken annotations with missing /Subtype
7275
if str(exc) == "'/Subtype'":
73-
print(
74-
f"Warning: PDF {pdf_file} has broken annotations. "
75-
f"Falling back to page-by-page method."
76+
logger.warning(
77+
"PDF %s has broken annotations. "
78+
"Falling back to page-by-page method.",
79+
pdf_file,
7680
)
7781
for page in pdf_reader.pages:
7882
pdf_writer.add_page(page)

0 commit comments

Comments
 (0)