/usr/lib/python3/dist-packages/ginga/misc/Datasrc.py is in python3-ginga 2.6.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | #
# Eric Jeschke (eric@naoj.org)
#
# Copyright (c) Eric R. Jeschke. All rights reserved.
# This is open-source software licensed under a BSD license.
# Please see the file LICENSE.txt for details.
#
import threading
class TimeoutError(Exception):
pass
class Datasrc(object):
"""Class to handle internal data cache."""
def __init__(self, length=0):
self.length = length
self.cursor = -1
self.datums = {}
self.history = []
self.sortedkeys = []
self.cond = threading.Condition()
self.newdata = threading.Event()
def __getitem__(self, key):
with self.cond:
return self.datums[key]
def __setitem__(self, key, value):
self.push(key, value)
def __contains__(self, key):
with self.cond:
return key in self.datums
def has_key(self, key):
with self.cond:
return key in self.datums
def __delitem__(self, key):
self.remove(key)
def __len__(self):
with self.cond:
return len(self.history)
def push(self, key, value):
with self.cond:
if key in self.history:
self.history.remove(key)
self.history.append(key)
self.datums[key] = value
self._eject_old()
self.newdata.set()
self.cond.notify()
def pop_one(self):
return self.remove(self.history[0])
def pop(self, *args):
if len(args) == 0:
return self.remove(self.history[0])
assert len(args) == 1, \
ValueError("Too many parameters to pop()")
return self.remove(args[0])
def remove(self, key):
with self.cond:
val = self.datums[key]
self.history.remove(key)
del self.datums[key]
self.sortedkeys = list(self.datums.keys())
self.sortedkeys.sort()
return val
def _eject_old(self):
# Eject oldest cache unless there is no cache limit
if (self.length is not None) and (self.length > 0):
while len(self.history) > self.length:
oldest = self.history.pop(0)
del self.datums[oldest]
# Update sorted keys regardless
self.sortedkeys = list(self.datums.keys())
self.sortedkeys.sort()
def index(self, key):
with self.cond:
return self.history.index(key)
def index2key(self, index):
with self.cond:
return self.history[index]
def index2value(self, index):
with self.cond:
return self.datums[self.history[index]]
def youngest(self):
return self.datums[self.history[-1]]
def oldest(self):
return self.datums[self.history[0]]
def pop_oldest(self):
return self.pop(self.history[0])
def pop_youngest(self):
return self.pop(self.history[-1])
def keys(self, sort='alpha'):
with self.cond:
if sort == 'alpha':
return self.sortedkeys
elif sort == 'time':
return self.history
else:
return self.datums.keys()
def wait(self, timeout=None):
with self.cond:
self.cond.wait(timeout=timeout)
if not self.newdata.isSet():
raise TimeoutError("Timed out waiting for datum")
self.newdata.clear()
return self.history[-1]
def get_bufsize(self):
with self.cond:
return self.length
def set_bufsize(self, length):
with self.cond:
self.length = length
self._eject_old()
#END
|