"""
Bootstrap for platform imports
Copy this snippet to the top of any script:
# === Platform Bootstrap ===
import sys
from pathlib import Path
_p = Path(__file__).resolve()
while _p != _p.parent:
if (_p / 'system' / 'lib' / 'config.py').exists():
sys.path.insert(0, str(_p))
break
_p = _p.parent
else:
raise RuntimeError("Workspace root not found")
# === End Bootstrap ===
from system.lib.config import Config
cfg = Config(project="pirotehnika")
Or import this module:
from system.lib.bootstrap import setup
setup() # adds workspace to sys.path
"""
import sys
import os
from pathlib import Path
def find_workspace(start: Path = None) -> Path:
"""Find workspace root by walking up directories"""
# First check environment
if os.environ.get('WORKSPACE'):
ws = Path(os.environ['WORKSPACE'])
if ws.exists():
return ws
# Walk up from start path
if start is None:
# Get caller's file location
import inspect
frame = inspect.currentframe()
if frame and frame.f_back:
start = Path(frame.f_back.f_globals.get('__file__', '.')).resolve()
else:
start = Path.cwd()
current = start if start.is_dir() else start.parent
while current != current.parent:
if (current / 'system' / 'lib' / 'config.py').exists():
return current
current = current.parent
raise RuntimeError(
f"Workspace root not found. "
f"Set $WORKSPACE env var or ensure system/lib/config.py exists in parent directories. "
f"Started from: {start}"
)
def setup(start: Path = None) -> Path:
"""Add workspace to sys.path and return workspace path"""
ws = find_workspace(start)
ws_str = str(ws)
if ws_str not in sys.path:
sys.path.insert(0, ws_str)
return ws
# Auto-setup when imported
if __name__ != '__main__':
try:
_ws = setup()
except RuntimeError:
pass # Silent fail for direct imports