|
| 1 | +"""Semantic context using semantic types for type safety. |
| 2 | +
|
| 3 | +This module provides SemanticContext, which is like Context but uses |
| 4 | +semantic types instead of plain strings for enhanced type safety. |
| 5 | +""" |
| 6 | + |
| 7 | +from __future__ import annotations |
| 8 | + |
| 9 | +from dataclasses import dataclass, field |
| 10 | +from typing import Optional |
| 11 | + |
| 12 | +from cli_patterns.core.parser_types import ( |
| 13 | + CommandId, |
| 14 | + CommandList, |
| 15 | + ContextKey, |
| 16 | + ContextState, |
| 17 | + ParseMode, |
| 18 | + make_command_id, |
| 19 | + make_context_key, |
| 20 | + make_parse_mode, |
| 21 | +) |
| 22 | +from cli_patterns.ui.parser.types import Context |
| 23 | + |
| 24 | + |
| 25 | +@dataclass |
| 26 | +class SemanticContext: |
| 27 | + """Parsing context containing session state and history using semantic types. |
| 28 | +
|
| 29 | + This is the semantic type equivalent of Context, providing type safety |
| 30 | + for parsing context operations while maintaining the same structure. |
| 31 | +
|
| 32 | + Attributes: |
| 33 | + mode: Current parsing mode (semantic type) |
| 34 | + history: Command history list (semantic types) |
| 35 | + session_state: Dictionary of session state data (semantic types) |
| 36 | + current_directory: Current working directory (optional) |
| 37 | + """ |
| 38 | + |
| 39 | + mode: ParseMode = field(default_factory=lambda: make_parse_mode("text")) |
| 40 | + history: CommandList = field(default_factory=list) |
| 41 | + session_state: ContextState = field(default_factory=dict) |
| 42 | + current_directory: Optional[str] = None |
| 43 | + |
| 44 | + @classmethod |
| 45 | + def from_context(cls, context: Context) -> SemanticContext: |
| 46 | + """Create a SemanticContext from a regular Context. |
| 47 | +
|
| 48 | + Args: |
| 49 | + context: Regular Context to convert |
| 50 | +
|
| 51 | + Returns: |
| 52 | + SemanticContext with semantic types |
| 53 | + """ |
| 54 | + return cls( |
| 55 | + mode=make_parse_mode(context.mode), |
| 56 | + history=[make_command_id(cmd) for cmd in context.history], |
| 57 | + session_state={ |
| 58 | + make_context_key(key): value |
| 59 | + for key, value in context.session_state.items() |
| 60 | + if isinstance( |
| 61 | + value, str |
| 62 | + ) # Only convert string values to maintain type safety |
| 63 | + }, |
| 64 | + current_directory=context.current_directory, |
| 65 | + ) |
| 66 | + |
| 67 | + def to_context(self) -> Context: |
| 68 | + """Convert this SemanticContext to a regular Context. |
| 69 | +
|
| 70 | + Returns: |
| 71 | + Regular Context with string types |
| 72 | + """ |
| 73 | + return Context( |
| 74 | + mode=str(self.mode), |
| 75 | + history=[str(cmd) for cmd in self.history], |
| 76 | + session_state={ |
| 77 | + str(key): value for key, value in self.session_state.items() |
| 78 | + }, |
| 79 | + current_directory=self.current_directory, |
| 80 | + ) |
| 81 | + |
| 82 | + def add_to_history(self, command: CommandId) -> None: |
| 83 | + """Add command to history. |
| 84 | +
|
| 85 | + Args: |
| 86 | + command: Semantic command to add to history |
| 87 | + """ |
| 88 | + self.history.append(command) |
| 89 | + |
| 90 | + def get_recent_commands(self, count: int) -> CommandList: |
| 91 | + """Get the most recent commands from history. |
| 92 | +
|
| 93 | + Args: |
| 94 | + count: Number of recent commands to retrieve |
| 95 | +
|
| 96 | + Returns: |
| 97 | + List of recent commands (semantic types) |
| 98 | + """ |
| 99 | + if count <= 0: |
| 100 | + return [] |
| 101 | + return self.history[-count:] |
| 102 | + |
| 103 | + def get_state( |
| 104 | + self, key: ContextKey, default: Optional[str] = None |
| 105 | + ) -> Optional[str]: |
| 106 | + """Get session state value by key. |
| 107 | +
|
| 108 | + Args: |
| 109 | + key: Semantic state key to retrieve |
| 110 | + default: Default value if key doesn't exist |
| 111 | +
|
| 112 | + Returns: |
| 113 | + State value or default |
| 114 | + """ |
| 115 | + return self.session_state.get(key, default) |
| 116 | + |
| 117 | + def set_state(self, key: ContextKey, value: Optional[str]) -> None: |
| 118 | + """Set session state value. |
| 119 | +
|
| 120 | + Args: |
| 121 | + key: Semantic state key to set |
| 122 | + value: Value to set (None to remove key) |
| 123 | + """ |
| 124 | + if value is None: |
| 125 | + self.session_state.pop(key, None) |
| 126 | + else: |
| 127 | + self.session_state[key] = value |
| 128 | + |
| 129 | + def has_state(self, key: ContextKey) -> bool: |
| 130 | + """Check if a state key exists. |
| 131 | +
|
| 132 | + Args: |
| 133 | + key: Semantic state key to check |
| 134 | +
|
| 135 | + Returns: |
| 136 | + True if key exists, False otherwise |
| 137 | + """ |
| 138 | + return key in self.session_state |
| 139 | + |
| 140 | + def clear_history(self) -> None: |
| 141 | + """Clear command history.""" |
| 142 | + self.history.clear() |
0 commit comments