App.find_dir(): Allow return value None

Allow find_dir() to return None in case it couldn't find a directory, that's a legal outcome. Add a boolean parameter "throw" to support throwing an exception if the existence needs to be asserted.

It would probably be nicer for the type checkers to split this up into a throwing and non-throwing function. Postponed.

Signed-off-by: Jan Lindemann <jan@janware.com>
This commit is contained in:
Jan Lindemann 2026-06-01 19:55:43 +02:00
commit f687ded1a6
Signed by: Jan Lindemann
GPG key ID: 3750640C9E25DD61
3 changed files with 27 additions and 18 deletions

View file

@ -456,24 +456,27 @@ class App(Base):
search_subdirs: list[str] = [],
search_absdirs: list[str] = [],
pretty: bool = True,
) -> str:
throw: bool = False,
) -> str | None:
ret = self.__find_dir(name, search_subdirs, search_absdirs, pretty)
if ret is None:
msg = f'Failed to find directory for "{name}"'
log(ERR, msg)
for search_name, search in [
('subdirs', search_subdirs),
('absdirs', search_absdirs),
]:
if search:
log(ERR, f' Searched {search_name} in:')
for d in search:
log(ERR, f' - {d}')
raise FileNotFoundError(msg)
return ret
if ret is not None:
return ret
if not throw:
return None
msg = f'Failed to find directory for "{name}":'
log(ERR, msg)
for search_name, search in [
('subdirs', search_subdirs),
('absdirs', search_absdirs),
]:
if search:
log(ERR, f'Searched {search_name}:')
for d in search:
log(ERR, f' - {d}')
raise FileNotFoundError(msg)
# TODO: add support for customizing this in project.conf
def htdocs_dir(self, project: str) -> str:
def htdocs_dir(self, project: str) -> str | None:
return self.find_dir(
project,
['/src/html/htdocs', '/tools/html/htdocs', '/htdocs'],
@ -481,7 +484,7 @@ class App(Base):
)
# TODO: add support for customizing this in project.conf
def tmpl_dir(self, name: str) -> str:
def tmpl_dir(self, name: str) -> str | None:
return self.find_dir(name, ['/tmpl'], ['/opt/' + name + '/share/tmpl'])
def strip_module_from_spec(self, mod):