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

Source Code for Module 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   
15  from Products.ZenUtils.Utils import convToUnits 
16   
17  UNAVAILABLE = 'N/A' 
18   
19 -def percent(partial, total):
20 if partial is None or total is None: 21 return None 22 if not total: 23 return None 24 return partial * 100 / total
25 26
27 -def percentString(n, decimals=0):
28 if n is None: 29 return UNAVAILABLE 30 return '%*.*f' % (2, decimals, n)
31 32 33 from AccessControl import ClassSecurityInfo 34 35
36 -class Record:
37 __allow_access_to_unprotected_subobjects__ = 1 38
39 - def __init__(self, **kw):
40 self.values = kw.copy()
41 42
43 - def __str__(self):
44 return str(self.values)
45 46
47 - def __repr__(self):
48 return repr(self.values)
49 50
51 - def __getitem__(self, name):
52 return self.values[name]
53
54 - def __getattr__(self, name):
55 return self.values.get(name)
56
57 - def percent(self, partial, total):
58 return percent(partial, total)
59
60 - def percentString(self, n, decimals=0):
61 return percentString(n, decimals)
62
63 - def humanBytes(self, value, scale=1):
64 if value is None: 65 return UNAVAILABLE 66 return convToUnits(value * scale)
67
68 - def humanBits(self, value, scale=1):
69 if value is None: 70 return UNAVAILABLE 71 return convToUnits(value * scale, 1000)
72
73 - def fmt(self, fmt, value):
74 if value is None: 75 return UNAVAILABLE 76 return fmt % value
77