From f19f6ed8b6ded81d2a1cc9d2fe606f71fcd0e27a Mon Sep 17 00:00:00 2001 From: Antoine Tenart Date: Sat, 11 May 2019 11:20:05 +0200 Subject: Rework the dependency checks to distinguish required/optional ones Rework the dependencies definition to include a 'required' flags, which is passed by the check_dependencies helper to the callers, so that they can distinguish between required and optional dependencies. This help in two ways: - The unit test for the dependencies was now failing when an optional one was missing, due to a previous rework. - Mat2's --check-dependencies was referring to "required dependencies" and was misleading for the user as some of them could be optional. Signed-off-by: Antoine Tenart --- libmat2/__init__.py | 64 +++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 47 insertions(+), 17 deletions(-) (limited to 'libmat2/__init__.py') diff --git a/libmat2/__init__.py b/libmat2/__init__.py index 41c2395..501baaa 100644 --- a/libmat2/__init__.py +++ b/libmat2/__init__.py @@ -30,35 +30,65 @@ UNSUPPORTED_EXTENSIONS = { } DEPENDENCIES = { - 'Cairo': 'cairo', - 'PyGobject': 'gi', - 'GdkPixbuf from PyGobject': 'gi.repository.GdkPixbuf', - 'Poppler from PyGobject': 'gi.repository.Poppler', - 'GLib from PyGobject': 'gi.repository.GLib', - 'Mutagen': 'mutagen', - } + 'Cairo': { + 'module': 'cairo', + 'required': True, + }, + 'PyGobject': { + 'module': 'gi', + 'required': True, + }, + 'GdkPixbuf from PyGobject': { + 'module': 'gi.repository.GdkPixbuf', + 'required': True, + }, + 'Poppler from PyGobject': { + 'module': 'gi.repository.Poppler', + 'required': True, + }, + 'GLib from PyGobject': { + 'module': 'gi.repository.GLib', + 'required': True, + }, + 'Mutagen': { + 'module': 'mutagen', + 'required': True, + }, +} CMD_DEPENDENCIES = { - 'Exiftool': exiftool._get_exiftool_path, - 'Ffmpeg': video._get_ffmpeg_path, - } + 'Exiftool': { + 'cmd': exiftool._get_exiftool_path, + 'required': False, + }, + 'Ffmpeg': { + 'cmd': video._get_ffmpeg_path, + 'required': False, + }, +} -def check_dependencies() -> Dict[str, bool]: +def check_dependencies() -> Dict[str, Dict[str, bool]]: ret = collections.defaultdict(bool) # type: Dict[str, bool] for key, value in DEPENDENCIES.items(): - ret[key] = True + ret[key] = { + 'found': True, + 'required': value['required'], + } try: - importlib.import_module(value) + importlib.import_module(value['module']) except ImportError: # pragma: no cover - ret[key] = False # pragma: no cover + ret[key]['found'] = False for k, v in CMD_DEPENDENCIES.items(): - ret[k] = True + ret[k] = { + 'found': True, + 'required': v['required'], + } try: - v() + v['cmd']() except RuntimeError: # pragma: no cover - ret[k] = False + ret[k]['found'] = False return ret -- cgit v1.3