1+ """ Decorators for annotating functions for behaviours.
2+
3+ All decorators are defined here and are referenced across the codebase.
4+ Note that some of them reference the bootstrapped environment where
5+ information is discovered from the live Command Centre the instance
6+ is connected to.
7+
8+ See Also:
9+ https://github.com/anomaly/gallagher/issues/69
10+ """
11+
12+ def deprecated (func ):
13+ """Decorator to indicate that a function is deprecated.
14+
15+ This decorator is used to indicate that a function is deprecated and
16+ should not be used. It will raise a warning when the function is called.
17+
18+ Args:
19+ func (Callable): The function to be deprecated.
20+
21+ Returns:
22+ Callable: The wrapped function.
23+
24+ Examples:
25+ >>> @deprecated
26+ ... def old_function():
27+ ... pass
28+
29+ """
30+ import warnings
31+ import functools
32+
33+ @functools .wraps (func )
34+ def wrapper (* args , ** kwargs ):
35+ warnings .warn (
36+ f"Call to deprecated function { func .__name__ } ." ,
37+ category = DeprecationWarning
38+ )
39+ return func (* args , ** kwargs )
40+
41+ return wrapper
42+
43+
44+ def available (version_string ):
45+ """Decorator to indicate that a function is available in a specific version.
46+
47+ This decorator is used to indicate that a function is available in a specific
48+ version of the Command Centre API. It will raise an error if the function is
49+ called in a version that does not support it.
50+
51+ Args:
52+ version_string (str): The version string the function is available in.
53+
54+ Returns:
55+ Callable: The wrapped function.
56+
57+ Examples:
58+ >>> @available("1.0.0")
59+ ... def new_function():
60+ ... pass
61+
62+ """
63+ import functools
64+
65+ def decorator (func ):
66+ @functools .wraps (func )
67+ def wrapper (* args , ** kwargs ):
68+ from .cc .core import Capabilities
69+
70+ if Capabilities .CURRENT .version < version_string :
71+ raise RuntimeError (
72+ f"Function { func .__name__ } is not available in version { Capabilities .CURRENT .version } ."
73+ )
74+
75+ return func (* args , ** kwargs )
76+
77+ return wrapper
78+
79+ return decorator
0 commit comments