From f687ded1a6228787ba0501a1a9e96842c9fdae4a Mon Sep 17 00:00:00 2001 From: Jan Lindemann Date: Mon, 1 Jun 2026 19:55:43 +0200 Subject: [PATCH] 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 --- src/python/jw/pkg/App.py | 35 ++++++++++--------- .../jw/pkg/cmds/projects/CmdGetAuthInfo.py | 5 ++- src/python/jw/pkg/cmds/projects/CmdTmplDir.py | 5 ++- 3 files changed, 27 insertions(+), 18 deletions(-) diff --git a/src/python/jw/pkg/App.py b/src/python/jw/pkg/App.py index c748f16e..10d4f8be 100644 --- a/src/python/jw/pkg/App.py +++ b/src/python/jw/pkg/App.py @@ -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): diff --git a/src/python/jw/pkg/cmds/projects/CmdGetAuthInfo.py b/src/python/jw/pkg/cmds/projects/CmdGetAuthInfo.py index bcb5dfe6..1f4a2d79 100644 --- a/src/python/jw/pkg/cmds/projects/CmdGetAuthInfo.py +++ b/src/python/jw/pkg/cmds/projects/CmdGetAuthInfo.py @@ -57,9 +57,12 @@ class CmdGetAuthInfo(Cmd): # export # --- Milk jw-pkg repo jw_pkg_dir = self.app.find_dir('jw-pkg', pretty = False) + if jw_pkg_dir is None: + log(DEBUG, 'Can\'t find jw-pkg directory') + return None if not os.path.isdir(jw_pkg_dir + '/.git'): log(DEBUG, f'jw-pkg directory is not a Git repo: {jw_pkg_dir}') - return + return None git_result = await self.app.exec_context.run( ['git', '-C', jw_pkg_dir, 'remote', '-v'] ) diff --git a/src/python/jw/pkg/cmds/projects/CmdTmplDir.py b/src/python/jw/pkg/cmds/projects/CmdTmplDir.py index ed6d4790..1f2ef5f1 100644 --- a/src/python/jw/pkg/cmds/projects/CmdTmplDir.py +++ b/src/python/jw/pkg/cmds/projects/CmdTmplDir.py @@ -22,5 +22,8 @@ class CmdTmplDir(Cmd): # export async def _run(self, args: Namespace) -> None: r = [] for m in args.module: - r.append(self.app.tmpl_dir(m)) + d = self.app.tmpl_dir(m) + if d is None: + continue + r.append(d) print(' '.join(r))