forked from kofemann/nfstest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbaseobj.py
247 lines (220 loc) · 8.13 KB
/
baseobj.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#===============================================================================
# Copyright 2012 NetApp, Inc. All Rights Reserved,
# contribution by Jorge Mora <[email protected]>
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#===============================================================================
"""
Base object
Base class so objects will inherit the methods which provide the string
representation of the object and methods to change the verbosity of such
string representation. It also includes a simple debug printing and logging
mechanism including methods to change the debug verbosity level and methods
to add debug levels.
"""
import re
import nfstest_config as c
from pprint import pformat,pprint
# Module constants
__author__ = 'Jorge Mora (%s)' % c.NFSTEST_AUTHOR_EMAIL
__version__ = '1.0.1'
__copyright__ = "Copyright (C) 2012 NetApp, Inc."
__license__ = "GPL v2"
# Module variables
_dindent = 0
_dlevel = 0
_rlevel = 0
_logfh = None
# Simple verbose level names
_debug_map = {
'none': 0,
'info': 1, # Display info only
'debug': 0xFF, # Display info and all debug messages 0x02-0x80
'all': 0xFFFFFFFF, # Display all messages
}
# Debug display prefixes
_debug_prefix = {
0x001: 'INFO: ',
}
def _init_debug():
"""Define all debug flags"""
for i in xrange(7):
dbg = 'dbg%d' % (i+1)
_debug_map[dbg] = (2 << i)
_debug_prefix[(2 << i)] = dbg.upper() + ': '
_init_debug()
class BaseObj(object):
"""Base class so objects will inherit the methods which provide the string
representation of the object and a simple debug printing and logging
mechanism.
"""
def __init__(self, *kwts, **kwds):
"""Constructor
Initialize object's private data according to the arguments given
Examples:
# Named arguments
x = BaseObj(a=1, b=2)
# Dictionary argument
x = BaseObj({'a':1, 'b':2})
# Tuple arguments: first for keys and second for the values
x = BaseObj(['a', 'b'], [1, 2])
# All of the above will create an object having two attributes:
x.a = 1 and x.b = 2
"""
keys = None
for item in kwts:
if type(item) == dict:
self.__dict__.update(item)
elif type(item) == list or type(item) == tuple:
if keys is None:
keys = item
else:
self.__dict__.update(zip(keys,item))
keys = None
# Process named arguments: x = BaseObj(a=1, b=2)
self.__dict__.update(kwds)
def __repr__(self):
"""String representation of object
The representation depends on the verbose level set by debug_repr().
If set to 0 the generic object representation is returned, else
the representation of the object includes all object attributes
and their values with proper indentation.
"""
global _rlevel
if _rlevel == 0:
# Return generic object representation
return object.__repr__(self)
# Representation of object with proper indentation
indent = ' ' * 4
out = self.__class__.__name__ + "(\n"
itemlist = getattr(self, '_itemlist', None)
if itemlist is None:
itemlist = sorted(self.__dict__.iterkeys())
for key in itemlist:
if key[0] != '_':
val = self.__dict__.get(key, None)
if val != None:
value = pformat(val)
out += " %s = %s,\n" % (key, value.replace("\n", "\n"+indent))
else:
out += " %s = None,\n" % key
out += ")"
return out
__str__ = __repr__
@staticmethod
def debug_repr(level=None):
"""Return or set verbose level of object's string representation.
When setting the verbose level, return the verbose level before
setting it.
level:
Level of verbosity to set
"""
global _rlevel
ret = _rlevel
if level is not None:
_rlevel = level
return ret
def debug_level(self, level=0):
"""Set debug level mask.
level:
Level to set. This could be a number or a string expression
of names defined by debug_map()
Examples:
# Set level
x.debug_level(0xFF)
# Set level using expression
x.debug_level('all')
x.debug_level('debug ^ 1')
"""
global _dlevel
if type(level) == str:
# Convert named verbose levels to a number
# -- Get a list of all named verbose levels
for item in sorted(set(re.split('\W+', level))):
if len(item) > 0:
if item in _debug_map:
# Replace all occurrences of named verbose level
# to its corresponding numeric value
level = re.sub(r'\b' + item + r'\b', hex(_debug_map[item]), level)
else:
try:
# Find out if verbose is a number
# (decimal, hex, octal, ...)
tmp = int(item, 0)
except:
raise Exception("Unknown debug level [%s]" % item)
# Evaluate the whole expression
_dlevel = eval(level)
else:
# Already a number
_dlevel = level
return _dlevel
@staticmethod
def debug_map(bitmap, name='', disp=''):
"""Add a debug mapping.
Generic debug levels map
<bitmap> <name> <disp prefix>
0x000 'none'
0x001 'info' 'INFO: ' # Display info messages only
0x0FF 'debug' 'DBG: ' # Display info and all debug messages (0x02-0x80)
>0x100 user defined verbose levels
"""
if name:
_debug_map[name] = bitmap
if disp:
_debug_prefix[bitmap] = disp
@staticmethod
def dindent(indent):
"""Set global indentation."""
global _dindent
_dindent = indent
def open_log(self, logfile):
"""Open log file."""
global _logfh
self.close_log()
_logfh = open(logfile, "w")
def close_log(self):
"""Close log file."""
global _logfh
if _logfh != None:
_logfh.close()
_logfh = None
@staticmethod
def write_log(data):
"""Write data to log file."""
if _logfh != None:
_logfh.write(data + "\n")
def dprint(self, level, msg, indent=0):
"""Print debug message if level is allowed by the verbose level
given in debug_level().
"""
ret = ''
if level is None:
return
if type(level) == str:
level = _debug_map[level.lower()]
if level & _dlevel:
# Add display prefix only if msg is not an empty string
if len(msg):
# Find the right display prefix
prefix = ' ' * _dindent
for bitmap in sorted(_debug_prefix):
if level & bitmap:
prefix += _debug_prefix[bitmap]
break
# Add display prefix to the message
sp = ' ' * indent
ret = prefix + sp + msg
indent += len(prefix)
if indent > 0:
sp = ' ' * indent
ret = ret.replace("\n", "\n"+sp)
print ret
self.write_log(ret)