Skip to content

Commit

Permalink
Add from_cache_dir class method.
Browse files Browse the repository at this point in the history
Add testing.
  • Loading branch information
morriscb committed Aug 19, 2024
1 parent d3205bb commit a6e5c4e
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 28 deletions.
73 changes: 47 additions & 26 deletions notebooks/10x_snRNASeq_tutorial_part_1.ipynb

Large diffs are not rendered by default.

33 changes: 32 additions & 1 deletion src/abc_atlas_access/abc_atlas_cache/abc_project_cache.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from typing import Optional, Union, List
from pathlib import Path
import logging
import os
import pandas as pd
import warnings

from abc_atlas_access.abc_atlas_cache.cloud_cache import (
S3CloudCache,
LocalCache
)
import warnings


class AbcProjectCacheVersionException(Exception):
Expand Down Expand Up @@ -136,6 +138,35 @@ def from_local_cache(
)
return cls(cache, local=True)

@classmethod
def from_cache_dir(
cls,
cache_dir: Union[str, Path],
) -> "ProjectCloudApiBase":
"""Instantiate either a local_cache or s3_cache instance of the class.
Tests if user has write access to the directory, instantiate a s3_cache
else instantiate a local_cache.
Parameters
----------
cache_dir: str or pathlib.Path
Path to the directory where data will be stored on the local system
Returns
-------
ProjectCloudApiBase instance
"""
cache_dir = Path(cache_dir)
# If the directory does not exist or we have write access to it
# we will use the s3 cache. Else we will use the local cache.
if not cache_dir.exists() or os.access(cache_dir, os.W_OK):
return cls.from_s3_cache(cache_dir)
elif cache_dir.exists() and not os.access(cache_dir, os.W_OK):
return cls.from_local_cache(cache_dir)
else:
raise RuntimeError("Could not access or create cache directory.")

def compare_manifests(self,
manifest_newer_name: str,
manifest_older_name: str
Expand Down
34 changes: 33 additions & 1 deletion tests/abc_atlas_cache/test_abc_project_cache.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import json
import pytest
from moto import mock_aws
import os
from unittest.mock import patch
import pytest

from .utils import (
BaseCacheTestCase,
create_manifest_dict,
Expand All @@ -11,6 +14,10 @@
from abc_atlas_access.abc_atlas_cache.abc_project_cache import AbcProjectCache
from abc_atlas_access.abc_atlas_cache.file_attributes import \
CacheFileAttributes
from abc_atlas_access.abc_atlas_cache.cloud_cache import (
LocalCache,
S3CloudCache
)


@mock_aws
Expand Down Expand Up @@ -249,3 +256,28 @@ def test_abc_project_cache_local_cache(self):
directory="test_directory"
)
assert metadata_paths == [self.cache_dir / self.new_metadata_path]

def test_abc_project_cache_from_cache_dir(self):
"""Run test that caches are initialized from the cache directory
correctly as either a S3CloudCache or LocalCache object when
appropreate.
"""
AbcProjectCache._bucket_name = self.test_bucket_name
cache = AbcProjectCache.from_cache_dir(self.cache_dir)
assert isinstance(cache.cache, S3CloudCache)

# Download data into the local cache.
cache.get_data_path(directory="second_dir",
file_name=self.new_file)
cache.get_data_path(directory="test_directory",
file_name=self.data_file)
cache.get_metadata_path(directory="test_directory",
file_name="metadata_file")

# Remove the previous cache object.
del cache

# Initialize a new read only object from the local cache.
with patch('os.access', return_value=False):
cache = AbcProjectCache.from_cache_dir(self.cache_dir)
assert isinstance(cache.cache, LocalCache)

0 comments on commit a6e5c4e

Please sign in to comment.