diff --git a/wlauto/core/instrumentation.py b/wlauto/core/instrumentation.py index ad33ab7c..b4dbfa95 100644 --- a/wlauto/core/instrumentation.py +++ b/wlauto/core/instrumentation.py @@ -197,6 +197,18 @@ def is_installed(instrument): return False +def is_enabled(instrument): + if isinstance(instrument, Instrument) or isinstance(instrument, type): + name = instrument.name + else: # assume string + name = instrument + try: + installed_instrument = get_instrument(name) + return installed_instrument.is_enabled + except ValueError: + return False + + failures_detected = False diff --git a/wlauto/instrumentation/__init__.py b/wlauto/instrumentation/__init__.py index 094b8fa6..72db181e 100644 --- a/wlauto/instrumentation/__init__.py +++ b/wlauto/instrumentation/__init__.py @@ -23,5 +23,13 @@ def instrument_is_installed(instrument): return instrumentation.is_installed(instrument) +def instrument_is_enabled(instrument): + """Returns ``True`` if the specified instrument is installed and is currently + enabled, and ``False`` other wise. The insturment maybe specified either + as a name or a subclass (or instance of subclass) of + :class:`wlauto.core.Instrument`.""" + return instrumentation.is_enabled(instrument) + + def clear_instrumentation(): instrumentation.installed = [] diff --git a/wlauto/tests/test_instrumentation.py b/wlauto/tests/test_instrumentation.py index 6dd22689..f7d1b96e 100644 --- a/wlauto/tests/test_instrumentation.py +++ b/wlauto/tests/test_instrumentation.py @@ -21,7 +21,7 @@ from nose.tools import assert_equal, raises, assert_true, assert_false from wlauto import Instrument from wlauto.core import signal, instrumentation -from wlauto.instrumentation import instrument_is_installed, clear_instrumentation +from wlauto.instrumentation import instrument_is_installed, instrument_is_enabled, clear_instrumentation class MockInstrument(Instrument): @@ -178,6 +178,16 @@ class InstrumentationTest(TestCase): assert_equal(instrument2.after, 1) assert_equal(instrument2.result, 1) + def test_check_enabled(self): + instrument = _instantiate(MockInstrument) + instrumentation.install(instrument) + instrumentation.enable(instrument) + assert_true(instrument_is_enabled(instrument)) + assert_true(instrument_is_enabled(instrument.name)) + instrumentation.disable(instrument) + assert_false(instrument_is_enabled(instrument)) + assert_false(instrument_is_enabled(instrument.name)) + def test_local_instrument(self): global counter counter = 0