1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
from pathlib import Path
import pytest
import logbook
logbook.StderrHandler().push_application()
@pytest.fixture
def logger():
return logbook.Logger("testlogger")
@pytest.fixture
def active_handler(request, test_handler, activation_strategy):
s = activation_strategy(test_handler)
s.activate()
@request.addfinalizer
def deactivate():
s.deactivate()
return test_handler
@pytest.fixture
def test_handler():
return logbook.TestHandler()
class ActivationStrategy:
def __init__(self, handler):
super().__init__()
self.handler = handler
def activate(self):
raise NotImplementedError() # pragma: no cover
def deactivate(self):
raise NotImplementedError() # pragma: no cover
def __enter__(self):
self.activate()
return self.handler
def __exit__(self, *_):
self.deactivate()
class ContextEnteringStrategy(ActivationStrategy):
def activate(self):
self.handler.__enter__()
def deactivate(self):
self.handler.__exit__(None, None, None)
class PushingStrategy(ActivationStrategy):
def activate(self):
from logbook.concurrency import is_gevent_enabled
if is_gevent_enabled():
self.handler.push_greenlet()
else:
self.handler.push_thread()
def deactivate(self):
from logbook.concurrency import is_gevent_enabled
if is_gevent_enabled():
self.handler.pop_greenlet()
else:
self.handler.pop_thread()
@pytest.fixture(params=[ContextEnteringStrategy, PushingStrategy])
def activation_strategy(request):
return request.param
class CustomPathLike:
def __init__(self, path):
self.path = path
def __fspath__(self):
return self.path
@pytest.fixture(params=[Path, str, CustomPathLike])
def logfile(tmp_path, request):
path = str(tmp_path / "logfile.log")
return request.param(path)
@pytest.fixture
def default_handler(request):
returned = logbook.StderrHandler()
returned.push_application()
request.addfinalizer(returned.pop_application)
return returned
try:
import gevent
except ImportError:
pass
else:
@pytest.fixture(
scope="module", autouse=True, params=[False, True], ids=["nogevent", "gevent"]
)
def gevent(request):
module_name = getattr(request.module, "__name__", "")
if (
not any(s in module_name for s in ("queues", "processors"))
and request.param
):
from logbook.concurrency import _disable_gevent, enable_gevent
enable_gevent()
@request.addfinalizer
def fin():
_disable_gevent()
|