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

# Source Code for Module Products.ZenUtils.Map

      1  ##############################################################################
      2  #
      3  # Copyright (C) Zenoss, Inc. 2007, all rights reserved.
      4  #
      5  # This content is made available according to terms specified in
      6  # License.zenoss under the directory where your Zenoss product is installed.
      7  #
      8  ##############################################################################
      9
     10
     11  import threading
     12  import time
     13  from functools import wraps
     14
     15  _POP_DEFAULT=object()
     16
     17 -class Timed(object):
     18      "Store elements in a map for the given time"
     19
     20 -    def __init__(self, map, timeout):
     21          self.map = map
     22          self.timeout = timeout
     23          self.lastClean = 0
     24
     25
     26 -    def clean(self, now = None):
     27          "remove old values"
     28          # FIXME O(N) search
     29          if now is None:
     30              now = time.time()
     31          if self.lastClean + self.timeout > now:
     32              return
     33          for k, (v, t) in self.map.items():
     34              if t + self.timeout < now:
     35                  del self.map[k]
     36          self.lastClean = now
     37
     38
     39 -    def get(self, key, default):
     40          now = time.time()
     41          self.clean(now)
     42          v, t = self.map.get(key, (default, None) )
     43          if t is None or t + self.timeout < now:
     44              return default
     45          return v
     46
     47
     48 -    def __getitem__(self, key):
     49          now = time.time()
     50          v, t = self.map[key]
     51          if t + self.timeout < now:
     52              del self.map[key]
     53              raise KeyError
     54          return v
     55
     56
     57 -    def __setitem__(self, key, value):
     58          now = time.time()
     59          self.clean(now)
     60          self.map[key] = (value, now)
     61
     62 -    def __contains__(self, key):
     63          return key in self.map
     64
     65 -    def pop(self, key, default=_POP_DEFAULT):
     66          if default is _POP_DEFAULT:
     67              return self.map.pop(key)
     68          else:
     69              return self.map.pop(key, default)
     70
     71 -    def update(self, d):
     72          now = time.time()
     73          self.clean(now)
     74          for k, v in d.items():
     75              self.map[k] = (v, now)
     76
     77
     78
     79 -def Locked_synchronize(fn):
     80      @wraps(fn)
     81      def _closure(self, *args, **kwargs):
     82          with self.lock:
     83              return fn(self, *args, **kwargs)
     84      return _closure
     85
     86 -class Locked(object):
     87      "Use a simple lock for all read/write access to a map"
     88
     89 -    def __init__(self, map):
     90          self.map = map
     91          self.lock = threading.Lock()
     92
     93      @Locked_synchronize
     94 -    def __contains__(self, key):
     95          return key in self.map
     96
     97 -    def has_key(self, key):
     98          "Deprecated, convert to using 'key in map' form"
     99          return key in self
    100
    101      @Locked_synchronize
    102 -    def pop(self, key, default=_POP_DEFAULT):
    103          if default is _POP_DEFAULT:
    104              return self.map.pop(key)
    105          else:
    106              return self.map.pop(key, default)
    107
    108      @Locked_synchronize
    109 -    def get(self, *args):
    110          if not args:
    111              raise TypeError("get takes at least 1 argument : {0} given".format(len(args)))
    112          return self.map.get(*args[:2])
    113
    114      @Locked_synchronize
    115 -    def __setitem__(self, key, item):
    116          self.map[key] = item
    117
    118      @Locked_synchronize
    119 -    def __getitem__(self, key):
    120          return self.map[key]
    121
    122      @Locked_synchronize
    123 -    def update(self, other):
    124          self.map.update(other)
    125