|
oc_chat
|
Functions | |
| dict | _load_json (Path path) |
| None | _write_json_atomic (Path path, dict data) |
| Path|None | _find_meta_file (Path session_dir) |
| list[Path] | _collect_sessions (Path sessions_root) |
| int | migrate (str sessions_dir, bool dry_run=True, bool verbose=True) |
| int | main () |
Variables | |
| _TIMESTAMP_RE = re.compile(r"^session_\d{8}_\d{6}$") | |
| dict | _SKIP_DIRS = {"archive", "legacy"} |
Session name migration: timestamp format → random-word format.
Purpose
-------
One-shot offline script that renames timestamp-format sessions
(``session_YYYYMMDD_HHMMSS``) to readable random-word names
(e.g., ``sunny-weaving-honey``).
Responsibilities
----------------
- Detect sessions matching the old timestamp pattern
- Generate a fresh random-word name for each
- Rename the session directory on disk
- Rename all files inside (which embed the old name as a prefix)
- Update the ``session_name`` field in the ``_meta.json`` file
- Print a mapping table: old_name → new_name
- Support ``--dry-run`` (default) to preview without writing
Diagnostics
-----------
Domain: OC_CHAT.MAIN
Levels:
L2 — migration events (renamed, skipped, errors)
L3 — per-file rename details
L4 — per-path filesystem operation traces
Contracts
---------
- Idempotent: sessions already in random-word format are skipped
- ``--dry-run`` makes no filesystem changes
- Exits with code 0 on success, 1 on any unrecoverable error
- Never touches the archive/ or legacy/ subdirectories
Dependencies
------------
- oc_diagnostics (structured debug logging via _dbg)
- oc_chat.core.wordgen (lazy; random_name, which generates each collision-free new session name)
- oc_chat.core.persistence (lazy; DEFAULT_SESSIONS_DIR, the default for --sessions-dir)
|
protected |
Return session dirs that match the old timestamp naming pattern.
Definition at line 89 of file migrate_session_names.py.
|
protected |
Return the most recently modified *_meta.json in session_dir, or None.
Definition at line 83 of file migrate_session_names.py.
|
protected |
Read a JSON file, stripping UTF-8 BOM if present.
Definition at line 64 of file migrate_session_names.py.
|
protected |
Write JSON atomically via a temp file + rename.
Definition at line 72 of file migrate_session_names.py.
| int oc_chat.scripts.migrate_session_names.main | ( | ) |
Entry point for ``python -m oc_chat.scripts.migrate_session_names``.
Definition at line 208 of file migrate_session_names.py.
| int oc_chat.scripts.migrate_session_names.migrate | ( | str | sessions_dir, |
| bool | dry_run = True, | ||
| bool | verbose = True ) |
Run the migration.
Args:
sessions_dir: Path to the sessions root directory.
dry_run: If True, print what would happen but make no changes.
verbose: If True, print progress to stdout.
Returns:
Number of sessions migrated (or that would be migrated).
Definition at line 102 of file migrate_session_names.py.
|
protected |
Definition at line 61 of file migrate_session_names.py.
|
protected |
Definition at line 58 of file migrate_session_names.py.