Skip to content

Commit

Permalink
Ensure always return a list if strict=False
Browse files Browse the repository at this point in the history
This makes it easier for the results to be used for if/else statements
  • Loading branch information
LightArrowsEXE committed Aug 18, 2024
1 parent e8de31c commit 8eaf8cb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
17 changes: 8 additions & 9 deletions lvsfunc/dependency/packages.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import importlib.util
from functools import wraps
from typing import Any, Callable, Literal
from typing import Any, Callable

from vstools import FuncExceptT

Expand All @@ -17,7 +17,7 @@ def check_installed_packages(
packages: list[str] | dict[str, DEP_URL] = [],
strict: bool = True,
func_except: FuncExceptT | None = None
) -> Literal[True] | list[str]:
) -> list[str]:
"""
Check if the given packages are installed.
Expand All @@ -29,19 +29,21 @@ def check_installed_packages(
>>> check_installed_packages({'lvsfunc': 'pip install lvsfunc})
>>> if check_installed_packages(['lvsfunc', 'vstools'], strict=False):
... print('Missing packages!')
:param packages: A list of packages to check for. If a dict is passed,
the values are treated as either a URL or a pip command to download the package.
:param strict: If True, raises an error if any of the packages are missing.
Default: True.
:param func_except: Function returned for custom error handling.
This should only be set by VS package developers.
:return: True if all packages are installed if strict=False, else raises an error.
If strict=False and packages are missing, returns a list of missing packages.
:return: A list of all missing packages if strict=False, else raises an error.
"""

if not packages:
return True
return list[str]()

packages_to_check, formatter = (
(packages.keys(), lambda pkg: f"{pkg} ({packages[pkg]})")
Expand All @@ -52,10 +54,7 @@ def check_installed_packages(
formatter(pkg) for pkg in packages_to_check if importlib.util.find_spec(pkg) is None
]

if not missing:
return True

if not strict:
if not missing or not strict:
return missing

raise MissingPackagesError(func_except or check_installed_packages, missing, reason=f"{strict=}")
Expand Down
17 changes: 8 additions & 9 deletions lvsfunc/dependency/plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from functools import wraps
from typing import Any, Callable, Literal
from typing import Any, Callable

from vstools import FuncExceptT, core

Expand All @@ -16,7 +16,7 @@ def check_installed_plugins(
plugins: list[str] | dict[str, DEP_URL] = [],
strict: bool = True,
func_except: FuncExceptT | None = None
) -> Literal[True] | list[str]:
) -> list[str]:
"""
Check if the given plugins are installed.
Expand All @@ -28,30 +28,29 @@ def check_installed_plugins(
>>> check_installed_plugins({'descale': 'https://github.com/Jaded-Encoding-Thaumaturgy/vapoursynth-descale'})
>>> if check_installed_plugins(['resize', 'descale'], strict=False):
... print('Missing plugins!')
:param plugins: A list of plugins to check for. If a dict is passed,
the values are treated as URLs to download the plugin.
:param strict: If True, raises an error if any of the plugins are missing.
Default: True.
:param func_except: Function returned for custom error handling.
This should only be set by VS package developers.
:return: True if all plugins are installed if strict=False, else raises an error.
If strict=False and plugins are missing, returns a list of missing plugins.
:return: A list of all missing plugins if strict=False, else raises an error.
"""

if not plugins:
return True
return list[str]()

missing = [
f"{plugin} ({plugins[plugin]})" if isinstance(plugins, dict) else plugin
for plugin in (plugins.keys() if isinstance(plugins, dict) else plugins)
if not hasattr(core, plugin)
]

if not missing:
return True

if not strict:
if not missing or not strict:
return missing

raise MissingPluginsError(func_except or check_installed_plugins, missing, reason=f"{strict=}")
Expand Down

0 comments on commit 8eaf8cb

Please sign in to comment.