Skip to content

Commit 3e4590c

Browse files
authored
Merge pull request #130 from rstudio/fix-disable-fs-cache
Fix disable fs cache
2 parents 348dc41 + 1936faa commit 3e4590c

File tree

4 files changed

+63
-20
lines changed

4 files changed

+63
-20
lines changed

pins/boards.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424

2525
# Note that once we drop python 3.7, we can make this a Protocol
2626
class IFileSystem:
27+
28+
protocol: "str | list"
29+
2730
def ls(self, path: str) -> Sequence[str]:
2831
...
2932

pins/constructors.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def board_deparse(board: BaseBoard):
5252
allow_pickle = ""
5353

5454
prot = board.fs.protocol
55+
5556
if prot == "rsc":
5657
url = board.fs.api.server_url
5758
return f"board_rsconnect(server_url={repr(url)}{allow_pickle})"
@@ -72,10 +73,10 @@ def board(
7273
protocol: str,
7374
path: str = "",
7475
versioned: bool = True,
75-
cache: "DEFAULT | None" = DEFAULT,
76+
cache: "type[DEFAULT] | None" = DEFAULT,
7677
allow_pickle_read=None,
7778
storage_options: "dict | None" = None,
78-
board_factory: "callable | BaseBoard | None" = None,
79+
board_factory: "callable | type[BaseBoard] | None" = None,
7980
):
8081
"""General function for constructing a pins board.
8182
@@ -109,10 +110,17 @@ def board(
109110
fsspec.filesystem.
110111
board_factory:
111112
An optional board class to use as the constructor.
113+
114+
Note
115+
----
116+
Many fsspec implementations of filesystems cache the searching of files, which may
117+
cause you to not see pins saved by other people. Disable this on these file systems
118+
with `storage_options = {"cache_timeout": 0}`.
119+
112120
"""
113121

114122
if storage_options is None:
115-
storage_options = {}
123+
storage_options = {"listings_expiry_time": 0}
116124

117125
# TODO: at this point should just manually construct the rsc board directly
118126
# from board_rsconnect...
@@ -276,7 +284,7 @@ def board_github(
276284
versioned,
277285
cache,
278286
allow_pickle_read=allow_pickle_read,
279-
storage_options={"org": org, "repo": repo},
287+
storage_options={"org": org, "repo": repo, "listings_expiry_time": 0},
280288
)
281289

282290

pins/tests/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def get_param_from_env(entry):
117117
return value
118118

119119
def create_tmp_board(self, src_board=None) -> BaseBoard:
120-
fs = filesystem(self.fs_name)
120+
fs = filesystem(self.fs_name, listings_expiry_time=0)
121121
temp_name = str(uuid.uuid4())
122122

123123
if isinstance(self.path, TemporaryDirectory):

pins/tests/test_constructors.py

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@ def check_cache_file_path(p_file, p_cache):
2828
assert str(p_file.relative_to(p_cache)).count("/") == 2
2929

3030

31+
def construct_from_board(board):
32+
prot = board.fs.protocol
33+
fs_name = prot if isinstance(prot, str) else prot[0]
34+
35+
if fs_name == "file":
36+
board = c.board_folder(board.board)
37+
elif fs_name == "rsc":
38+
board = c.board_rsconnect(
39+
server_url=board.fs.api.server_url, api_key=board.fs.api.api_key
40+
)
41+
else:
42+
board = getattr(c, f"board_{fs_name}")(board.board)
43+
44+
return board
45+
46+
3147
# End-to-end constructor tests
3248

3349
# there are two facets of boards: reading and writing.
@@ -120,7 +136,7 @@ def test_constructor_board_github(tmp_cache, http_example_board_path, df_csv):
120136
check_cache_file_path(res[0], cache_dir)
121137

122138

123-
@pytest.fixture(scope="session")
139+
@pytest.fixture(scope="function")
124140
def board(backend):
125141
# TODO: copied from test_compat.py
126142

@@ -129,23 +145,12 @@ def board(backend):
129145
backend.teardown_board(board)
130146

131147

132-
def test_constructor_board(board, df_csv, tmp_cache):
148+
def test_constructor_boards(board, df_csv, tmp_cache):
133149
# TODO: would be nice to have fixtures for each board constructor
134150
# doesn't need to copy over pins-compat content
135151

136152
# create board from constructor -------------------------------------------
137-
138-
prot = board.fs.protocol
139-
fs_name = prot if isinstance(prot, str) else prot[0]
140-
141-
if fs_name == "file":
142-
board = c.board_folder(board.board)
143-
elif fs_name == "rsc":
144-
board = c.board_rsconnect(
145-
server_url=board.fs.api.server_url, api_key=board.fs.api.api_key
146-
)
147-
else:
148-
board = getattr(c, f"board_{fs_name}")(board.board)
153+
board = construct_from_board(board)
149154

150155
# read a pin and check its contents ---------------------------------------
151156

@@ -157,7 +162,7 @@ def test_constructor_board(board, df_csv, tmp_cache):
157162
# check the cache structure -----------------------------------------------
158163

159164
# check cache
160-
if fs_name == "file":
165+
if board.fs.protocol == "file":
161166
# no caching for local file boards
162167
pass
163168
else:
@@ -187,6 +192,33 @@ def test_constructor_board(board, df_csv, tmp_cache):
187192
assert orig_access < new_access
188193

189194

195+
@pytest.fixture(scope="function")
196+
def board2(backend):
197+
board2 = backend.create_tmp_board()
198+
yield board2
199+
backend.teardown_board(board2)
200+
201+
202+
def test_constructor_boards_multi_user(board2, df_csv, tmp_cache):
203+
prot = board2.fs.protocol
204+
fs_name = prot if isinstance(prot, str) else prot[0]
205+
206+
if fs_name == "rsc":
207+
# TODO: RSConnect writes pin names like <user>/<name>, so would need to
208+
# modify test
209+
pytest.skip()
210+
211+
first = construct_from_board(board2)
212+
213+
first.pin_write(df_csv, "df_csv", type="csv")
214+
assert first.pin_list() == ["df_csv"]
215+
216+
second = construct_from_board(board2)
217+
second.pin_write(df_csv, "another_df_csv", type="csv")
218+
219+
assert sorted(second.pin_list()) == sorted(["df_csv", "another_df_csv"])
220+
221+
190222
# Board particulars ===========================================================
191223

192224

0 commit comments

Comments
 (0)