Package Products :: Package ZenUtils :: Module Map |
[hide private]
[frames] | no frames]

# Source Code for Module Products.ZenUtils.Map

      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
     14  import threading
     15  import time
     16  from functools import wraps
     17
     18
     19 -class Timed(object):
     20      "Store elements in a map for the given time"
     21
     22 -    def __init__(self, map, timeout):
     23          self.map = map
     24          self.timeout = timeout
     25          self.lastClean = 0
     26
     27
     28 -    def clean(self, now = None):
     29          "remove old values"
     30          # FIXME O(N) search
     31          if now is None:
     32              now = time.time()
     33          if self.lastClean + self.timeout > now:
     34              return
     35          for k, (v, t) in self.map.items():
     36              if t + self.timeout < now:
     37                  del self.map[k]
     38          self.lastClean = now
     39
     40
     41 -    def get(self, key, default):
     42          now = time.time()
     43          self.clean(now)
     44          v, t = self.map.get(key, (default, None) )
     45          if t is None or t + self.timeout < now:
     46              return default
     47          return v
     48
     49
     50 -    def __getitem__(self, key):
     51          now = time.time()
     52          v, t = self.map[key]
     53          if t + self.timeout < now:
     54              del self.map[key]
     55              raise KeyError
     56          return v
     57
     58
     59 -    def __setitem__(self, key, value):
     60          now = time.time()
     61          self.clean(now)
     62          self.map[key] = (value, now)
     63
     64
     65 -    def update(self, d):
     66          now = time.time()
     67          self.clean(now)
     68          for k, v in d.items():
     69              self.map[k] = (v, now)
     70
     71
     72
     73 -def Locked_synchronize(fn):
     74      @wraps(fn)
     75      def _closure(self, *args, **kwargs):
     76          with self.lock:
     77              return fn(self, *args, **kwargs)
     78      return _closure
     79
     80 -class Locked(object):
     81      "Use a simple lock for all read/write access to a map"
     82
     83 -    def __init__(self, map):
     84          self.map = map
     85          self.lock = threading.Lock()
     86
     87      @Locked_synchronize
     88 -    def __contains__(self, key):
     89          return key in self.map
     90
     91 -    def has_key(self, key):
     92          "Deprecated, convert to using 'key in map' form"
     93          return key in self
     94
     95      @Locked_synchronize
     96 -    def get(self, *args):
     97          if not args:
     98              raise TypeError("get takes at least 1 argument : {0} given".format(len(args)))
     99          return self.map.get(*args[:2])
    100
    101      @Locked_synchronize
    102 -    def __setitem__(self, key, item):
    103          self.map[key] = item
    104
    105      @Locked_synchronize
    106 -    def __getitem__(self, key):
    107          return self.map[key]
    108
    109      @Locked_synchronize
    110 -    def update(self, other):
    111          self.map.update(other)
    112