From 6e3bc35f74edd5e8f38ee889c9ca3cd69bd61f1e Mon Sep 17 00:00:00 2001 From: Rebecca Breu Date: Sun, 26 May 2024 15:08:45 +0200 Subject: [PATCH] Open images from command line --- CHANGELOG.rst | 5 +++++ beeref/config/settings.py | 10 +++++++--- beeref/view.py | 11 ++++++++--- tests/conftest.py | 2 +- tests/test_view.py | 17 +++++++++++++---- 5 files changed, 34 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 903ad8b..b8beee5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -17,6 +17,11 @@ Added * Added a setting to choose the default arrange method on importing images in batch. (Settings -> Settings -> Images & Items -> Default Arrange Method). +* Added the ability to open image files from command line. If the + first command line arg is a bee file, it will be opened and all + further files will be ignored, as previously. If the first argument + isn't a bee file, all files will be treated as images and inserted + as if opened with "Insert -> Images". Fixed ----- diff --git a/beeref/config/settings.py b/beeref/config/settings.py index c6743de..5f7357f 100644 --- a/beeref/config/settings.py +++ b/beeref/config/settings.py @@ -29,10 +29,14 @@ parser = argparse.ArgumentParser( description=f'{constants.APPNAME_FULL} {constants.VERSION}') parser.add_argument( - 'filename', - nargs='?', + 'filenames', + nargs='*', default=None, - help='Bee file to open') + help=('Bee file or images to open. ' + 'If the first file is a bee file, it will be opened and all ' + 'further files will be ignored. If the first argument isn\'t a ' + 'bee file, all files will be treated as images and inserted as ' + 'if opened with "Insert -> Images".')) parser.add_argument( '--settings-dir', help='settings directory to use instead of default location') diff --git a/beeref/view.py b/beeref/view.py index b54453a..9e40532 100644 --- a/beeref/view.py +++ b/beeref/view.py @@ -82,9 +82,14 @@ def __init__(self, app, parent=None): self.control_target = self self.init_main_controls(main_window=parent) - # Load file given via command line - if commandline_args.filename: - self.open_from_file(commandline_args.filename) + # Load files given via command line + if commandline_args.filenames: + fn = commandline_args.filenames[0] + if os.path.splitext(fn)[1] == '.bee': + self.open_from_file(fn) + else: + self.do_insert_images(commandline_args.filenames) + self.update_window_title() @property diff --git a/tests/conftest.py b/tests/conftest.py index 29413eb..c9d253f 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -30,7 +30,7 @@ def reset_beeref_actions(): def commandline_args(): config_patcher = patch('beeref.view.commandline_args') config_mock = config_patcher.start() - config_mock.filename = None + config_mock.filenames = [] yield config_mock config_patcher.stop() diff --git a/tests/test_view.py b/tests/test_view.py index 0b98563..4cdedf2 100644 --- a/tests/test_view.py +++ b/tests/test_view.py @@ -24,8 +24,8 @@ def test_inits_menu(qapp): @patch('beeref.view.BeeGraphicsView.open_from_file') -def test_init_without_filename(open_file_mock, qapp, commandline_args): - commandline_args.filename = None +def test_init_without_filenames(open_file_mock, qapp, commandline_args): + commandline_args.filenames = None parent = QtWidgets.QMainWindow() view = BeeGraphicsView(qapp, parent) open_file_mock.assert_not_called() @@ -34,14 +34,23 @@ def test_init_without_filename(open_file_mock, qapp, commandline_args): @patch('beeref.view.BeeGraphicsView.open_from_file') -def test_init_with_filename(open_file_mock, qapp, commandline_args): - commandline_args.filename = 'test.bee' +def test_init_with_filenames_beefile(open_file_mock, qapp, commandline_args): + commandline_args.filenames = ['test.bee'] parent = QtWidgets.QMainWindow() view = BeeGraphicsView(qapp, parent) open_file_mock.assert_called_once_with('test.bee') del view +@patch('beeref.view.BeeGraphicsView.do_insert_images') +def test_init_with_filenames_images(insert_img_mock, qapp, commandline_args): + commandline_args.filenames = ['/foo/bar.png', '/foo/baz.jpg'] + parent = QtWidgets.QMainWindow() + view = BeeGraphicsView(qapp, parent) + insert_img_mock.assert_called_once_with(['/foo/bar.png', '/foo/baz.jpg']) + del view + + @patch('beeref.widgets.welcome_overlay.WelcomeOverlay.hide') def test_on_scene_changed_when_items(hide_mock, view): item = BeePixmapItem(QtGui.QImage())