File size: 4,302 Bytes
4168af9
 
 
ba7196e
1a85f63
4168af9
 
1a85f63
4168af9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a85f63
 
 
 
 
 
4168af9
 
 
 
 
 
 
6f701a4
4168af9
 
 
 
 
6f701a4
4168af9
 
 
 
 
 
1a85f63
6f701a4
4168af9
 
 
 
 
 
 
 
 
ba7196e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
126
127
128
129
130
131
132
133
134
135
136
137
138
import functools
import warnings

from .error_utils import UnitxtWarning
from .settings_utils import get_constants, get_settings

constants = get_constants()
settings = get_settings()


class DeprecationError(Exception):
    """Custom exception for deprecated versions."""

    pass


def compare_versions(version1, version2):
    """Compare two semantic versioning strings and determine their relationship.

    Parameters:
    - version1 (str): The first version string to compare.
    - version2 (str): The second version string to compare.

    Returns:
    - int: -1 if version1 < version2, 1 if version1 > version2, 0 if equal.

    Example:
    >>> compare_versions("1.2.0", "1.2.3")
    -1
    >>> compare_versions("1.3.0", "1.2.8")
    1
    >>> compare_versions("1.0.0", "1.0.0")
    0
    """
    parts1 = [int(part) for part in version1.split(".")]
    parts2 = [int(part) for part in version2.split(".")]
    length_difference = len(parts1) - len(parts2)
    if length_difference > 0:
        parts2.extend([0] * length_difference)
    elif length_difference < 0:
        parts1.extend([0] * (-length_difference))
    for part1, part2 in zip(parts1, parts2):
        if part1 < part2:
            return -1
        if part1 > part2:
            return 1
    return 0


def depraction_wrapper(obj, version, alt_text):
    """A wrapper function for deprecation handling, issuing warnings or errors based on version comparison.

    Args:
        obj (callable): The object to be wrapped, typically a function or class method.
        version (str): The version at which the object becomes deprecated.
        alt_text (str): Additional text to display, usually suggests an alternative.

    Returns:
        callable: A wrapped version of the original object that checks for deprecation.
    """

    @functools.wraps(obj)
    def wrapper(*args, **kwargs):
        if constants.version < version:
            if settings.default_verbosity in ["debug", "info", "warning"]:
                warnings.warn(
                    f"{obj.__name__} is deprecated.{alt_text}",
                    DeprecationWarning,
                    stacklevel=2,
                )
        elif constants.version >= version:
            raise DeprecationError(f"{obj.__name__} is no longer supported.{alt_text}")
        return obj(*args, **kwargs)

    return wrapper


def deprecation(version, alternative=None, msg=None):
    """Decorator for marking functions or class methods as deprecated.

    Args:
        version (str): The version at which the function or method becomes deprecated.
        alternative (str, optional): Suggested alternative to the deprecated functionality.
        msg (str, optional): Additional message regarding the deprecation reason or alternatives.

    Returns:
        callable: A decorator that can be applied to functions or class methods.
    """

    def decorator(obj):
        alt_text = f" Use {alternative} instead." if alternative is not None else ""
        alt_text += msg if msg is not None else ""
        if callable(obj):
            func = obj
        elif hasattr(obj, "__init__"):
            func = obj.__init__
        else:
            raise ValueError("Unsupported object type for deprecation.")
        return depraction_wrapper(func, version, alt_text)

    return decorator


def init_warning(msg=""):
    # Decorator that raises warning when class is initialized
    def decorator(initiated_class):
        UnitxtWarning(msg)
        return initiated_class

    return decorator


def warn_on_call(warning_type=UserWarning, msg=""):
    def decorator(obj):
        if isinstance(obj, type):
            original_init = obj.__init__

            @functools.wraps(original_init)
            def new_init(self, *args, **kwargs):
                warnings.warn(msg, warning_type, stacklevel=2)
                original_init(self, *args, **kwargs)

            obj.__init__ = new_init
            return obj

        if callable(obj):

            @functools.wraps(obj)
            def wrapper(*args, **kwargs):
                warnings.warn(msg, warning_type, stacklevel=2)
                return obj(*args, **kwargs)

            return wrapper

        raise TypeError("This decorator can only be applied to classes or functions.")

    return decorator