-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #17: Basilisk mocking for documentation generation
- Loading branch information
Showing
2 changed files
with
53 additions
and
0 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
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() |
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