|
1 | 1 | from dataclasses import dataclass |
2 | 2 | from pathlib import Path |
3 | | -from typing import Union, TYPE_CHECKING |
| 3 | +from typing import Optional, Union, TYPE_CHECKING |
4 | 4 | import webbrowser |
5 | 5 | import logging |
6 | 6 | from functools import cached_property |
@@ -66,19 +66,22 @@ def __init__(self, image_dir: Union[str, Path]): |
66 | 66 | self._files_cache = {} |
67 | 67 | self._iterdir = image_dir.iterdir() |
68 | 68 |
|
69 | | - def get_image(self, id: str, type: str, sep='_') -> Path: |
70 | | - |
| 69 | + def get_image(self, id: str, type: str, sep='_') -> Optional[Path]: |
71 | 70 | prefix = f'{id}{sep}{type}' |
72 | | - if prefix in self._files_cache: |
73 | | - return self._files_cache[prefix] |
74 | | - else: |
75 | | - for file in self._iterdir: |
76 | | - haystack_prefix = file.name.split(".", 1)[0] |
77 | | - self._files_cache[haystack_prefix] = file |
78 | | - if prefix == haystack_prefix: |
79 | | - return file |
| 71 | + try: |
| 72 | + if prefix in self._files_cache: |
| 73 | + return self._files_cache[prefix] |
| 74 | + else: |
| 75 | + for file in self._iterdir: |
| 76 | + haystack_prefix = file.name.split(".", 1)[0] |
| 77 | + self._files_cache[haystack_prefix] = file |
| 78 | + if prefix == haystack_prefix: |
| 79 | + return file |
| 80 | + return None |
| 81 | + except FileNotFoundError: |
80 | 82 | return None |
81 | 83 |
|
| 84 | + |
82 | 85 | class LibraryItem: |
83 | 86 | """ |
84 | 87 | Base class for all library items. |
|
0 commit comments