blob: 4b5eb7fa3e70696f53f3f2c197c44b996001ae18 [file] [log] [blame]
Deepak Panickal9b35cf52014-07-01 17:57:19 +00001""" Utility module to help debug Python scripts
2
Bruce Mitchenera18231a2015-11-05 23:57:33 +00003 --------------------------------------------------------------------------
4 File: utilsDebug.py
Deepak Panickal9b35cf52014-07-01 17:57:19 +00005
Bruce Mitchenera18231a2015-11-05 23:57:33 +00006 Overview: Python module to supply functions to help debug Python
7 scripts.
8 Gotchas: None.
9 Copyright: None.
10 --------------------------------------------------------------------------
Deepak Panickal9b35cf52014-07-01 17:57:19 +000011"""
12
13# Python modules:
Bruce Mitchenera18231a2015-11-05 23:57:33 +000014import sys
Deepak Panickal9b35cf52014-07-01 17:57:19 +000015
16# Third party modules:
17
18# In-house modules:
19
20# Instantiations:
21
22#-----------------------------------------------------------------------------
Bruce Mitchenera18231a2015-11-05 23:57:33 +000023# Details: Class to implement simple stack function trace. Instantiation the
24# class as the first function you want to trace. Example:
25# obj = utilsDebug.CDebugFnVerbose("validate_arguments()")
26# Gotchas: This class will not work in properly in a multi-threaded
27# environment.
28# Authors: Illya Rudkin 28/11/2013.
29# Changes: None.
Deepak Panickal9b35cf52014-07-01 17:57:19 +000030#--
Bruce Mitchenera18231a2015-11-05 23:57:33 +000031class CDebugFnVerbose(object):
32 # Public static properties:
33 bVerboseOn = False # True = turn on function tracing, False = turn off.
Deepak Panickal9b35cf52014-07-01 17:57:19 +000034
Bruce Mitchenera18231a2015-11-05 23:57:33 +000035 # Public:
36 #++------------------------------------------------------------------------
37 # Details: CDebugFnVerbose constructor.
38 # Type: Method.
39 # Args: vstrFnName - (R) Text description i.e. a function name.
40 # Return: None.
41 # Throws: None.
42 #--
43 # CDebugFnVerbose(vstrFnName)
Deepak Panickal9b35cf52014-07-01 17:57:19 +000044
Bruce Mitchenera18231a2015-11-05 23:57:33 +000045 #++------------------------------------------------------------------------
46 # Details: Print out information on the object specified.
47 # Type: Method.
48 # Args: vstrText - (R) Some helper text description.
49 # vObject - (R) Some Python type object.
50 # Return: None.
51 # Throws: None.
52 #--
53 def dump_object(self, vstrText, vObject):
54 if CDebugFnVerbose.bVerboseOn == False:
55 return
56 sys.stdout.write("%d%s> Dp: %s" % (CDebugFnVerbose.__nLevel, self.__get_dots(),
57 vstrText))
58 print(vObject)
59
60 #++------------------------------------------------------------------------
61 # Details: Print out some progress text given by the client.
62 # Type: Method.
63 # Args: vstrText - (R) Some helper text description.
64 # Return: None.
65 # Throws: None.
66 #--
67 def dump_text(self, vstrText):
68 if CDebugFnVerbose.bVerboseOn == False:
69 return
70 print(("%d%s> Dp: %s" % (CDebugFnVerbose.__nLevel, self.__get_dots(),
71 vstrText)))
72
73 # Private methods:
74 def __init__(self, vstrFnName):
75 self.__indent_out(vstrFnName)
76
77 #++------------------------------------------------------------------------
78 # Details: Build an indentation string of dots based on the __nLevel.
79 # Type: Method.
80 # Args: None.
81 # Return: Str - variable length string.
82 # Throws: None.
83 #--
84 def __get_dots(self):
85 return "".join("." for i in range(0, CDebugFnVerbose.__nLevel))
86
87 #++------------------------------------------------------------------------
88 # Details: Build and print out debug verbosity text indicating the function
89 # just exited from.
90 # Type: Method.
91 # Args: None.
92 # Return: None.
93 # Throws: None.
94 #--
95 def __indent_back(self):
96 if CDebugFnVerbose.bVerboseOn:
97 print(("%d%s< fn: %s" % (CDebugFnVerbose.__nLevel, self.__get_dots(),
98 self.__strFnName)))
99 CDebugFnVerbose.__nLevel -= 1
100
101 #++------------------------------------------------------------------------
102 # Details: Build and print out debug verbosity text indicating the function
103 # just entered.
104 # Type: Method.
105 # Args: vstrFnName - (R) Name of the function entered.
106 # Return: None.
107 # Throws: None.
108 #--
109 def __indent_out(self, vstrFnName):
110 CDebugFnVerbose.__nLevel += 1
111 self.__strFnName = vstrFnName
112 if CDebugFnVerbose.bVerboseOn:
113 print(("%d%s> fn: %s" % (CDebugFnVerbose.__nLevel, self.__get_dots(),
114 self.__strFnName)))
115
116 # Private statics attributes:
117 __nLevel = 0 # Indentation level counter
118
119 # Private attributes:
120 __strFnName = ""