oc_chat
Loading...
Searching...
No Matches
oc_chat.__main__ Namespace Reference

Functions

None _preparse_env_flags ()
None _force_utf8_stdio ()
None _apply_workdir (list[str]|None argv=None)

Detailed Description

Package entry point — enables ``python -m oc_chat``.

Purpose
-------
Pre-scans ``sys.argv`` for environment-affecting flags
(``--no-color``, ``--workdir``) *before* importing :mod:`oc_chat.main`.
This is necessary because :mod:`oc_chat.core.config` reads ``NO_COLOR`` at
import time and any module using ``from … import STYLE_DIM`` would
otherwise bind to the original ANSI strings before argparse runs; likewise
the sandbox-root walk-up, ``.oc_chat/`` state anchor, and prompt-layer
anchors all derive from ``cwd`` — the ``--workdir`` chdir must precede
every one of them.

Also forces stdout/stderr to UTF-8 on Windows so glyphs like ``→``
and ``⏳`` don't crash the legacy ``cp1252`` codepage.

Responsibilities
----------------
- :func:`_preparse_env_flags` — translate
  environment-affecting CLI flags into env vars before any imports.
- :func:`_force_utf8_stdio` — best-effort UTF-8 reconfiguration on
  Windows; no-op on platforms whose streams already use UTF-8.
- :func:`_apply_workdir` — parse ``--workdir PATH`` (or ``--workdir=PATH``)
  from ``argv``, resolve, validate (exit 2 on nonexistent dir), and
  ``os.chdir``; called before the ``oc_chat.main`` import so every
  anchor resolves from the requested directory.
- Import and run :func:`oc_chat.main.main` after the pre-flight is
  done.

Diagnostics
-----------
Domain: OC_CHAT.MAIN
Levels:
    L2 — lifecycle events
    L3 — semantic details
    L4 — deep tracing

Contracts
---------
- Pre-flight functions run *before* any other ``oc_chat`` import to
  guarantee correct environment-variable and working-directory timing.
- ``--workdir`` fail-closed: a nonexistent path exits 2 immediately;
  the session never falls through to a wrong anchor.
- ``reconfigure`` failures are silently tolerated — falling back to
  the platform default is preferable to crashing on a stream that
  refuses reconfiguration.
- Standalone-only: importing this module via something other than
  ``python -m oc_chat`` still runs the pre-flight side effects.

Dependencies
------------
- oc_diagnostics (structured debug logging via _dbg)
- oc_chat.main (oc_chat engine module — main)

Function Documentation

◆ _apply_workdir()

None oc_chat.__main__._apply_workdir ( list[str] | None argv = None)
protected
Preflight: parse ``--workdir`` from *argv*, chdir before any imports.

Supports both ``--workdir PATH`` and ``--workdir=PATH`` forms.
A nonexistent directory triggers ``SystemExit(2)`` — fail-closed so
the session never anchors to the wrong ``cwd``.  Leaves *argv*
intact for later argparse processing (argparse also declares the
flag so ``--help`` documents it).

Definition at line 87 of file __main__.py.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _force_utf8_stdio()

None oc_chat.__main__._force_utf8_stdio ( )
protected
Force stdout/stderr to UTF-8 on Windows.

``reconfigure`` is a no-op on streams that already use UTF-8 (modern
terminals, Linux, macOS).

Definition at line 73 of file __main__.py.

Here is the caller graph for this function:

◆ _preparse_env_flags()

None oc_chat.__main__._preparse_env_flags ( )
protected
Translate environment-affecting CLI flags into env vars before imports.

Definition at line 67 of file __main__.py.

Here is the caller graph for this function: