Skip to content

Removed (should make this after frozen dataclasses) #585

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Conversation

tony
Copy link
Member

@tony tony commented Feb 28, 2025

Summary by Sourcery

Introduces snapshot functionality for tmux objects, allowing read-only copies of the server, sessions, windows, and panes. These snapshots preserve the object structure and relationships while preventing modifications or tmux command execution. Also adds functionality to filter snapshots and convert them to dictionaries.

New Features:

  • Adds ServerSnapshot, SessionSnapshot, WindowSnapshot, and PaneSnapshot classes for creating read-only snapshots of tmux objects.
  • Implements snapshot filtering to prune the snapshot tree based on a filter function.
  • Provides functionality to convert snapshots to dictionaries, avoiding circular references for serialization.
  • Adds a function to filter a snapshot to contain only active sessions, windows and panes.
  • Adds a test script to demonstrate the snapshot functionality, including creation, read-only enforcement, filtering, and serialization.

Copy link

sourcery-ai bot commented Feb 28, 2025

Reviewer's Guide by Sourcery

This pull request introduces snapshot functionality for libtmux, allowing users to create read-only copies of tmux server, session, window, and pane objects. It includes features for filtering snapshots and converting them to dictionaries for serialization. A test script is also included to demonstrate the functionality.

Class diagram for ServerSnapshot

classDiagram
    class ServerSnapshot {
        -created_at: datetime
        -sessions_snapshot: list[SessionSnapshot]
        -windows_snapshot: list[WindowSnapshot]
        -panes_snapshot: list[PaneSnapshot]
        -server: Server
        +__post_init__()
        +__setattr__(name: str, value: t.Any)
        +__enter__()
        +__exit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None)
        +cmd(*args: t.Any, **kwargs: t.Any)
        +is_alive() : bool
        +raise_if_dead() : t.NoReturn
        +sessions : QueryList[SessionSnapshot]
        +windows : QueryList[WindowSnapshot]
        +panes : QueryList[PaneSnapshot]
        +from_server(server: Server, include_content: bool = True) : ServerSnapshot
    }
    ServerSnapshot -- SessionSnapshot : contains
    Server <|-- ServerSnapshot
Loading

Class diagram for SessionSnapshot

classDiagram
    class SessionSnapshot {
        -created_at: datetime
        -server_snapshot: ServerSnapshot | None
        -windows_snapshot: list[WindowSnapshot]
        -session: Session
        +__post_init__()
        +__setattr__(name: str, value: t.Any)
        +__enter__()
        +__exit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None)
        +cmd(*args: t.Any, **kwargs: t.Any)
        +windows : QueryList[WindowSnapshot]
        +server : ServerSnapshot | None
        +active_window : WindowSnapshot | None
        +active_pane : PaneSnapshot | None
        +from_session(session: Session, capture_content: bool = False, server_snapshot: ServerSnapshot | None) : SessionSnapshot
    }
    SessionSnapshot -- WindowSnapshot : contains
    Session <|-- SessionSnapshot
Loading

Class diagram for WindowSnapshot

classDiagram
    class WindowSnapshot {
        -created_at: datetime
        -session_snapshot: SessionSnapshot | None
        -panes_snapshot: list[PaneSnapshot]
        -window: Window
        +__post_init__()
        +__setattr__(name: str, value: t.Any)
        +__enter__()
        +__exit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None)
        +cmd(*args: t.Any, **kwargs: t.Any)
        +panes : QueryList[PaneSnapshot]
        +session : SessionSnapshot | None
        +active_pane : PaneSnapshot | None
        +from_window(window: Window, capture_content: bool = True, session_snapshot: SessionSnapshot | None) : WindowSnapshot
    }
    WindowSnapshot -- PaneSnapshot : contains
    Window <|-- WindowSnapshot
Loading

Class diagram for PaneSnapshot

classDiagram
    class PaneSnapshot {
        -pane_content: list[str] | None
        -created_at: datetime
        -window_snapshot: WindowSnapshot | None
        -pane: Pane
        +__post_init__()
        +__setattr__(name: str, value: t.Any)
        +__enter__()
        +__exit__(exc_type: type[BaseException] | None, exc_val: BaseException | None, exc_tb: TracebackType | None)
        +cmd(*args: t.Any, **kwargs: t.Any)
        +capture_pane(*args: t.Any, **kwargs: t.Any) : list[str]
        +window : WindowSnapshot | None
        +session : SessionSnapshot | None
        +from_pane(pane: Pane, capture_content: bool = True, window_snapshot: WindowSnapshot | None) : PaneSnapshot
    }
    Pane <|-- PaneSnapshot
Loading

File-Level Changes

Change Details Files
Introduces snapshot classes for tmux objects (Server, Session, Window, Pane) that provide read-only copies of the objects, preventing modifications and tmux command execution.
  • Adds PaneSnapshot, WindowSnapshot, SessionSnapshot, and ServerSnapshot classes.
  • Implements read-only behavior using __setattr__ to prevent attribute modification.
  • Overrides the cmd method to raise NotImplementedError, preventing tmux command execution.
  • Adds methods to create snapshots from live tmux objects (from_pane, from_window, from_session, from_server).
  • Adds properties to navigate the snapshot hierarchy (e.g., pane.window, window.session).
src/libtmux/snapshot.py
Adds functionality to filter a snapshot hierarchy based on a filter function.
  • Implements the filter_snapshot function to recursively filter snapshot objects.
  • The filter function determines whether an object is kept or removed from the snapshot.
  • Maintains parent-child relationships in the filtered snapshot.
  • Returns a new filtered snapshot or None if everything is filtered out.
src/libtmux/snapshot.py
Adds functionality to convert a snapshot object to a dictionary, avoiding circular references.
  • Implements the snapshot_to_dict function to convert snapshot objects to dictionaries.
  • Skips internal and parent reference fields to prevent circular references.
  • Handles lists of snapshots and nested snapshots recursively.
  • Handles QueryList objects by converting them to lists of dictionaries.
  • Handles datetime objects by converting them to strings.
src/libtmux/snapshot.py
Adds functionality to filter a snapshot to include only active sessions, windows, and panes.
  • Implements the snapshot_active_only function to filter a server snapshot.
  • Uses the filter_snapshot function with a filter that checks the 'pane_active' and 'window_active' attributes.
  • Raises a ValueError if no active objects are found.
src/libtmux/snapshot.py
Adds a test script to demonstrate the snapshot functionality.
  • Creates a test server.
  • Takes a complete snapshot of the server.
  • Prints information about the snapshot, such as the number of sessions, windows, and panes.
  • Tests that the snapshot is read-only by attempting to execute a command.
  • Demonstrates filtering the snapshot to get only active components.
  • Demonstrates serializing the snapshot to a dictionary and outputting it to JSON.
tests/test_snapshot.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@tony tony closed this Feb 28, 2025
@tony tony changed the title WIP: Snapshot Removed (should make this after frozen dataclasses) Feb 28, 2025
@tony tony deleted the snapshots branch February 28, 2025 11:31
@tony tony restored the snapshots branch February 28, 2025 11:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant