/ templates / SovereignSymbol / dreamtalk_init.py
dreamtalk_init.py
  1  """
  2  DreamTalk Path Resolver
  3  
  4  This module enables the holarchic submodule pattern for DreamTalk symbols.
  5  It finds the nearest initialized DreamTalk library by walking up the directory
  6  tree, ensuring all code in a symbol holarchy uses the same module instance.
  7  
  8  Usage (at the top of any sovereign symbol's .py file):
  9  
 10      from dreamtalk_init import init
 11      init()
 12  
 13      from DreamTalk.imports import *
 14  """
 15  
 16  import sys
 17  from pathlib import Path
 18  
 19  _initialized = False
 20  _dreamtalk_path = None
 21  
 22  
 23  def find_dreamtalk(start_path=None):
 24      """
 25      Walk up the directory tree to find an initialized DreamTalk submodule.
 26  
 27      An initialized DreamTalk is identified by the presence of imports.py
 28      (empty submodule pointers won't have this file).
 29  
 30      Args:
 31          start_path: Starting directory for search. Defaults to caller's location.
 32  
 33      Returns:
 34          Path to the submodules/ directory containing initialized DreamTalk.
 35  
 36      Raises:
 37          ImportError: If no initialized DreamTalk is found.
 38      """
 39      if start_path is None:
 40          # Get the caller's file location
 41          import inspect
 42          frame = inspect.currentframe()
 43          if frame and frame.f_back and frame.f_back.f_back:
 44              caller_file = frame.f_back.f_back.f_globals.get('__file__')
 45              if caller_file:
 46                  start_path = Path(caller_file).parent
 47          if start_path is None:
 48              start_path = Path.cwd()
 49  
 50      current = Path(start_path).resolve()
 51  
 52      while current != current.parent:
 53          # Check for DreamTalk in submodules/
 54          candidate = current / "submodules" / "DreamTalk"
 55          if (candidate / "imports.py").exists():
 56              return current / "submodules"
 57  
 58          # Also check if we ARE in DreamTalk (for development/testing)
 59          if (current / "imports.py").exists() and current.name == "DreamTalk":
 60              return current.parent
 61  
 62          current = current.parent
 63  
 64      raise ImportError(
 65          "No initialized DreamTalk found in parent hierarchy.\n"
 66          "Ensure DreamTalk is added as a submodule and initialized:\n"
 67          "  git submodule add <dreamtalk-url> submodules/DreamTalk\n"
 68          "  git submodule update --init submodules/DreamTalk"
 69      )
 70  
 71  
 72  def init(start_path=None):
 73      """
 74      Initialize the Python path to use the nearest DreamTalk.
 75  
 76      This should be called at the top of any sovereign symbol's Python file,
 77      before importing from DreamTalk.
 78  
 79      Args:
 80          start_path: Starting directory for search. Defaults to caller's location.
 81  
 82      Returns:
 83          Path to the DreamTalk directory that was added to sys.path.
 84      """
 85      global _initialized, _dreamtalk_path
 86  
 87      if _initialized:
 88          return _dreamtalk_path
 89  
 90      submodules_path = find_dreamtalk(start_path)
 91  
 92      # Add to sys.path if not already present
 93      submodules_str = str(submodules_path)
 94      if submodules_str not in sys.path:
 95          sys.path.insert(0, submodules_str)
 96  
 97      _dreamtalk_path = submodules_path / "DreamTalk"
 98      _initialized = True
 99  
100      return _dreamtalk_path
101  
102  
103  def get_dreamtalk_path():
104      """
105      Get the path to the initialized DreamTalk, or None if not yet initialized.
106      """
107      return _dreamtalk_path
108  
109  
110  def is_initialized():
111      """
112      Check if DreamTalk path resolution has been performed.
113      """
114      return _initialized