|
1 | | -"""Protocol definitions for CLI Patterns.""" |
| 1 | +"""Protocol definitions for CLI Patterns. |
2 | 2 |
|
3 | | -# Placeholder for protocol definitions |
| 3 | +This module defines the core protocols (interfaces) that implementation classes |
| 4 | +must satisfy. Protocols enable: |
| 5 | +- Dependency injection |
| 6 | +- Multiple implementations |
| 7 | +- Type-safe interfaces |
| 8 | +- Runtime checking (with @runtime_checkable) |
| 9 | +
|
| 10 | +All protocols are runtime-checkable, meaning isinstance() checks work at runtime. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +from typing import Protocol, runtime_checkable |
| 16 | + |
| 17 | +from cli_patterns.core.models import ( |
| 18 | + ActionConfigUnion, |
| 19 | + ActionResult, |
| 20 | + CollectionResult, |
| 21 | + NavigationResult, |
| 22 | + OptionConfigUnion, |
| 23 | + SessionState, |
| 24 | +) |
| 25 | +from cli_patterns.core.types import BranchId |
| 26 | + |
| 27 | + |
| 28 | +@runtime_checkable |
| 29 | +class ActionExecutor(Protocol): |
| 30 | + """Protocol for executing actions. |
| 31 | +
|
| 32 | + Implementations of this protocol handle the execution of actions |
| 33 | + (bash commands, Python functions, etc.) and return results. |
| 34 | +
|
| 35 | + Example: |
| 36 | + class BashExecutor: |
| 37 | + def execute(self, action: ActionConfigUnion, state: SessionState) -> ActionResult: |
| 38 | + if isinstance(action, BashActionConfig): |
| 39 | + # Execute bash command |
| 40 | + result = subprocess.run(action.command, ...) |
| 41 | + return ActionResult(...) |
| 42 | + """ |
| 43 | + |
| 44 | + def execute(self, action: ActionConfigUnion, state: SessionState) -> ActionResult: |
| 45 | + """Execute an action and return the result. |
| 46 | +
|
| 47 | + Args: |
| 48 | + action: The action configuration to execute |
| 49 | + state: Current session state |
| 50 | +
|
| 51 | + Returns: |
| 52 | + ActionResult containing success status, output, and errors |
| 53 | + """ |
| 54 | + ... |
| 55 | + |
| 56 | + |
| 57 | +@runtime_checkable |
| 58 | +class OptionCollector(Protocol): |
| 59 | + """Protocol for collecting option values from users. |
| 60 | +
|
| 61 | + Implementations of this protocol handle the interactive collection |
| 62 | + of option values (strings, selections, paths, etc.) and return results. |
| 63 | +
|
| 64 | + Example: |
| 65 | + class InteractiveCollector: |
| 66 | + def collect(self, option: OptionConfigUnion, state: SessionState) -> CollectionResult: |
| 67 | + if isinstance(option, StringOptionConfig): |
| 68 | + # Prompt user for string input |
| 69 | + value = input(f"{option.description}: ") |
| 70 | + return CollectionResult(...) |
| 71 | + """ |
| 72 | + |
| 73 | + def collect( |
| 74 | + self, option: OptionConfigUnion, state: SessionState |
| 75 | + ) -> CollectionResult: |
| 76 | + """Collect an option value from the user. |
| 77 | +
|
| 78 | + Args: |
| 79 | + option: The option configuration to collect |
| 80 | + state: Current session state |
| 81 | +
|
| 82 | + Returns: |
| 83 | + CollectionResult containing the collected value or error |
| 84 | + """ |
| 85 | + ... |
| 86 | + |
| 87 | + |
| 88 | +@runtime_checkable |
| 89 | +class NavigationController(Protocol): |
| 90 | + """Protocol for controlling wizard navigation. |
| 91 | +
|
| 92 | + Implementations of this protocol handle navigation between branches |
| 93 | + in the wizard tree, including history management. |
| 94 | +
|
| 95 | + Example: |
| 96 | + class TreeNavigator: |
| 97 | + def navigate(self, target: BranchId, state: SessionState) -> NavigationResult: |
| 98 | + # Update state with new branch |
| 99 | + state.navigation_history.append(state.current_branch) |
| 100 | + state.current_branch = target |
| 101 | + return NavigationResult(...) |
| 102 | + """ |
| 103 | + |
| 104 | + def navigate(self, target: BranchId, state: SessionState) -> NavigationResult: |
| 105 | + """Navigate to a target branch. |
| 106 | +
|
| 107 | + Args: |
| 108 | + target: The branch ID to navigate to |
| 109 | + state: Current session state (will be modified) |
| 110 | +
|
| 111 | + Returns: |
| 112 | + NavigationResult containing success status and target |
| 113 | + """ |
| 114 | + ... |
| 115 | + |
| 116 | + |
| 117 | +# TODO: Future protocol extension points |
| 118 | +# - ValidationProtocol: For custom option validation |
| 119 | +# - InterpolationProtocol: For variable interpolation in commands |
| 120 | +# - PersistenceProtocol: For session state persistence |
| 121 | +# - ThemeProtocol: For custom theming (may already exist in ui.design) |
0 commit comments