__init__.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. """
  2. Common utilities for Trellis workflow scripts.
  3. This module provides shared functionality used by other Trellis scripts.
  4. """
  5. import io
  6. import sys
  7. # =============================================================================
  8. # Windows Encoding Fix (MUST be at top, before any other output)
  9. # =============================================================================
  10. # On Windows, stdout defaults to the system code page (often GBK/CP936).
  11. # This causes UnicodeEncodeError when printing non-ASCII characters.
  12. #
  13. # Any script that imports from common will automatically get this fix.
  14. # =============================================================================
  15. def _configure_stream(stream: object) -> object:
  16. """Configure a stream for UTF-8 encoding on Windows."""
  17. # Try reconfigure() first (Python 3.7+, more reliable)
  18. if hasattr(stream, "reconfigure"):
  19. stream.reconfigure(encoding="utf-8", errors="replace") # type: ignore[union-attr]
  20. return stream
  21. # Fallback: detach and rewrap with TextIOWrapper
  22. elif hasattr(stream, "detach"):
  23. return io.TextIOWrapper(
  24. stream.detach(), # type: ignore[union-attr]
  25. encoding="utf-8",
  26. errors="replace",
  27. )
  28. return stream
  29. if sys.platform == "win32":
  30. sys.stdout = _configure_stream(sys.stdout) # type: ignore[assignment]
  31. sys.stderr = _configure_stream(sys.stderr) # type: ignore[assignment]
  32. sys.stdin = _configure_stream(sys.stdin) # type: ignore[assignment]
  33. def configure_encoding() -> None:
  34. """
  35. Configure stdout/stderr/stdin for UTF-8 encoding on Windows.
  36. This is automatically called when importing from common,
  37. but can be called manually for scripts that don't import common.
  38. Safe to call multiple times.
  39. """
  40. global sys
  41. if sys.platform == "win32":
  42. sys.stdout = _configure_stream(sys.stdout) # type: ignore[assignment]
  43. sys.stderr = _configure_stream(sys.stderr) # type: ignore[assignment]
  44. sys.stdin = _configure_stream(sys.stdin) # type: ignore[assignment]
  45. from .paths import (
  46. DIR_WORKFLOW,
  47. DIR_WORKSPACE,
  48. DIR_TASKS,
  49. DIR_ARCHIVE,
  50. DIR_SPEC,
  51. DIR_SCRIPTS,
  52. FILE_DEVELOPER,
  53. FILE_CURRENT_TASK,
  54. FILE_TASK_JSON,
  55. FILE_JOURNAL_PREFIX,
  56. get_repo_root,
  57. get_developer,
  58. check_developer,
  59. get_tasks_dir,
  60. get_workspace_dir,
  61. get_active_journal_file,
  62. count_lines,
  63. get_current_task,
  64. get_current_task_abs,
  65. normalize_task_ref,
  66. resolve_task_ref,
  67. set_current_task,
  68. clear_current_task,
  69. has_current_task,
  70. generate_task_date_prefix,
  71. )
  72. from .active_task import (
  73. ActiveTask,
  74. clear_active_task,
  75. resolve_active_task,
  76. resolve_context_key,
  77. set_active_task,
  78. )