Skip to content

Commit

Permalink
Reformat multi-line function calls
Browse files Browse the repository at this point in the history
  • Loading branch information
mportesdev committed Dec 4, 2023
1 parent 24b2392 commit dae39e1
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 129 deletions.
11 changes: 9 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ Example:
from pictureshow import PictureShow
pictures = PictureShow('pics/cucumber.jpg', 'pics/onion.jpg')
pictures = PictureShow(
'pics/cucumber.jpg',
'pics/onion.jpg',
)
pictures.save_pdf('vegetables.pdf')
The keyword parameters of the ``save_pdf`` method and their default values
Expand Down Expand Up @@ -136,7 +139,11 @@ Example:
from pictureshow import pictures_to_pdf
pictures_to_pdf('pics/cucumber.jpg', 'pics/onion.jpg', output_file='vegetables.pdf')
pictures_to_pdf(
'pics/cucumber.jpg',
'pics/onion.jpg',
output_file='vegetables.pdf',
)
(Please note that contrary to the ``PictureShow.save_pdf`` method, ``output_file``
must be specified as a keyword argument in the above example, because the
Expand Down
138 changes: 94 additions & 44 deletions src/pictureshow/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,93 @@


def get_args(parser):
parser.add_argument('pictures', nargs='+', metavar='PICTURE',
help='one or more picture paths or URLs')
parser.add_argument('-a', '--fill-area', action='store_true',
help="fill drawing area with picture, ignoring the picture's"
" aspect ratio")
parser.add_argument('-f', '--force-overwrite', action='store_true',
help='save to output filename even if file exists')
parser.add_argument('-F', '--fail', choices=('skipped', 'no-output', 'no'),
default='no-output', metavar='MODE',
help='If set to `skipped`, fail (exit with code 2) if at least '
'one file was skipped due to an error. '
'If set to `no-output` (default), fail if all files were '
'skipped and no PDF file was created; succeed (exit with '
'code 0) if at least one file was successfully saved. '
'If set to `no`, succeed even if all files were '
'skipped.')
parser.add_argument('-L', '--landscape', action='store_true',
help='set landscape orientation of page; default is portrait')
parser.add_argument('-l', '--layout', default='1x1',
help='specify grid layout (columns x rows) of pictures on page,'
' e.g. 2x3 or 2,3; default is 1x1')
parser.add_argument('-m', '--margin', type=float, default=72,
help='set width of empty space around drawing areas;'
' default is 72 (72 points = 1 inch)')
parser.add_argument('-o', '--output-file', required=True, metavar='PATH',
help='path of the output PDF file (required)')
parser.add_argument('-p', '--page-size', choices=PAGE_SIZES, default='A4',
metavar='SIZE',
help=f'specify page size; default is A4'
f' (available sizes: {", ".join(PAGE_SIZES)})')
parser.add_argument(
'pictures',
nargs='+',
metavar='PICTURE',
help='one or more picture paths or URLs',
)
parser.add_argument(
'-a',
'--fill-area',
action='store_true',
help="fill drawing area with picture, ignoring the picture's aspect ratio",
)
parser.add_argument(
'-f',
'--force-overwrite',
action='store_true',
help='save to output filename even if file exists',
)
parser.add_argument(
'-F',
'--fail',
choices=('skipped', 'no-output', 'no'),
default='no-output',
metavar='MODE',
help='If set to `skipped`, fail (exit with code 2) if at least one file '
'was skipped due to an error. '
'If set to `no-output` (default), fail if all files were skipped '
'and no PDF file was created; succeed (exit with code 0) if at '
'least one file was successfully saved. '
'If set to `no`, succeed even if all files were skipped.',
)
parser.add_argument(
'-L',
'--landscape',
action='store_true',
help='set landscape orientation of page; default is portrait',
)
parser.add_argument(
'-l',
'--layout',
default='1x1',
help='specify grid layout (columns x rows) of pictures on page, '
'e.g. 2x3 or 2,3; default is 1x1',
)
parser.add_argument(
'-m',
'--margin',
type=float,
default=72,
help='set width of empty space around drawing areas; '
'default is 72 (72 points = 1 inch)',
)
parser.add_argument(
'-o',
'--output-file',
required=True,
metavar='PATH',
help='path of the output PDF file (required)',
)
parser.add_argument(
'-p',
'--page-size',
choices=PAGE_SIZES,
default='A4',
metavar='SIZE',
help='specify page size; default is A4 '
f'(available sizes: {", ".join(PAGE_SIZES)})',
)
verbosity_group = parser.add_mutually_exclusive_group()
verbosity_group.add_argument('-q', '--quiet', action='store_true',
help='suppress printing to stdout')
parser.add_argument('-s', '--stretch-small', action='store_true',
help='scale small pictures up to fit drawing areas')
verbosity_group.add_argument('-v', '--verbose', action='store_true',
help='show details on files skipped due to error')
verbosity_group.add_argument(
'-q',
'--quiet',
action='store_true',
help='suppress printing to stdout',
)
parser.add_argument(
'-s',
'--stretch-small',
action='store_true',
help='scale small pictures up to fit drawing areas',
)
verbosity_group.add_argument(
'-v',
'--verbose',
action='store_true',
help='show details on files skipped due to error',
)
parser.add_argument('-V', '--version', action='version')

return parser.parse_args()
Expand Down Expand Up @@ -105,14 +155,14 @@ def main():
try:
pic_show = PictureShow(*args.pictures)
for ok_flag in pic_show._save_pdf(
output_file=output_file,
page_size=args.page_size,
landscape=args.landscape,
margin=args.margin,
layout=args.layout,
stretch_small=args.stretch_small,
fill_area=args.fill_area,
force_overwrite=args.force_overwrite
output_file=output_file,
page_size=args.page_size,
landscape=args.landscape,
margin=args.margin,
layout=args.layout,
stretch_small=args.stretch_small,
fill_area=args.fill_area,
force_overwrite=args.force_overwrite,
):
print('.' if ok_flag else '!', end='', flush=True)
print()
Expand Down
22 changes: 17 additions & 5 deletions src/pictureshow/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,14 @@ def save_pdf(
`num_pages` - number of pages of the resulting PDF document
"""
for _ in self._save_pdf(
output_file, page_size, landscape, margin, layout, stretch_small,
fill_area, force_overwrite
output_file,
page_size,
landscape,
margin,
layout,
stretch_small,
fill_area,
force_overwrite,
):
pass
return self.result
Expand Down Expand Up @@ -88,7 +94,7 @@ def _save_pdf(
self._backend.get_picture_size(picture),
area[2:], # short for (area.width, area.height)
stretch_small,
fill_area
fill_area,
)
self._backend.add_picture(
picture, area.x + x, area.y + y, pic_width, pic_height
Expand Down Expand Up @@ -228,6 +234,12 @@ def pictures_to_pdf(
pic_show = PictureShow(*pic_files)

return pic_show.save_pdf(
output_file, page_size, landscape, margin, layout, stretch_small, fill_area,
force_overwrite
output_file,
page_size,
landscape,
margin,
layout,
stretch_small,
fill_area,
force_overwrite,
)
32 changes: 16 additions & 16 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@
@pytest.mark.parametrize(
'number, noun, expected',
(
pytest.param(1, 'file', '1 file', id='1 file'),
pytest.param(2, 'file', '2 files', id='2 files'),
pytest.param(1, 'picture', '1 picture', id='1 picture'),
pytest.param(3, 'page', '3 pages', id='3 pages'),
)
pytest.param(1, 'file', '1 file', id='1 file'),
pytest.param(2, 'file', '2 files', id='2 files'),
pytest.param(1, 'picture', '1 picture', id='1 picture'),
pytest.param(3, 'page', '3 pages', id='3 pages'),
),
)
def test_number(number, noun, expected):
assert _number(number, noun) == expected
Expand All @@ -38,10 +38,10 @@ def test_number(number, noun, expected):
@pytest.mark.parametrize(
'path, expected',
(
pytest.param('pics', 'pics.pdf', id='no suffix'),
pytest.param('pics.pdf', 'pics.pdf', id='.pdf suffix'),
pytest.param('pics.pics', 'pics.pics', id='other suffix'),
)
pytest.param('pics', 'pics.pdf', id='no suffix'),
pytest.param('pics.pdf', 'pics.pdf', id='.pdf suffix'),
pytest.param('pics.pics', 'pics.pics', id='other suffix'),
),
)
def test_ensure_suffix(path, expected):
assert _ensure_suffix(path) == expected
Expand All @@ -59,7 +59,7 @@ class TestCommandLine:
(
pytest.param(PICS_1_GOOD, 1, '.', '1 picture', '1 page', id='1 valid'),
pytest.param(PICS_2_GOOD, 2, '..', '2 pictures', '2 pages', id='2 valid'),
)
),
)
def test_valid_input(self, new_pdf, pic_files, num_pages, progress, pics, pages):
pic_files = ' '.join(str(path) for path in pic_files)
Expand Down Expand Up @@ -94,7 +94,7 @@ def test_valid_and_invalid_input(self, new_pdf):
pytest.param(PICS_2_BAD, '!!', '2 files', id='2 invalid'),
pytest.param(PICS_DIR, '!', '1 file', id='dir'),
pytest.param(PICS_MISSING, '!', '1 file', id='missing'),
)
),
)
def test_invalid_input(self, new_pdf, pic_files, progress, num_invalid):
pic_files = ' '.join(str(path) for path in pic_files)
Expand Down Expand Up @@ -125,7 +125,7 @@ def test_high_margin_throws_error(self, new_pdf):
pytest.param('1x3', 2, '2 pages', id='1x3'),
pytest.param('3,2', 1, '1 page', id='3,2'),
pytest.param('1,1', 6, '6 pages', id='1,1'),
)
),
)
def test_multiple_pictures_layout(self, new_pdf, layout, num_pages, pages):
# 6 pictures
Expand All @@ -144,7 +144,7 @@ def test_multiple_pictures_layout(self, new_pdf, layout, num_pages, pages):
(
pytest.param('1', id='invalid format'),
pytest.param('0x1', id='invalid value'),
)
),
)
def test_invalid_layout_throws_error(self, new_pdf, layout):
command = f'pictureshow -l{layout} {PIC_FILE} -o {new_pdf}'
Expand Down Expand Up @@ -279,7 +279,7 @@ class TestFailOnSkippedFiles:
pytest.param(PICS_1_GOOD, 0, id='no skipped'),
pytest.param(PICS_1_GOOD_1_BAD, 2, id='some skipped'),
pytest.param(PICS_1_BAD, 2, id='all skipped'),
)
),
)
def test_skipped(self, new_pdf, pic_files, expected_code):
pic_files = ' '.join(str(path) for path in pic_files)
Expand All @@ -294,7 +294,7 @@ def test_skipped(self, new_pdf, pic_files, expected_code):
pytest.param(PICS_1_GOOD, 0, id='no skipped'),
pytest.param(PICS_1_GOOD_1_BAD, 0, id='some skipped'),
pytest.param(PICS_1_BAD, 2, id='all skipped'),
)
),
)
def test_no_output(self, new_pdf, pic_files, expected_code):
pic_files = ' '.join(str(path) for path in pic_files)
Expand All @@ -309,7 +309,7 @@ def test_no_output(self, new_pdf, pic_files, expected_code):
pytest.param(PICS_1_GOOD, 0, id='no skipped'),
pytest.param(PICS_1_GOOD_1_BAD, 0, id='some skipped'),
pytest.param(PICS_1_BAD, 0, id='all skipped'),
)
),
)
def test_no(self, new_pdf, pic_files, expected_code):
pic_files = ' '.join(str(path) for path in pic_files)
Expand Down
Loading

0 comments on commit dae39e1

Please sign in to comment.