Commit 8bfb8ae6 authored by Samuel GAIST's avatar Samuel GAIST

[data] Add reset method to DataSource classes

This allows to reset the state of a data source.

Currently only used by the CachedDataSource
parent c4c9edca
......@@ -209,6 +209,14 @@ class DataSource(object):
def close(self):
self.infos = []
def reset(self):
"""Reset the state of the data source
This shall only clear the current state, not require a new call
to setup the source.
"""
pass
def __del__(self):
"""Makes sure all resources are released when the object is deleted"""
self.close()
......@@ -467,11 +475,18 @@ class CachedDataSource(DataSource):
return True
def close(self):
if self.current_file is not None:
self.current_file.close()
self.reset()
super(CachedDataSource, self).close()
def reset(self):
"""Rest the current state"""
if self.current_file is not None:
self.current_file.close()
self.current_file = None
self.current_file_index = None
def __getitem__(self, index):
"""Retrieve a block of data
......
......@@ -348,6 +348,18 @@ class TestCachedDataSource(TestCachedDataBase):
self.check_valid_data_indices(cached_file)
self.check_invalid_indices(cached_file)
def test_reset(self):
self.writeData("user/single_integer/1", 0, 9)
cached_source = CachedDataSource()
cached_source.setup(self.filename, prefix)
_, _, _ = cached_source[0]
self.assertIsNotNone(cached_source.current_file_index)
self.assertIsNotNone(cached_source.current_file)
cached_source.reset()
self.assertIsNone(cached_source.current_file_index)
self.assertIsNone(cached_source.current_file)
# ----------------------------------------------------------
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment