From f0b3eea4eca84331fb7f33a39c7446080ac1fbe7 Mon Sep 17 00:00:00 2001 From: Heikki Ketoharju Date: Wed, 10 Jan 2024 23:38:14 +0200 Subject: [PATCH] fix: Add gives proper error message when library.toml missing Added also a small helpful message about how to create a library.toml fixes #3 --- src/filesystem_operations/libraryreader.py | 8 +++++++- src/gallerycmd/add/add.py | 5 +++++ .../filesystem_operations/test_libraryreader.py | 4 ++-- test/integration/imagegallery/test_imagegallery.py | 3 ++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/filesystem_operations/libraryreader.py b/src/filesystem_operations/libraryreader.py index 29af2fd..f8f1647 100644 --- a/src/filesystem_operations/libraryreader.py +++ b/src/filesystem_operations/libraryreader.py @@ -2,4 +2,10 @@ def load_library(filename): - return tomli.load(open(filename, "rb")) + try: + return tomli.load(open(filename, "rb")) + except(FileNotFoundError): + raise LibraryFileMissing + +class LibraryFileMissing(Exception): + pass diff --git a/src/gallerycmd/add/add.py b/src/gallerycmd/add/add.py index 1e0154f..75da131 100644 --- a/src/gallerycmd/add/add.py +++ b/src/gallerycmd/add/add.py @@ -1,4 +1,5 @@ import sys +from filesystem_operations.libraryreader import LibraryFileMissing from filesystem_operations.librarysaver import save_library from gallerycmd.parser import subparsers @@ -15,6 +16,10 @@ def main(args): except(FileNotFoundError): print("No such image:", args.filename) exit(1) + except(LibraryFileMissing): + print("\nNo library.toml present!\n") + print(" You can create a new gallery by issuing 'gallery init > library.toml'") + exit(1) def add_image(filename, title="", description="", tags=[]): diff --git a/test/integration/filesystem_operations/test_libraryreader.py b/test/integration/filesystem_operations/test_libraryreader.py index 4b81025..8084e84 100644 --- a/test/integration/filesystem_operations/test_libraryreader.py +++ b/test/integration/filesystem_operations/test_libraryreader.py @@ -1,7 +1,7 @@ import pytest import tomli -from filesystem_operations.libraryreader import load_library +from filesystem_operations.libraryreader import LibraryFileMissing, load_library def test_load_gallery_fails_without_filename(): @@ -9,7 +9,7 @@ def test_load_gallery_fails_without_filename(): load_library() def test_file_not_found(tmp_path): - with pytest.raises(FileNotFoundError): + with pytest.raises(LibraryFileMissing): load_library(tmp_path / "foo") def test_file_not_toml(tmp_path): diff --git a/test/integration/imagegallery/test_imagegallery.py b/test/integration/imagegallery/test_imagegallery.py index baa9dfa..08c2190 100644 --- a/test/integration/imagegallery/test_imagegallery.py +++ b/test/integration/imagegallery/test_imagegallery.py @@ -1,12 +1,13 @@ import pytest from _pytest import monkeypatch +from filesystem_operations.libraryreader import LibraryFileMissing from test.integration.filesystem_helpers import mkdir from Imagegallery import Filetree, LibraryToml, Imagegallery def test_throws_if_no_library_toml(tmp_path, monkeypatch): monkeypatch.chdir(tmp_path) - with pytest.raises(FileNotFoundError): + with pytest.raises(LibraryFileMissing): gallery = Imagegallery.from_disk() def test_works_if_file_present(environment):