Skip to content

Commit

Permalink
Issue #17: Basilisk mocking for documentation generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark2000 committed Jan 30, 2024
1 parent 5bd76bc commit 96485fa
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
45 changes: 45 additions & 0 deletions docs/sitecustomize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import ast
import os
import pkgutil
import sys
from unittest.mock import MagicMock

"""
Copy this file to the site-packages directory of your virtual environment to mock
Basilisk for generating documentation without Basilisk installed
"""

os.environ["PYTHON_MOCK_BASILISK"] = "1"

bsk_rl_package_path = (
pkgutil.get_loader("bsk_rl").get_filename().split("__init__.py")[0]
)
print(bsk_rl_package_path)

# Find all imports from Basilisk within bsk_rl package
all_basilisk_imports = set()
for root, _, files in os.walk(bsk_rl_package_path):
for file in files:
if file.endswith(".py"):
file_path = os.path.join(root, file)
with open(file_path, "r") as f:
try:
tree = ast.parse(f.read(), filename=file)
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for alias in node.names:
if alias.name.startswith("Basilisk"):
all_basilisk_imports.add(alias.name)
elif isinstance(node, ast.ImportFrom):
if node.module and node.module.startswith("Basilisk"):
all_basilisk_imports.add(node.module)
except Exception as e:
print(f"Error parsing {file_path}: {e}")

# Mock those imports
for submodule_name in all_basilisk_imports:
sys.modules[submodule_name] = MagicMock()

# Mock some other imports that might cause issues
sys.modules["Basilisk"].__path__ = "not/a/real/path"
sys.modules["chebpy"] = MagicMock()
8 changes: 8 additions & 0 deletions src/bsk_rl/_check_bsk_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@


def _check_bsk_version():
# Don't run check if Basilisk is mocked
try:
if os.environ["PYTHON_MOCK_BASILISK"] == "1":
return
except KeyError:
pass

# Otherwise, check Basilisk version against requirement
f = open(
os.path.join(
os.path.dirname(os.path.realpath(__file__)),
Expand Down

0 comments on commit 96485fa

Please sign in to comment.