meerschaum.utils.debug

Functions to handle debug statements

  1#! /usr/bin/env python
  2# -*- coding: utf-8 -*-
  3# vim:fenc=utf-8
  4
  5"""
  6Functions to handle debug statements
  7"""
  8
  9from __future__ import annotations
 10from meerschaum.utils.typing import Union, Optional, List
 11
 12def dprint(
 13        msg: str,
 14        leader: bool = True,
 15        package: bool = True,
 16        color: Optional[Union[str, List[str]]] = None,
 17        attrs: Optional[List[str]] = None,
 18        nopretty: bool = False,
 19        _progress: Optional['rich.progress.Progress'] = None,
 20        _task: Optional[int] = None,
 21        **kw
 22    ) -> None:
 23    """Print a debug message."""
 24    if attrs is None:
 25        attrs = []
 26    if not isinstance(color, bool) and not nopretty:
 27        try:
 28            from meerschaum.utils.formatting import CHARSET, ANSI, colored
 29        except Exception as e:
 30            CHARSET, ANSI, colored = 'ascii', False, None
 31        from meerschaum.config._paths import CONFIG_DIR_PATH, PERMANENT_PATCH_DIR_PATH
 32        from meerschaum.config import _config
 33        cf = _config('formatting')
 34        _color = color
 35    else:
 36        CHARSET, ANSI, colored, _color, cf = 'ascii', False, None, None, None
 37
 38    import logging, sys, inspect
 39    logging.basicConfig(format='%(message)s')
 40    log = logging.getLogger(__name__)
 41
 42    parent_frame = inspect.stack()[1][0]
 43    parent_info = inspect.getframeinfo(parent_frame)
 44    parent_lineno = parent_info.lineno
 45    parent_globals = parent_frame.f_globals
 46    parent_package = parent_globals['__name__']
 47    msg = str(msg)
 48    premsg = ""
 49    if package:
 50        premsg = parent_package + ':' + str(parent_lineno) + '\n'
 51    if leader and cf is not None:
 52        try:
 53            debug_leader = cf['formatting']['debug'][CHARSET]['icon'] if cf is not None else ''
 54        except KeyError:
 55            print(
 56                "Failed to load config. " +
 57                "Please delete the following directories and restart Meerschaum:"
 58            )
 59            for p in [CONFIG_DIR_PATH, PERMANENT_PATCH_DIR_PATH]:
 60                print('  - ' + str(p))
 61            debug_leader = ''
 62        premsg = ' ' + debug_leader + ' ' + premsg
 63
 64    if ANSI:
 65        if _color is not None:
 66            if isinstance(_color, str):
 67                _color = [_color]
 68        else:
 69            if cf is not None and not nopretty:
 70                try:
 71                    _color = cf['formatting']['debug']['ansi']['rich'] if cf is not None else {}
 72                except KeyError:
 73                    _color = {}
 74            else:
 75                _color = {}
 76        if colored is not None:
 77            premsg = colored(premsg, **_color)
 78    if _progress is not None:
 79        from meerschaum.utils.packages import import_rich, attempt_import
 80        rich = import_rich()
 81        rich_text = attempt_import('rich.text')
 82        text = rich_text.Text.from_ansi(premsg + msg)
 83        _progress.console.log(text)
 84    else:
 85        print(premsg + msg)
 86
 87
 88def _checkpoint(
 89        _progress: Optional['rich.progress.Progress'] = None,
 90        _task: Optional[int] = None,
 91        _total: Optional[int] = None,
 92        **kw
 93    ) -> None:
 94    """If the `_progress` and `_task` objects are provided, increment the task by one step.
 95    If `_total` is provided, update the total instead.
 96    """
 97    if _progress is not None and _task is not None:
 98        _kw = {'total': _total} if _total is not None else {'advance': 1}
 99        _progress.update(_task, **_kw)
100
101
102def trace(browser: bool = True):
103    """
104    Open a web-based debugger to trace the execution of the program.
105    """
106    from meerschaum.utils.packages import attempt_import
107    heartrate = attempt_import('heartrate')
108    heartrate.trace(files=heartrate.files.all, browser=browser)
def dprint( msg: str, leader: bool = True, package: bool = True, color: Union[str, List[str], NoneType] = None, attrs: Optional[List[str]] = None, nopretty: bool = False, _progress: Optional[rich.progress.Progress] = None, _task: Optional[int] = None, **kw) -> None:
13def dprint(
14        msg: str,
15        leader: bool = True,
16        package: bool = True,
17        color: Optional[Union[str, List[str]]] = None,
18        attrs: Optional[List[str]] = None,
19        nopretty: bool = False,
20        _progress: Optional['rich.progress.Progress'] = None,
21        _task: Optional[int] = None,
22        **kw
23    ) -> None:
24    """Print a debug message."""
25    if attrs is None:
26        attrs = []
27    if not isinstance(color, bool) and not nopretty:
28        try:
29            from meerschaum.utils.formatting import CHARSET, ANSI, colored
30        except Exception as e:
31            CHARSET, ANSI, colored = 'ascii', False, None
32        from meerschaum.config._paths import CONFIG_DIR_PATH, PERMANENT_PATCH_DIR_PATH
33        from meerschaum.config import _config
34        cf = _config('formatting')
35        _color = color
36    else:
37        CHARSET, ANSI, colored, _color, cf = 'ascii', False, None, None, None
38
39    import logging, sys, inspect
40    logging.basicConfig(format='%(message)s')
41    log = logging.getLogger(__name__)
42
43    parent_frame = inspect.stack()[1][0]
44    parent_info = inspect.getframeinfo(parent_frame)
45    parent_lineno = parent_info.lineno
46    parent_globals = parent_frame.f_globals
47    parent_package = parent_globals['__name__']
48    msg = str(msg)
49    premsg = ""
50    if package:
51        premsg = parent_package + ':' + str(parent_lineno) + '\n'
52    if leader and cf is not None:
53        try:
54            debug_leader = cf['formatting']['debug'][CHARSET]['icon'] if cf is not None else ''
55        except KeyError:
56            print(
57                "Failed to load config. " +
58                "Please delete the following directories and restart Meerschaum:"
59            )
60            for p in [CONFIG_DIR_PATH, PERMANENT_PATCH_DIR_PATH]:
61                print('  - ' + str(p))
62            debug_leader = ''
63        premsg = ' ' + debug_leader + ' ' + premsg
64
65    if ANSI:
66        if _color is not None:
67            if isinstance(_color, str):
68                _color = [_color]
69        else:
70            if cf is not None and not nopretty:
71                try:
72                    _color = cf['formatting']['debug']['ansi']['rich'] if cf is not None else {}
73                except KeyError:
74                    _color = {}
75            else:
76                _color = {}
77        if colored is not None:
78            premsg = colored(premsg, **_color)
79    if _progress is not None:
80        from meerschaum.utils.packages import import_rich, attempt_import
81        rich = import_rich()
82        rich_text = attempt_import('rich.text')
83        text = rich_text.Text.from_ansi(premsg + msg)
84        _progress.console.log(text)
85    else:
86        print(premsg + msg)

Print a debug message.

def trace(browser: bool = True):
103def trace(browser: bool = True):
104    """
105    Open a web-based debugger to trace the execution of the program.
106    """
107    from meerschaum.utils.packages import attempt_import
108    heartrate = attempt_import('heartrate')
109    heartrate.trace(files=heartrate.files.all, browser=browser)

Open a web-based debugger to trace the execution of the program.