-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #521 from rstudio/shiny-express
- Loading branch information
Showing
3 changed files
with
133 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# The contents of this file are copied from: | ||
# https://github.com/posit-dev/py-shiny/blob/feb4cb7f872922717c39753514ae2d7fa32f10a1/shiny/express/_is_express.py | ||
|
||
from __future__ import annotations | ||
|
||
import ast | ||
from pathlib import Path | ||
import re | ||
|
||
__all__ = ("is_express_app",) | ||
|
||
|
||
def is_express_app(app: str, app_dir: str | None) -> bool: | ||
"""Detect whether an app file is a Shiny express app | ||
Parameters | ||
---------- | ||
app | ||
App filename, like "app.py". It may be a relative path or absolute path. | ||
app_dir | ||
Directory containing the app file. If this is `None`, then `app` must be an | ||
absolute path. | ||
Returns | ||
------- | ||
: | ||
`True` if it is a Shiny express app, `False` otherwise. | ||
""" | ||
if not app.lower().endswith(".py"): | ||
return False | ||
|
||
if app_dir is not None: | ||
app_path = Path(app_dir) / app | ||
else: | ||
app_path = Path(app) | ||
|
||
if not app_path.exists(): | ||
return False | ||
|
||
try: | ||
# Read the file, parse it, and look for any imports of shiny.express. | ||
with open(app_path) as f: | ||
content = f.read() | ||
tree = ast.parse(content, app_path) | ||
detector = DetectShinyExpressVisitor() | ||
detector.visit(tree) | ||
|
||
except Exception: | ||
return False | ||
|
||
return detector.found_shiny_express_import | ||
|
||
|
||
class DetectShinyExpressVisitor(ast.NodeVisitor): | ||
def __init__(self): | ||
super().__init__() | ||
self.found_shiny_express_import = False | ||
|
||
def visit_Import(self, node: ast.Import): | ||
if any(alias.name == "shiny.express" for alias in node.names): | ||
self.found_shiny_express_import = True | ||
|
||
def visit_ImportFrom(self, node: ast.ImportFrom): | ||
if node.module == "shiny.express": | ||
self.found_shiny_express_import = True | ||
elif node.module == "shiny" and any(alias.name == "express" for alias in node.names): | ||
self.found_shiny_express_import = True | ||
|
||
# Visit top-level nodes. | ||
def visit_Module(self, node: ast.Module): | ||
super().generic_visit(node) | ||
|
||
# Don't recurse into any nodes, so the we'll only ever look at top-level nodes. | ||
def generic_visit(self, node: ast.AST): | ||
pass | ||
|
||
|
||
def escape_to_var_name(x: str) -> str: | ||
""" | ||
Given a string, escape it to a valid Python variable name which contains | ||
[a-zA-Z0-9_]. All other characters will be escaped to _<hex>_. Also, if the first | ||
character is a digit, it will be escaped to _<hex>_, because Python variable names | ||
can't begin with a digit. | ||
""" | ||
encoded = "" | ||
is_first = True | ||
|
||
for char in x: | ||
if is_first and re.match("[0-9]", char): | ||
encoded += f"_{ord(char):x}_" | ||
elif re.match("[a-zA-Z0-9]", char): | ||
encoded += char | ||
else: | ||
encoded += f"_{ord(char):x}_" | ||
|
||
if is_first: | ||
is_first = False | ||
|
||
return encoded |