Skip to content

Commit c90f623

Browse files
authored
Merge pull request #107 from sappelhoff/depr
deprecate resolve_stream
2 parents 28d969c + a04ebbd commit c90f623

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

src/pylsl/resolve.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from .lib import lib
44
from .info import StreamInfo
5-
from .util import FOREVER
5+
from .util import deprecated, FOREVER
66

77

88
def resolve_streams(wait_time=1.0):
@@ -166,7 +166,12 @@ def results(self):
166166
return [StreamInfo(handle=buffer[k]) for k in range(num_found)]
167167

168168

169+
@deprecated("Use `resolve_streams` instead.")
169170
def resolve_stream(*args):
171+
"""Resolve a stream.
172+
173+
This function is deprecated. Use `resolve_streams` instead.
174+
"""
170175
if len(args) == 0:
171176
return resolve_streams()
172177
elif type(args[0]) in [int, float]:

src/pylsl/util.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import ctypes
2+
import functools
3+
import warnings
24

35
from .lib import lib
46

@@ -108,3 +110,34 @@ def handle_error(errcode):
108110
raise InternalError("an internal error has occurred.")
109111
elif errcode < 0:
110112
raise RuntimeError("an unknown error has occurred.")
113+
114+
115+
def deprecated(reason: str = None):
116+
"""Mark functions as deprecated.
117+
118+
It will result in a warning being emitted when the function is used.
119+
120+
Example:
121+
@deprecated("use new_function instead")
122+
def old_function():
123+
pass
124+
"""
125+
def decorator(func):
126+
message = f"Function '{func.__name__}' is deprecated."
127+
if reason:
128+
message += f" {reason}"
129+
130+
@functools.wraps(func)
131+
def wrapper(*args, **kwargs):
132+
warnings.simplefilter("always", DeprecationWarning) # ensure it shows up
133+
warnings.warn(
134+
message,
135+
category=DeprecationWarning,
136+
stacklevel=2
137+
)
138+
warnings.simplefilter("default", DeprecationWarning)
139+
return func(*args, **kwargs)
140+
141+
return wrapper
142+
143+
return decorator

0 commit comments

Comments
 (0)