xref: /dpdk/dts/framework/remote_session/interactive_shell.py (revision 92439dc9ac51529de448f4484864e2ef7b1bc07c)
1# SPDX-License-Identifier: BSD-3-Clause
2# Copyright(c) 2023 University of New Hampshire
3# Copyright(c) 2024 Arm Limited
4
5"""Interactive shell with manual stop/start functionality.
6
7Provides a class that doesn't require being started/stopped using a context manager and can instead
8be started and stopped manually, or have the stopping process be handled at the time of garbage
9collection.
10"""
11
12import weakref
13from typing import ClassVar
14
15from .single_active_interactive_shell import SingleActiveInteractiveShell
16
17
18class InteractiveShell(SingleActiveInteractiveShell):
19    """Adds manual start and stop functionality to interactive shells.
20
21    Like its super-class, this class should not be instantiated directly and should instead be
22    extended. This class also provides an option for automated cleanup of the application using a
23    weakref and a finalize class. This finalize class allows for cleanup of the class at the time
24    of garbage collection and also ensures that cleanup only happens once. This way if a user
25    initiates the closing of the shell manually it is not repeated at the time of garbage
26    collection.
27    """
28
29    _finalizer: weakref.finalize
30    #: One attempt should be enough for shells which don't have to worry about other instances
31    #: closing before starting a new one.
32    _init_attempts: ClassVar[int] = 1
33
34    def start_application(self) -> None:
35        """Start the application.
36
37        After the application has started, use :class:`weakref.finalize` to manage cleanup.
38        """
39        self._start_application()
40        self._finalizer = weakref.finalize(self, self._close)
41
42    def close(self) -> None:
43        """Free all resources using :class:`weakref.finalize`."""
44        self._finalizer()
45