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 or (at your
     8  # option) any later version as published by 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 collections import defaultdict
    17  from Products.ZenUtils.Utils import convToUnits
    18
    19  UNAVAILABLE = 'N/A'
    20
    21 -def percent(partial, total):
    22      if partial is None or total is None:
    23          return None
    24      if not total:
    25          return None
    26      return partial * 100 / total
    27
    28
    29 -def percentString(n, decimals=0):
    30      if n is None:
    31          return UNAVAILABLE
    32      return '%*.*f' % (2, decimals, n)
    33
    34
    35 -class Record( object ):
    36      zope.interface.implements( zope.interface.Interface )
    37      __allow_access_to_unprotected_subobjects__ = 1
    38
    39 -    def __init__(self, **kw):
    40          self.values = kw.copy()
    41
    42 -    def __str__(self):
    43          return str(self.values)
    44
    45
    46 -    def __repr__(self):
    47          return repr(self.values)
    48
    49
    50 -    def __getitem__(self, name):
    51          return self.values[name]
    52
    53 -    def __getattr__(self, name):
    54          return self.values.get(name)
    55
    56 -    def percent(self, partial, total):
    57          return percent(partial, total)
    58
    59 -    def percentString(self, n, decimals=0):
    60          return percentString(n, decimals)
    61
    62 -    def humanBytes(self, value, scale=1, unitstr="B"):
    63          if value is None:
    64              return UNAVAILABLE
    65          return convToUnits(value * scale, unitstr=unitstr)
    66
    67 -    def humanBits(self, value, scale=1, unitstr="b"):
    68          if value is None:
    69              return UNAVAILABLE
    70          return convToUnits(value * scale, 1000, unitstr=unitstr)
    71
    72 -    def fmt(self, fmt, value):
    73          if value is None:
    74              return UNAVAILABLE
    75          return fmt % value
    76
    77
    78 -def nested_defaultdict(n,typ):
    79      """create a nested defaultdict n-levels deep, with type typ leaf values"""
    80      fact = typ
    81      i = 1
    82      while i < n:
    83          fact = lambda f=fact: defaultdict(f)
    84          i += 1
    85      return defaultdict(fact)
    86