config: purge all but the last 10 drawings from the config storage directory

Tuhi is not a permanent storage for drawings, this needs to be handled by the
client(s). To make sure no-one is tempted to use it as permanent storage,
always delete all but the last 10 drawings on startup.

Fixes #45
This commit is contained in:
Peter Hutterer 2018-01-31 10:39:26 +10:00 committed by Benjamin Tissoires
parent 83852701ec
commit 1a25163733
1 changed files with 21 additions and 0 deletions

View File

@ -64,6 +64,8 @@ class TuhiConfig(GObject.Object):
config = configparser.ConfigParser() config = configparser.ConfigParser()
config.read(path) config.read(path)
self._purge_drawings(entry)
assert config['Device']['Address'] == entry.name assert config['Device']['Address'] == entry.name
self._devices[entry.name] = config['Device'] self._devices[entry.name] = config['Device']
@ -133,3 +135,22 @@ class TuhiConfig(GObject.Object):
drawings.append(d) drawings.append(d)
return drawings return drawings
def _purge_drawings(self, directory):
'''Removes all but the most recent 10 files from the config
directory. This is primarily done so that no-one relies on the tuhi
daemon for permanent storage.'''
files = []
with os.scandir(directory) as it:
for entry in it:
if entry.is_file() and entry.name.endswith('.json'):
files.append(entry)
if len(files) <= 10:
return
files.sort(key=lambda e: e.name)
for f in files[:-10]:
logger.debug(f'{directory.name}: purging {f.name}')
os.remove(f)