Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Define available page sizes in backend class #57

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/pictureshow/backends.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import os

from PIL import UnidentifiedImageError
from reportlab.lib import pagesizes
from reportlab.lib.utils import ImageReader
from reportlab.pdfgen.canvas import Canvas


class ReportlabBackend:
page_sizes = {
name: size
for name, size in vars(pagesizes).items()
if name.isupper() # exclude deprecated names and function names
}

read_errors = (
OSError, # file does not exist or is a dir
UnidentifiedImageError, # file not recognized as picture
Expand Down
6 changes: 3 additions & 3 deletions src/pictureshow/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from platformdirs import user_cache_path

from . import __version__
from .core import PAGE_SIZES, PictureShow
from .core import PictureShow

CACHE_PATH = user_cache_path('pictureshow', ensure_exists=True)
ERROR_LOG = CACHE_PATH / 'errors.log'
Expand Down Expand Up @@ -85,11 +85,11 @@ def _setup_parser():
page_group.add_argument(
'-p',
'--page-size',
choices=PAGE_SIZES,
choices=PictureShow.page_sizes,
default='A4',
metavar='SIZE',
help='specify page size; default is %(default)s '
f'(available sizes: {", ".join(PAGE_SIZES)})',
f'(available sizes: {", ".join(PictureShow.page_sizes)})',
)
page_group.add_argument(
'-L',
Expand Down
23 changes: 9 additions & 14 deletions src/pictureshow/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,9 @@
from pathlib import Path
from typing import NamedTuple

from reportlab.lib import pagesizes

from .backends import ReportlabBackend
from .exceptions import LayoutError, MarginError, PageSizeError, RGBColorError

PAGE_SIZES = {
name: size
for name, size in vars(pagesizes).items()
# use isupper() to exclude deprecated names and function names
if name.isupper()
}

DELIMITER = re.compile('[x,]')


Expand All @@ -37,11 +28,15 @@ def size(self):

_Result = namedtuple('_Result', 'num_ok errors num_pages')

_backend_class = ReportlabBackend


class PictureShow:
page_sizes = _backend_class.page_sizes.copy()

def __init__(self, *pic_files):
self._pic_files = pic_files
self._backend = ReportlabBackend()
self._backend = _backend_class()

def save_pdf(
self,
Expand Down Expand Up @@ -135,15 +130,15 @@ def _validate_target_path(path, force_overwrite):

return path

@staticmethod
def _validate_page_size(page_size, landscape):
@classmethod
def _validate_page_size(cls, page_size, landscape):
if isinstance(page_size, str):
try:
page_size = PAGE_SIZES[page_size.upper()]
page_size = cls.page_sizes[page_size.upper()]
except KeyError as err:
raise PageSizeError(
f'unknown page size {page_size!r},'
f' please use one of: {", ".join(PAGE_SIZES)}'
f' please use one of: {", ".join(cls.page_sizes)}'
) from err

page_size_error = PageSizeError(
Expand Down