File: debug.py

package info (click to toggle)
ppa-dev-tools 0.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,096 kB
  • sloc: python: 5,069; makefile: 3
file content (44 lines) | stat: -rw-r--r-- 1,150 bytes parent folder | download
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
#!/usr/bin/env python3
# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-

# Author:  Bryce Harrington <bryce@canonical.com>
#
# Copyright (C) 2019 Bryce W. Harrington
#
# Released under GNU GPLv2 or later, read the file 'LICENSE.GPLv2+' for
# more information.

import sys
import pprint
import textwrap

DEBUGGING = False


def dbg(msg, wrap=0, prefix=None, indent=''):
    """Print information if debugging is enabled."""
    if DEBUGGING:
        if type(msg) is str:
            if wrap == 0 and indent != '':
                wrap = 72
            if wrap > 0:
                if prefix is None and len(indent) > 0:
                    prefix = indent
                msg = textwrap.fill(
                    msg,
                    width=wrap,
                    initial_indent=prefix,
                    subsequent_indent=indent)
            sys.stderr.write(f"{msg}\n")
        else:
            pprint.pprint(msg)


def warn(msg):
    """Print warning message to stderr."""
    sys.stderr.write(f"Warning: {msg}\n")


def error(msg):
    """Print error message to stderr."""
    sys.stderr.write(f"Error: {msg}\n")