Skip to content

Commit 729430d

Browse files
committed
notes(2025-03-02[architecture-plan]) Update for latest changes
1 parent ec6db9b commit 729430d

File tree

1 file changed

+52
-45
lines changed

1 file changed

+52
-45
lines changed

notes/2025-03-02-architecture-plan.md

+52-45
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ The module now implements a hierarchical snapshot system for tmux objects with t
99
1. A modular package structure:
1010
```
1111
src/libtmux/snapshot/
12-
├── __init__.py # Module documentation only, no exports
13-
├── base.py # Base classes with Sealable mixins
12+
├── __init__.py # Module documentation and examples
13+
├── base.py # Base classes with Sealable mixins and common methods
1414
├── types.py # Type definitions, exports, and annotations
15+
├── factory.py # Type-safe centralized factory functions
1516
├── models/
1617
│ ├── __init__.py # Package documentation only, no exports
1718
│ ├── pane.py # PaneSnapshot implementation
@@ -29,24 +30,36 @@ The module now implements a hierarchical snapshot system for tmux objects with t
2930

3031
3. Each class inherits from both:
3132
- The corresponding tmux class (Server, Session, etc.)
32-
- A `Sealable` base class to provide immutability (defined in `base.py`)
33+
- A `SnapshotBase` class (derived from `Sealable`) to provide immutability and common methods
34+
- Specialized base classes (`SealablePaneBase`, `SealableWindowBase`, etc.) in `base.py`
3335

34-
4. Utility functions for:
36+
4. Centralized factory functions in `factory.py`:
37+
- Type-safe overloaded `create_snapshot()` function for all tmux object types
38+
- `create_snapshot_active()` convenience function for active-only snapshots
39+
- Clear return type annotations using overloads
40+
41+
5. Enhanced API with common methods in the `SnapshotBase` class:
42+
- `to_dict()` for serialization
43+
- `filter()` for transforming snapshots
44+
- `active_only()` for creating active-only views
45+
46+
6. Utility functions for:
3547
- Filtering snapshots (`filter_snapshot`)
3648
- Converting to dictionaries (`snapshot_to_dict`)
3749
- Creating active-only views (`snapshot_active_only`)
3850

39-
5. Direct imports approach:
40-
- No re-exports from `__init__.py` files
41-
- Users import directly from specific modules
42-
- Clear and explicit dependencies between modules
51+
7. Direct, explicit imports and clean API:
52+
- User-friendly factory functions
53+
- Clear documentation in docstrings with examples
54+
- Consistent API patterns across all snapshot classes
4355

4456
## Typing Approach
4557

4658
The module makes excellent use of Python's modern typing features:
4759

4860
- Type variables with covariance (`PaneT = t.TypeVar("PaneT", bound=Pane, covariant=True)`)
49-
- Proper return type annotations with Union types
61+
- Overloaded functions for type-safe factories
62+
- Union types with proper return type annotations
5063
- Type checking guards (`if t.TYPE_CHECKING:`)
5164
- Type casts for better type safety (`t.cast("ServerSnapshot", filtered)`)
5265
- Centralized type definitions in `types.py`
@@ -71,20 +84,26 @@ All proposals and enhancements must adhere to these core design principles:
7184
- `SessionSnapshot` inherits from `Session`
7285
- `ServerSnapshot` inherits from `Server`
7386

87+
4. **Fluent API**: Provide a fluent, chainable API for working with snapshots.
88+
- Methods like `filter()` and `to_dict()` on all snapshot classes
89+
- Convenience methods that return new instances (maintaining immutability)
90+
- Consistent method names and patterns across all snapshot types
91+
7492
## Strengths of Current Implementation
7593

7694
1. **Modular Structure**: Smaller, focused files with clear responsibilities
77-
2. **Separation of Concerns**: Types, base classes, models, and utilities are now properly separated
95+
2. **Separation of Concerns**: Types, base classes, models, utilities, and factories are properly separated
7896
3. **Immutability Pattern**: Using `frozen_dataclass_sealable` provides a robust way to create immutable snapshots
79-
4. **Type Safety**: Strong typing throughout the codebase
80-
5. **Direct Imports**: Explicit dependencies encourage better code organization
81-
6. **Maintainability**: Easier to understand, test, and extend each component
97+
4. **Type Safety**: Strong typing throughout the codebase with overloaded functions for precise return types
98+
5. **Centralized Factory**: Single entry point with `create_snapshot()` and `create_snapshot_active()` functions
99+
6. **Common Base Class**: `SnapshotBase` provides shared functionality across all snapshot types
100+
7. **User-Friendly API**: Clear, consistent methods with comprehensive docstrings and examples
82101

83102
## Remaining Areas for Improvement
84103

85-
While the modular structure has been implemented, there are still opportunities for enhancing the API:
104+
While the architecture has been significantly improved, there are still opportunities for further enhancement:
86105

87-
1. **Complex Factory Methods**: The `from_X` methods contain complex logic for finding server references, with multiple fallback strategies:
106+
1. **Complex Factory Methods**: The individual `from_X` methods in snapshot classes still contain complex logic for finding server references, with multiple fallback strategies:
88107
```python
89108
if source_server is None and window_snapshot is not None:
90109
source_server = window_snapshot.server
@@ -541,50 +560,38 @@ def create_snapshot(
541560

542561
```python
543562
# Simple usage with centralized factory function
544-
from libtmux.snapshot import create_snapshot
563+
from libtmux.snapshot.factory import create_snapshot, create_snapshot_active
545564

546565
# Create a snapshot of a server
566+
server = Server()
547567
snapshot = create_snapshot(server)
548568

549569
# Create a snapshot with content capture
550570
snapshot_with_content = create_snapshot(server, capture_content=True)
551571

552-
# Use fluent methods for filtering and transformation
553-
active_windows = (
554-
create_snapshot(server)
555-
.filter(lambda s: isinstance(s, WindowSnapshot) and s.active)
556-
.to_dict()
557-
)
558-
559-
# Use advanced configuration
560-
from libtmux.snapshot.models.config import SnapshotConfig
572+
# Create a snapshot of only active components
573+
active_snapshot = create_snapshot_active(server)
561574

562-
config = SnapshotConfig(
563-
capture_content=True,
564-
max_content_lines=100,
565-
include_active_only=True
566-
)
567-
568-
snapshot = create_snapshot(server, config=config)
569-
570-
# Use context manager
571-
from libtmux.snapshot import snapshot_context
572-
573-
with snapshot_context(server) as snapshot:
574-
# Work with immutable snapshot
575-
dev_session = snapshot.find_session("dev")
576-
if dev_session:
577-
print(f"Dev session has {len(dev_session.windows)} windows")
575+
# Use fluent methods for filtering and transformation
576+
dev_session = snapshot.filter(lambda s: isinstance(s, SessionSnapshot) and s.name == "dev")
577+
session_data = dev_session.to_dict() if dev_session else None
578+
579+
# Find specific components
580+
main_session = snapshot.find_session("main")
581+
if main_session:
582+
active_window = main_session.active_window
583+
if active_window:
584+
content = active_window.active_pane.content if active_window.active_pane else None
578585
```
579586

580587
## Implementation Priority and Timeline
581588

582589
Based on the proposals above, the following implementation timeline is suggested:
583590

584591
1. **Phase 1: Enhanced Factory Functions and Basic Fluent API** (1-2 weeks)
585-
- Implement the centralized factory in `factory.py`
586-
- Add basic fluent methods to snapshot classes
587-
- Update type definitions for better safety
592+
- Implement the centralized factory in `factory.py`
593+
- Add basic fluent methods to snapshot classes
594+
- Update type definitions for better safety
588595

589596
2. **Phase 2: Advanced Type Safety with Protocol Classes** (1-2 weeks)
590597
- Implement Protocol classes in `types.py`
@@ -598,7 +605,7 @@ Based on the proposals above, the following implementation timeline is suggested
598605

599606
4. **Phase 4: Complete API Refinement and Documentation** (1-2 weeks)
600607
- Finalize the public API
601-
- Add comprehensive docstrings with examples
608+
- Add comprehensive docstrings with examples
602609
- Provide usage examples in README
603610

604611
These proposals maintain the core design principles of inheritance, immutability, and type safety while significantly improving the API ergonomics, type checking, and user experience.

0 commit comments

Comments
 (0)