MCPcopy Create free account
hub / github.com/bitcraze/crazyflie-lib-python / TocCache

Class TocCache

cflib/crazyflie/toccache.py:45–123  ·  view source on GitHub ↗

Access to TOC cache. To turn of the cache functionality don't supply any directories.

Source from the content-addressed store, hash-verified

43
44
45class TocCache():
46 """
47 Access to TOC cache. To turn of the cache functionality
48 don't supply any directories.
49 """
50
51 def __init__(self, ro_cache=None, rw_cache=None):
52 self._cache_files = []
53 if (ro_cache):
54 self._cache_files += glob(ro_cache + '/*.json')
55 if (rw_cache):
56 self._cache_files += glob(rw_cache + '/*.json')
57 if not os.path.exists(rw_cache):
58 os.makedirs(rw_cache)
59
60 self._rw_cache = rw_cache
61
62 def fetch(self, crc):
63 """ Try to get a hit in the cache, return None otherwise """
64 cache_data = None
65 pattern = '%08X.json' % crc
66 hit = None
67
68 for name in self._cache_files:
69 if (name.endswith(pattern)):
70 hit = name
71
72 if (hit):
73 try:
74 cache = open(hit)
75 cache_data = json.load(cache,
76 object_hook=self._decoder)
77 cache.close()
78 except Exception as exp:
79 logger.warning('Error while parsing cache file [%s]:%s',
80 hit, str(exp))
81
82 return cache_data
83
84 def insert(self, crc, toc):
85 """ Save a new cache to file """
86 if self._rw_cache:
87 try:
88 filename = '%s/%08X.json' % (self._rw_cache, crc)
89 cache = open(filename, 'w')
90 cache.write(json.dumps(toc, indent=2,
91 default=self._encoder))
92 cache.close()
93 logger.info('Saved cache to [%s]', filename)
94 self._cache_files += [filename]
95 except Exception as exp:
96 logger.warning('Could not save cache to file [%s]: %s',
97 filename, str(exp))
98 else:
99 logger.warning('Could not save cache, no writable directory')
100
101 def _encoder(self, obj):
102 """ Encode a toc element leaf-node """

Callers 1

__init__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected