Skip to content

Commit d9e230a

Browse files
committed
Stop using env var for shiny express filename
1 parent d18bd2e commit d9e230a

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

rsconnect/main.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
produce_bootstrap_output,
8181
parse_client_response,
8282
)
83-
from .shiny_express import is_express_app
83+
from .shiny_express import escape_to_var_name, is_express_app
8484

8585
server_store = ServerStore()
8686
future_enabled = False
@@ -1424,8 +1424,7 @@ def deploy_app(
14241424
)
14251425

14261426
if is_express_app(entrypoint + ".py", directory):
1427-
env_vars["SHINY_EXPRESS_APP_FILE"] = entrypoint + ".py"
1428-
entrypoint = "shiny.express.app"
1427+
entrypoint = "shiny.express.app:" + escape_to_var_name(entrypoint + ".py")
14291428

14301429
extra_args = dict(
14311430
directory=directory,

rsconnect/shiny_express.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import ast
77
from pathlib import Path
8+
import re
89

910
__all__ = ("is_express_app",)
1011

@@ -72,3 +73,27 @@ def visit_Module(self, node: ast.Module):
7273
# Don't recurse into any nodes, so the we'll only ever look at top-level nodes.
7374
def generic_visit(self, node: ast.AST):
7475
pass
76+
77+
78+
def escape_to_var_name(x: str) -> str:
79+
"""
80+
Given a string, escape it to a valid Python variable name which contains
81+
[a-zA-Z0-9_]. All other characters will be escaped to _<hex>_. Also, if the first
82+
character is a digit, it will be escaped to _<hex>_, because Python variable names
83+
can't begin with a digit.
84+
"""
85+
encoded = ""
86+
is_first = True
87+
88+
for char in x:
89+
if is_first and re.match("[0-9]", char):
90+
encoded += f"_{ord(char):x}_"
91+
elif re.match("[a-zA-Z0-9]", char):
92+
encoded += char
93+
else:
94+
encoded += f"_{ord(char):x}_"
95+
96+
if is_first:
97+
is_first = False
98+
99+
return encoded

0 commit comments

Comments
 (0)