Skip to content

Commit 6630561

Browse files
committed
Make PDFBaker options its own (data)class
1 parent fa1c01f commit 6630561

4 files changed

Lines changed: 37 additions & 20 deletions

File tree

src/pdfbaker/__main__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import click
88

99
from pdfbaker import __version__
10-
from pdfbaker.baker import PDFBaker
10+
from pdfbaker.baker import PDFBaker, PDFBakerOptions
1111
from pdfbaker.errors import PDFBakerError
1212

1313
logger = logging.getLogger(__name__)
@@ -36,6 +36,7 @@ def cli() -> None:
3636
@click.option(
3737
"--debug", is_flag=True, help="Debug mode (implies --verbose and --keep-build)"
3838
)
39+
# pylint: disable=too-many-arguments,too-many-positional-arguments
3940
def bake(
4041
config_file: Path,
4142
quiet: bool,
@@ -50,13 +51,13 @@ def bake(
5051
keep_build = True
5152

5253
try:
53-
baker = PDFBaker(
54-
config_file,
54+
options = PDFBakerOptions(
5555
quiet=quiet,
5656
verbose=verbose,
5757
trace=trace,
5858
keep_build=keep_build,
5959
)
60+
baker = PDFBaker(config_file, options=options)
6061
baker.bake()
6162
return 0
6263
except PDFBakerError as exc:

src/pdfbaker/baker.py

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88

99
import logging
10+
from dataclasses import dataclass
1011
from pathlib import Path
1112
from typing import Any
1213

@@ -15,7 +16,7 @@
1516
from .errors import ConfigurationError
1617
from .logging import TRACE, LoggingMixin
1718

18-
__all__ = ["PDFBaker"]
19+
__all__ = ["PDFBaker", "PDFBakerOptions"]
1920

2021

2122
DEFAULT_CONFIG = {
@@ -28,6 +29,23 @@
2829
}
2930

3031

32+
@dataclass
33+
class PDFBakerOptions:
34+
"""Options for controlling PDFBaker behavior.
35+
36+
Attributes:
37+
quiet: Show errors only
38+
verbose: Show debug information
39+
trace: Show trace information (even more detailed than debug)
40+
keep_build: Keep build artifacts after processing
41+
"""
42+
43+
quiet: bool = False
44+
verbose: bool = False
45+
trace: bool = False
46+
keep_build: bool = False
47+
48+
3149
class PDFBaker(LoggingMixin):
3250
"""Main class for PDF document generation."""
3351

@@ -53,31 +71,27 @@ def __init__(
5371
def __init__(
5472
self,
5573
config_file: Path,
56-
quiet: bool = False,
57-
verbose: bool = False,
58-
trace: bool = False,
59-
keep_build: bool = False,
74+
options: PDFBakerOptions | None = None,
6075
) -> None:
6176
"""Initialize PDFBaker with config file path. Set logging level.
6277
6378
Args:
64-
config_file: Path to config file, document directory is its parent
65-
quiet: Show errors only
66-
verbose: Show debug information
67-
trace: Show trace information (even more detailed than debug)
68-
keep_build: Keep build artifacts
79+
config_file: Path to config file
80+
options: Optional options for logging and build behavior
6981
"""
7082
super().__init__()
83+
options = options or PDFBakerOptions()
84+
7185
logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
72-
if quiet:
86+
if options.quiet:
7387
logging.getLogger().setLevel(logging.ERROR)
74-
elif trace:
88+
elif options.trace:
7589
logging.getLogger().setLevel(TRACE)
76-
elif verbose:
90+
elif options.verbose:
7791
logging.getLogger().setLevel(logging.DEBUG)
7892
else:
7993
logging.getLogger().setLevel(logging.INFO)
80-
self.keep_build = keep_build
94+
self.keep_build = options.keep_build
8195
self.config = self.Configuration(
8296
baker=self,
8397
base_config=DEFAULT_CONFIG,

src/pdfbaker/logging.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
from typing import Any
55

66
TRACE = 5
7-
logging.addLevelName(TRACE, 'TRACE')
7+
logging.addLevelName(TRACE, "TRACE")
8+
89

910
class LoggingMixin:
1011
"""Mixin providing consistent logging functionality across pdfbaker classes."""

tests/test_baker.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import shutil
44
from pathlib import Path
55

6-
from pdfbaker.baker import PDFBaker
6+
from pdfbaker.baker import PDFBaker, PDFBakerOptions
77

88

99
def test_examples() -> None:
@@ -31,5 +31,6 @@ def test_examples() -> None:
3131
f.write(content)
3232

3333
# Run baker
34-
baker = PDFBaker(test_config, quiet=True, keep_build=True)
34+
options = PDFBakerOptions(quiet=True, keep_build=True)
35+
baker = PDFBaker(test_config, options=options)
3536
baker.bake()

0 commit comments

Comments
 (0)