summaryrefslogtreecommitdiff
path: root/efl/utils/deprecated.pyx
blob: 7b0b4f22b14a0221ba40a907ff01ab7b37e577f1 (plain)
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
# Copyright (C) 2007-2014 various contributors (see AUTHORS)
#
# This file is part of Python-EFL.
#
# Python-EFL is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# Python-EFL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this Python-EFL.  If not, see <http://www.gnu.org/licenses/>.

import traceback
import types
from functools import update_wrapper

from cpython cimport PY_VERSION_HEX, PyUnicode_AsUTF8String

from efl.eina cimport EINA_LOG_DOM_WARN
from efl.utils.logger cimport PY_EFL_LOG_DOMAIN

cdef class DEPRECATED(object):

    def __init__(self, version=None, message=None):
        self.version = version
        self.message = message

    def __call__(self, f):
        wrapper = WRAPPER(f, self.version, self.message)

        assignments = ["__name__", "__doc__"]
        if hasattr(f, "__module__"):
            assignments.append("__module__")
        update_wrapper(wrapper, f, assigned=assignments)

        # Version is required for the deprecated directive

        doc = wrapper.__doc__

        if doc is not None and self.version is not None:
            lines = doc.expandtabs().splitlines()

            indent = 0
            if len(lines) >= 2:
                for line in lines[1:]:
                    stripped = line.lstrip()
                    if stripped:
                        indent = len(line) - len(stripped)
                        break

            wrapper.__doc__ += "\n\n"

            wrapper.__doc__ += indent * " " + ".. deprecated:: %s\n" % (self.version,)

            wrapper.__doc__ += (indent + 4) * " " + "%s\n" % (self.message,)

        return wrapper


class WRAPPER(object):
    def __init__(self, f, version, message):
        self.f = f
        self.version = version
        self.message = message

    def __get__(self, obj, objtype):
        if PY_VERSION_HEX < 0x03000000:
            return types.MethodType(self, obj, objtype)
        else:
            return types.MethodType(self, obj)

    def __call__(self, *args, **kwargs):
        cdef:
            object stack
            tuple caller
            str msg

        stack = traceback.extract_stack()
        caller = stack[-1]
        caller_module, caller_line, caller_name, caller_code = caller
        if caller_code is not None:
            if hasattr(self.f, "__objclass__"):
                msg = "Deprecated method %s of class %s called in %s:%s %s." % \
                    (self.f.__name__, self.f.__objclass__.__name__, caller_module, caller_line, caller_code)
            else:
                msg = "Deprecated function %s called in %s:%s %s." % \
                    (self.f.__name__, caller_module, caller_line, caller_code)
        else:
            msg = "Deprecated function %s called in %s:%s." % \
                (self.f.__name__, caller_module, caller_line)

        if self.message is not None:
            msg += " " + self.message

        msg2 = msg
        if isinstance(msg2, unicode): msg2 = PyUnicode_AsUTF8String(msg2)

        EINA_LOG_DOM_WARN(PY_EFL_LOG_DOMAIN, msg2, NULL)

        return self.f(*args, **kwargs)