Test to see if item is importable, and optionally check against a minimum version. If min_version is given, the default behavior is to check against the `__version__` attribute of the item, but specifying `callback` allows you to extract the value you are interested in. e.g::
(item, min_version=None, callback=extract_version)
| 102 | return mod.__version__ |
| 103 | |
| 104 | def test_for(item, min_version=None, callback=extract_version): |
| 105 | """Test to see if item is importable, and optionally check against a minimum |
| 106 | version. |
| 107 | |
| 108 | If min_version is given, the default behavior is to check against the |
| 109 | `__version__` attribute of the item, but specifying `callback` allows you to |
| 110 | extract the value you are interested in. e.g:: |
| 111 | |
| 112 | In [1]: import sys |
| 113 | |
| 114 | In [2]: from IPython.testing.iptest import test_for |
| 115 | |
| 116 | In [3]: test_for('sys', (2,6), callback=lambda sys: sys.version_info) |
| 117 | Out[3]: True |
| 118 | |
| 119 | """ |
| 120 | try: |
| 121 | check = import_item(item) |
| 122 | except (ImportError, RuntimeError): |
| 123 | # GTK reports Runtime error if it can't be initialized even if it's |
| 124 | # importable. |
| 125 | return False |
| 126 | else: |
| 127 | if min_version: |
| 128 | if callback: |
| 129 | # extra processing step to get version to compare |
| 130 | check = callback(check) |
| 131 | |
| 132 | return check >= min_version |
| 133 | else: |
| 134 | return True |
| 135 | |
| 136 | # Global dict where we can store information on what we have and what we don't |
| 137 | # have available at test run time |