aboutsummaryrefslogtreecommitdiffhomepage
path: root/libs/rebulk/debug.py
blob: 3229252e7f1753f58f1effd804f771580d1a550c (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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Debug tools.

Can be configured by changing values of those variable.

DEBUG = False
Enable this variable to activate debug features (like defined_at parameters). It can slow down Rebulk

LOG_LEVEL = 0
Default log level of generated rebulk logs.
"""

import inspect
import logging
import os
from collections import namedtuple


DEBUG = False
LOG_LEVEL = logging.DEBUG


class Frame(namedtuple('Frame', ['lineno', 'package', 'name', 'filename'])):
    """
    Stack frame representation.
    """
    __slots__ = ()

    def __repr__(self):
        return f"{os.path.basename(self.filename)}#L{self.lineno}"


def defined_at():
    """
    Get definition location of a pattern or a match (outside of rebulk package).
    :return:
    :rtype:
    """
    if DEBUG:
        frame = inspect.currentframe()
        while frame:
            try:
                if frame.f_globals['__package__'] != __package__:
                    break
            except KeyError:  # pragma:no cover
                # If package is missing, consider we are in. Workaround for python 3.3.
                break
            frame = frame.f_back
        ret = Frame(frame.f_lineno,
                    frame.f_globals.get('__package__'),
                    frame.f_globals.get('__name__'),
                    frame.f_code.co_filename)
        del frame
        return ret