|
|
@@ -749,9 +749,12 @@ def enumeratedir(_dir, created_by_ref): |
|
|
|
os.path.isdir(os.path.join(_dir, x)) ] |
|
|
|
|
|
|
|
class TagCache: |
|
|
|
'''Takes tuples, and stores them in a cache where only |
|
|
|
the most count recent ones are kept.''' |
|
|
|
|
|
|
|
def __init__(self, tags=(), count=10): |
|
|
|
self._cache = dict((x, None) for x in tags) |
|
|
|
self._count = 10 |
|
|
|
self._count = count |
|
|
|
self._modified = False |
|
|
|
|
|
|
|
@property |
|
|
@@ -762,6 +765,9 @@ class TagCache: |
|
|
|
return self._modified |
|
|
|
|
|
|
|
def add(self, tag): |
|
|
|
'''Add a tag (tuple) to the cache, possibly dropping ann older |
|
|
|
tag if necessary.''' |
|
|
|
|
|
|
|
self._modified = True |
|
|
|
|
|
|
|
try: |
|
|
@@ -771,14 +777,19 @@ class TagCache: |
|
|
|
|
|
|
|
self._cache[tag] = None |
|
|
|
|
|
|
|
if len(self._cache) > self._count: |
|
|
|
del self._cache[next(iter(a.keys()))] |
|
|
|
while len(self._cache) > self._count: |
|
|
|
del self._cache[next(iter(self._cache.keys()))] |
|
|
|
|
|
|
|
def tags(self): |
|
|
|
'''Returns the sorted list of tags in the cache.''' |
|
|
|
|
|
|
|
return sorted(self._cache.keys()) |
|
|
|
|
|
|
|
@classmethod |
|
|
|
def load(cls, fname): |
|
|
|
'''Load the cache from fname. Must be previously have saved |
|
|
|
using the store method.''' |
|
|
|
|
|
|
|
try: |
|
|
|
with open(fname, 'rb') as fp: |
|
|
|
cache = _asn1coder.loads(fp.read()) |
|
|
@@ -792,13 +803,66 @@ class TagCache: |
|
|
|
return cls(**cache) |
|
|
|
|
|
|
|
def store(self, fname): |
|
|
|
'''Store the cache to fname. The modified property will |
|
|
|
be cleared after this.''' |
|
|
|
|
|
|
|
self._modified = False |
|
|
|
|
|
|
|
cache = dict(tags=list(self._cache.keys())) |
|
|
|
cache = dict(tags=list(self._cache.keys()), count=self._count) |
|
|
|
|
|
|
|
with open(fname, 'wb') as fp: |
|
|
|
fp.write(_asn1coder.dumps(cache)) |
|
|
|
|
|
|
|
class _TestTagCache(unittest.TestCase): |
|
|
|
def setUp(self): |
|
|
|
d = pathlib.Path(tempfile.mkdtemp()).resolve() |
|
|
|
self.basetempdir = d |
|
|
|
|
|
|
|
self.tempdir = d / 'subdir' |
|
|
|
|
|
|
|
self.tempdir.mkdir() |
|
|
|
|
|
|
|
def tearDown(self): |
|
|
|
shutil.rmtree(self.basetempdir) |
|
|
|
self.tempdir = None |
|
|
|
|
|
|
|
def test_cache(self): |
|
|
|
# test basic functionality |
|
|
|
tc = TagCache(count=2) |
|
|
|
|
|
|
|
self.assertFalse(tc.modified) |
|
|
|
|
|
|
|
tc.add(('foo', 'foo')) |
|
|
|
|
|
|
|
self.assertTrue(tc.modified) |
|
|
|
|
|
|
|
tc.add(('bar', 'bar')) |
|
|
|
|
|
|
|
self.assertEqual(tc.tags(), [ ('bar', 'bar'), ('foo', 'foo') ]) |
|
|
|
|
|
|
|
tc.add(('foo', 'foo')) |
|
|
|
|
|
|
|
tc.add(('baz', 'baz')) |
|
|
|
|
|
|
|
tc.add(('foo', 'foo')) |
|
|
|
|
|
|
|
self.assertEqual(tc.tags(), [ ('baz', 'baz'), ('foo', 'foo') ]) |
|
|
|
|
|
|
|
cachefile = self.tempdir / 'somecache' |
|
|
|
tc.store(cachefile) |
|
|
|
|
|
|
|
self.assertFalse(tc.modified) |
|
|
|
|
|
|
|
ntc = TagCache.load(cachefile) |
|
|
|
|
|
|
|
self.assertFalse(ntc.modified) |
|
|
|
|
|
|
|
self.assertEqual(tc.tags(), [ ('baz', 'baz'), ('foo', 'foo') ]) |
|
|
|
|
|
|
|
ntc.add(('whee', 'whee')) |
|
|
|
|
|
|
|
self.assertEqual(ntc.tags(), [ ('foo', 'foo'), ('whee', 'whee') ]) |
|
|
|
|
|
|
|
def _get_paths(options): |
|
|
|
fnames = ( |
|
|
|
'.medashare_identity.pasn1', |
|
|
@@ -1158,7 +1222,7 @@ def cmd_interactive(options, persona, objstr, cache): |
|
|
|
print('7) Open file.') |
|
|
|
print('8) Turn auto skip %s' % 'off' if autoskip else 'on') |
|
|
|
|
|
|
|
tags = sorted(cache.tags()) |
|
|
|
tags = cache.tags() |
|
|
|
|
|
|
|
for pos, (tag, value) in enumerate(tags): |
|
|
|
print('%s) %s=%s' % (string.ascii_lowercase[pos], tag, value)) |
|
|
|