Commit 22e3a95f authored by Stephan Rave's avatar Stephan Rave
Browse files

[config] add require_dependency()

parent e253a08f
......@@ -8,6 +8,19 @@ import platform
import sys
import warnings
from pymor.core.exceptions import DependencyMissing, QtMissing, TorchMissing
def require_dependency(dependency):
dependency = dependency.upper()
if not getattr(config, f'HAVE_{dependency}'):
if dependency == 'QT':
raise QtMissing
elif dependency == 'TORCH':
raise TorchMissing
raise DependencyMissing(dependency)
def _can_import(module):
def _can_import_single(m):
......@@ -59,20 +59,28 @@ class NeuralNetworkTrainingFailed(Exception):
"""Is raised when training of a neural network fails."""
class QtMissing(ImportError):
class DependencyMissing(ImportError):
"""Raised when optional packages are required but are not installed."""
def __init__(self, dependency, msg=None):
self.dependency = dependency
super().__init__(msg or f'optional dependency {dependency} required')
class QtMissing(DependencyMissing):
"""Raise me where having importable Qt bindings is non-optional"""
def __init__(self, msg=None):
msg = msg or 'cannot visualize: import of Qt bindings failed'
super().__init__('QT', msg)
class TorchMissing(ImportError):
class TorchMissing(DependencyMissing):
"""Raise me where having importable torch version is non-optional"""
def __init__(self, msg=None):
msg = msg or 'cannot use neural networks: import of torch failed'
super().__init__('TORCH', msg)
class RuleNotMatchingError(NotImplementedError):
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment