Skip to content

Commit 0bd6a6a

Browse files
authored
Merge pull request #94 from machow/deparse-rsc-local
feat: deparse rsc and file boards
2 parents 033ef43 + 64bb6eb commit 0bd6a6a

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

pins/constructors.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,51 @@ class DEFAULT:
1111
pass
1212

1313

14+
# Representing constructors ===================================================
15+
16+
# Note that this is not a constructor, but a function to represent them.
17+
def board_deparse(board: BaseBoard):
18+
"""Return a representation of how a board could be reconstructed.
19+
20+
Note that this function does not try to represent the exact arguments used
21+
to construct a board, but key pieces (like the path to the board). You may
22+
need to specify environment variables with API keys to complete the connection.
23+
24+
Parameters
25+
----------
26+
board:
27+
A pins board to be represented.
28+
29+
Examples
30+
--------
31+
32+
The example below deparses a board connected to RStudio Connect.
33+
34+
>>> board_deparse(board_rsconnect(server_url="http://example.com", api_key="xxx"))
35+
"board_rsconnect(server_url='http://example.com')"
36+
37+
Note that the deparsing an RStudio Connect board does not keep the api_key,
38+
which is sensitive information. In this case, you can set the CONNECT_API_KEY
39+
environment variable to connect.
40+
41+
Below is an example of representing a board connected to a local folder.
42+
43+
>>> board_deparse(board_folder("a/b/c"))
44+
"board_folder('a/b/c')"
45+
"""
46+
47+
prot = board.fs.protocol
48+
if prot == "rsc":
49+
url = board.fs.api.server_url
50+
return f"board_rsconnect(server_url={repr(url)})"
51+
elif prot == "file":
52+
return f"board_folder({repr(board.board)})"
53+
else:
54+
raise NotImplementedError(
55+
"board deparsing currently not supported for protocol: {prot}"
56+
)
57+
58+
1459
# Board constructors ==========================================================
1560
# note that libraries not used by board classes above are imported within these
1661
# functions. may be worth moving these funcs into their own module.

pins/tests/test_constructors.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,20 @@ def test_board_constructor_folder(tmp_dir2, df):
188188
df2 = board.pin_read("some_df")
189189

190190
assert df.equals(df2)
191+
192+
193+
# Deparsing ===================================================================
194+
195+
196+
def test_board_deparse(board):
197+
prot = board.fs.protocol
198+
if prot not in ["rsc", "file"]:
199+
# not implemented for other boards
200+
pytest.xfail()
201+
202+
with rm_env("CONNECT_API_KEY"):
203+
if prot == "rsc":
204+
os.environ["CONNECT_API_KEY"] = board.fs.api.api_key
205+
206+
new_board = eval(c.board_deparse(board), c.__dict__)
207+
new_board.pin_list()

0 commit comments

Comments
 (0)