-
Notifications
You must be signed in to change notification settings - Fork 3
/
debug.py
executable file
·123 lines (93 loc) · 2.67 KB
/
debug.py
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
# -*- coding: utf-8 -*-
# Copyright 13-October-2016, 14:51:07
#
# Author : Dulip Withanage , University of Heidelberg
import sys
from termcolor import colored
class Debuggable(object):
def __init__(self, module_name):
self.module_name = module_name
class Debug(object):
def __init__(self):
''' Initialise this debug instance '''
self.debug = False
def enable_debug(self):
"""
Enables debugging. Is generally set by Module initialisation
"""
self.debug = True
def print_debug(self, module, message):
"""
Print debug message
Parameters
----------
module: python module
Returns the name of the module
message: str
message as a string
See Also
--------
print_()
"""
if self.debug:
if not isinstance(message, str):
self.fatal_error(
self, 'A non unicode string was passed to the debugger')
self.print_(module, message)
def print_console(self, module, message):
"""
Print debug message
Parameters
----------
module: python module
Returns the name of the module
message: str
message as a string
See Also
--------
print_()
"""
print(('[{0}] {1}'.format(colored(module.get_module_name(), 'green'), str(message))))
def print_(self, module, message):
"""
Prints a formatted message
Parameters
----------
module: python module
Returns the name of the module
message: str
message as a string
Returns
-------
message :str
Formatted Message , [Module name] message
See Also
--------
module.get_module_name()
"""
print(('[{0}] {1}'.format(module.get_module_name(), str(message))))
def get_module_name(self):
"""
Reads the name of the module for debugging and logging
Returns
-------
name :string
Name of the Module
"""
return 'Debugger'
@staticmethod
def fatal_error(module, message):
"""
Prints a formatted error message and exits
Parameters
----------
module: python module
Returns the name of the module
message: str
Error message
See Also
--------
module.get_module_name()
"""
print(('[{0}] {1} {2}'.format(colored(module.get_module_name(), 'red'),'[FATAL ERROR]', str(message))))
sys.exit(1)