Skip to content

Commit

Permalink
move PinsRscCache to its own mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelizimm committed Dec 1, 2023
1 parent 4aac2a5 commit 44870d1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 27 deletions.
42 changes: 18 additions & 24 deletions pins/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,24 @@ def __call__(self, path):
return f"{base_name}_{suffix}"


class PinsRscCacheMapper:
"""Modifies the PinsCache to allow hash_prefix to be an RSC server url.
Note that this class also modifies the first / in a path to be a -, so that
pin contents will not be put into subdirectories, for e.g. michael/mtcars/data.txt.
"""

def __init__(self, hash_prefix):
self.hash_prefix = hash_prefix

def __call__(self, path, same_name):
# the main change in this function is that, for same_name, it returns
# the full path
# change pin path of form <user>/<content> to <user>+<content>
hash = path.replace("/", "+", 1)
return hash


class PinsCache(SimpleCacheFileSystem):
protocol = "pinscache"

Expand Down Expand Up @@ -122,30 +140,6 @@ def _check_file(self, path):
return fn


class PinsRscCache(PinsCache):
"""Modifies the PinsCache to allow hash_prefix to be an RSC server url.
Note that this class also modifies the first / in a path to be a -, so that
pin contents will not be put into subdirectories, for e.g. michael/mtcars/data.txt.
"""

protocol = "pinsrsccache"

def hash_name(self, path, same_name):
# the main change in this function is that, for same_name, it returns
# the full path
if same_name:
if self.hash_prefix is None:
raise NotImplementedError()

# change pin path of form <user>/<content> to <user>+<content>
hash = path.replace("/", "+", 1)
return hash

else:
raise NotImplementedError()


class PinsUrlCache(PinsCache):
protocol = "pinsurlcache"

Expand Down
10 changes: 7 additions & 3 deletions pins/constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import tempfile

from .boards import BaseBoard, BoardRsConnect, BoardManual
from .cache import PinsCache, PinsRscCache, PinsAccessTimeCache, prefix_cache
from .cache import PinsCache, PinsRscCacheMapper, PinsAccessTimeCache, prefix_cache
from .config import get_data_dir, get_cache_dir


Expand Down Expand Up @@ -154,8 +154,12 @@ def board(
board_cache = prefix_cache(fs, hash_prefix)
cache_dir = os.path.join(base_cache_dir, board_cache)

fs = PinsRscCache(
cache_storage=cache_dir, fs=fs, hash_prefix=hash_prefix, same_names=True
fs = PinsCache(
cache_storage=cache_dir,
fs=fs,
hash_prefix=hash_prefix,
same_names=True,
mapper=PinsRscCacheMapper,
)
else:
# ensures each subdir path is its own cache directory
Expand Down

0 comments on commit 44870d1

Please sign in to comment.