Skip to content

Commit

Permalink
change: Enhance error messages for photos cmd
Browse files Browse the repository at this point in the history
Also, create a helper module that will sport methods for printing out common,
well formatted error messages
  • Loading branch information
Heikki Ketoharju committed Mar 2, 2024
1 parent d98ac19 commit e5a87a7
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/photoscmd/add/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from filesystem_operations.librarysaver import save_library

from photoscmd.parser import subparsers
from photoscmd.errors import print_libary_toml_missing_error
from PhotoLibrary import PhotoLibrary


def main(args):
try:
photolibrary = add_image(args.filename,
Expand All @@ -17,8 +19,7 @@ def main(args):
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'")
print_libary_toml_missing_error()
exit(1)


Expand Down
6 changes: 6 additions & 0 deletions src/photoscmd/edit/edit.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from PhotoLibrary import PhotoLibrary
from filesystem_operations.librarysaver import save_library
from filesystem_operations.libraryreader import LibraryFileMissing

from photoscmd.parser import subparsers
from photoscmd.errors import print_libary_toml_missing_error


def main(args):
Expand All @@ -17,6 +19,10 @@ def main(args):
except(NeedsNamedArgument):
print("You must provide either title, description or tags")
exit(1)
except(LibraryFileMissing):
print_libary_toml_missing_error()
exit(1)


def edit_image(filename, title=None, description=None, tags=None):
photolibrary = PhotoLibrary.from_disk()
Expand Down
3 changes: 3 additions & 0 deletions src/photoscmd/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def print_libary_toml_missing_error():
print("\nNo library.toml present!")
print("You can create a new gallery by issuing 'gallery init > library.toml'")
8 changes: 5 additions & 3 deletions src/photoscmd/list/list.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from photoscmd.parser import subparsers
from PhotoLibrary import PhotoLibrary
from PhotoLibrary.tags import filter_by_tag
from filesystem_operations.libraryreader import LibraryFileMissing
from photoscmd.errors import print_libary_toml_missing_error


def format(photolibrary):
Expand All @@ -26,9 +28,9 @@ def main(args):
try:
photolibrary = PhotoLibrary.from_disk()
photolibrary.flag_missing()
except FileNotFoundError:
print("No library.toml file found in this directory.")
exit(0)
except LibraryFileMissing:
print_libary_toml_missing_error()
exit(1)

formatted = format(filter_by_tag(photolibrary, args.tag))
print("\n".join(formatted))
Expand Down
5 changes: 5 additions & 0 deletions test/unit/list/list_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,23 @@
tags = ['foo']
"""


@pytest.fixture
def simple_photolibrary():
return PhotoLibrary.from_vars(
tomli.loads(singleimage),
None
)


@pytest.fixture
def complex_photolibrary():
return PhotoLibrary.from_vars(
tomli.loads(many_images),
None
)


def test_list_single_image(simple_photolibrary):
formatted = list.format(simple_photolibrary)
assert formatted == [
Expand All @@ -48,6 +51,7 @@ def test_list_single_image(simple_photolibrary):
"Image description",
""]


def test_list_many_images(complex_photolibrary):
formatted = list.format(complex_photolibrary)
assert formatted == [
Expand All @@ -61,6 +65,7 @@ def test_list_many_images(complex_photolibrary):
""
]


def test_missing_file(simple_photolibrary):
simple_photolibrary.metadata["path/to/image1.jpg"]["missing"] = True
formatted = list.format(simple_photolibrary)
Expand Down

0 comments on commit e5a87a7

Please sign in to comment.