Package Products :: Package ZenReports :: Module Utils
[hide private]
[frames] | no frames]

Source Code for Module Products.ZenReports.Utils

 1  ########################################################################### 
 2  # 
 3  # This program is part of Zenoss Core, an open source monitoring platform. 
 4  # Copyright (C) 2007, Zenoss Inc. 
 5  # 
 6  # This program is free software; you can redistribute it and/or modify it 
 7  # under the terms of the GNU General Public License version 2 as published by 
 8  # the Free Software Foundation. 
 9  # 
10  # For complete information please visit: http://www.zenoss.com/oss/ 
11  # 
12  ########################################################################### 
13  import Globals 
14  import zope.interface 
15   
16  from Products.ZenUtils.Utils import convToUnits 
17   
18  UNAVAILABLE = 'N/A' 
19   
20 -def percent(partial, total):
21 if partial is None or total is None: 22 return None 23 if not total: 24 return None 25 return partial * 100 / total
26 27
28 -def percentString(n, decimals=0):
29 if n is None: 30 return UNAVAILABLE 31 return '%*.*f' % (2, decimals, n)
32 33
34 -class Record( object ):
35 zope.interface.implements( zope.interface.Interface ) 36 __allow_access_to_unprotected_subobjects__ = 1 37
38 - def __init__(self, **kw):
39 self.values = kw.copy()
40
41 - def __str__(self):
42 return str(self.values)
43 44
45 - def __repr__(self):
46 return repr(self.values)
47 48
49 - def __getitem__(self, name):
50 return self.values[name]
51
52 - def __getattr__(self, name):
53 return self.values.get(name)
54
55 - def percent(self, partial, total):
56 return percent(partial, total)
57
58 - def percentString(self, n, decimals=0):
59 return percentString(n, decimals)
60
61 - def humanBytes(self, value, scale=1):
62 if value is None: 63 return UNAVAILABLE 64 return convToUnits(value * scale)
65
66 - def humanBits(self, value, scale=1):
67 if value is None: 68 return UNAVAILABLE 69 return convToUnits(value * scale, 1000)
70
71 - def fmt(self, fmt, value):
72 if value is None: 73 return UNAVAILABLE 74 return fmt % value
75