Skip to content

Commit

Permalink
added remove_cache_table_files function
Browse files Browse the repository at this point in the history
  • Loading branch information
k1o0 committed Oct 30, 2024
1 parent 85b4e1a commit 6b89bd1
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ This version fixes issues with Alyx authentication in silent mode, and improves
### Added

- one.alf.exceptions.ALFWarning category allows users to filter warnings relating to mixed revisions
- one.alf.cache.remove_cache_table_files and One._remove_cache_table_files for deleting cache table files

## [2.9.1]

Expand Down
31 changes: 30 additions & 1 deletion one/alf/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@
from one.converters import session_record2path
from one.util import QC_TYPE, patch_cache

__all__ = ['make_parquet_db', 'remove_missing_datasets', 'DATASETS_COLUMNS', 'SESSIONS_COLUMNS']
__all__ = [
'make_parquet_db', 'remove_missing_datasets', 'remove_cache_table_files',
'DATASETS_COLUMNS', 'SESSIONS_COLUMNS']
_logger = logging.getLogger(__name__)

# -------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -338,3 +340,30 @@ def remove_missing_datasets(cache_dir, tables=None, remove_empty_sessions=True,
path = path.parent

return sorted(to_delete)


def remove_cache_table_files(folder, tables=('sessions', 'datasets')):
"""Delete cache tables on disk.
Parameters
----------
folder : pathlib.Path
The directory path containing cache tables to remove.
tables : list of str
A list of table names to remove, e.g. ['sessions', 'datasets'].
NB: This will also delete the cache_info.json metadata file.
Returns
-------
list of pathlib.Path
A list of the removed files.
"""
filenames = ('cache_info.json', *(f'{t}.pqt' for t in tables))
removed = []
for file in map(folder.joinpath, filenames):
if file.exists():
file.unlink()
removed.append(file)
else:
_logger.warning('%s not found', file)
return removed
21 changes: 20 additions & 1 deletion one/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
import one.alf.io as alfio
import one.alf.files as alfiles
import one.alf.exceptions as alferr
from .alf.cache import make_parquet_db, DATASETS_COLUMNS, SESSIONS_COLUMNS
from .alf.cache import (
make_parquet_db, remove_cache_table_files, DATASETS_COLUMNS, SESSIONS_COLUMNS)
from .alf.spec import is_uuid_string, QC, to_alf
from . import __version__
from one.converters import ConversionMixin, session_record2path
Expand Down Expand Up @@ -111,6 +112,24 @@ def _reset_cache(self):
'raw': {}} # map of original table metadata
})

def _remove_cache_table_files(self, tables=None):
"""Delete cache tables on disk.
Parameters
----------
tables : list of str
A list of table names to removes, e.g. ['sessions', 'datasets'].
If None, the currently loaded table names are removed. NB: This
will also delete the cache_info.json metadata file.
Returns
-------
list of pathlib.Path
A list of the removed files.
"""
tables = tables or filter(lambda x: x[0] != '_', self._cache)
return remove_cache_table_files(self._tables_dir, tables)

def load_cache(self, tables_dir=None, **kwargs):
"""
Load parquet cache files from a local directory.
Expand Down
14 changes: 14 additions & 0 deletions one/tests/test_one.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@

class TestONECache(unittest.TestCase):
"""Test methods that use sessions and datasets tables.
This class loads the parquet tables from the fixtures and builds a file tree in a temp folder
"""
tempdir = None
Expand Down Expand Up @@ -1025,6 +1026,19 @@ def test_save_loaded_ids(self):
self.assertEqual(ids, [])
self.assertIsNone(filename)

def test_remove_cache_table_files(self):
"""Test One._remove_cache_table_files method."""
root = self.one._tables_dir
for name in ('cache_info.json', 'foo.pqt'):
root.joinpath(name).touch()
removed = self.one._remove_cache_table_files()
expected = ['sessions.pqt', 'datasets.pqt', 'cache_info.json']
self.assertCountEqual(expected, [str(x.relative_to(root)) for x in removed])
with self.assertLogs('one.alf.cache', 30) as cm:
removed = self.one._remove_cache_table_files(tables=('foo',))
self.assertEqual([root / 'foo.pqt'], removed)
self.assertIn('cache_info.json not found', cm.records[0].message)


@unittest.skipIf(OFFLINE_ONLY, 'online only test')
class TestOneAlyx(unittest.TestCase):
Expand Down

0 comments on commit 6b89bd1

Please sign in to comment.