xref: /dpdk/dts/framework/remote_session/interactive_shell.py (revision 92439dc9ac51529de448f4484864e2ef7b1bc07c)
1840b1e01SJuraj Linkeš# SPDX-License-Identifier: BSD-3-Clause
2840b1e01SJuraj Linkeš# Copyright(c) 2023 University of New Hampshire
3fd8cd8eeSLuca Vizzarro# Copyright(c) 2024 Arm Limited
4840b1e01SJuraj Linkeš
52b648cd4SJeremy Spewock"""Interactive shell with manual stop/start functionality.
6840b1e01SJuraj Linkeš
72b648cd4SJeremy SpewockProvides a class that doesn't require being started/stopped using a context manager and can instead
82b648cd4SJeremy Spewockbe started and stopped manually, or have the stopping process be handled at the time of garbage
92b648cd4SJeremy Spewockcollection.
10840b1e01SJuraj Linkeš"""
11840b1e01SJuraj Linkeš
12*92439dc9SJeremy Spewockimport weakref
13*92439dc9SJeremy Spewockfrom typing import ClassVar
14*92439dc9SJeremy Spewock
152b648cd4SJeremy Spewockfrom .single_active_interactive_shell import SingleActiveInteractiveShell
16840b1e01SJuraj Linkeš
17840b1e01SJuraj Linkeš
182b648cd4SJeremy Spewockclass InteractiveShell(SingleActiveInteractiveShell):
192b648cd4SJeremy Spewock    """Adds manual start and stop functionality to interactive shells.
20840b1e01SJuraj Linkeš
212b648cd4SJeremy Spewock    Like its super-class, this class should not be instantiated directly and should instead be
22*92439dc9SJeremy Spewock    extended. This class also provides an option for automated cleanup of the application using a
23*92439dc9SJeremy Spewock    weakref and a finalize class. This finalize class allows for cleanup of the class at the time
24*92439dc9SJeremy Spewock    of garbage collection and also ensures that cleanup only happens once. This way if a user
25*92439dc9SJeremy Spewock    initiates the closing of the shell manually it is not repeated at the time of garbage
26*92439dc9SJeremy Spewock    collection.
27840b1e01SJuraj Linkeš    """
28840b1e01SJuraj Linkeš
29*92439dc9SJeremy Spewock    _finalizer: weakref.finalize
30*92439dc9SJeremy Spewock    #: One attempt should be enough for shells which don't have to worry about other instances
31*92439dc9SJeremy Spewock    #: closing before starting a new one.
32*92439dc9SJeremy Spewock    _init_attempts: ClassVar[int] = 1
33*92439dc9SJeremy Spewock
34bfad0948SLuca Vizzarro    def start_application(self) -> None:
35*92439dc9SJeremy Spewock        """Start the application.
36*92439dc9SJeremy Spewock
37*92439dc9SJeremy Spewock        After the application has started, use :class:`weakref.finalize` to manage cleanup.
38*92439dc9SJeremy Spewock        """
392b648cd4SJeremy Spewock        self._start_application()
40*92439dc9SJeremy Spewock        self._finalizer = weakref.finalize(self, self._close)
41840b1e01SJuraj Linkeš
42840b1e01SJuraj Linkeš    def close(self) -> None:
43*92439dc9SJeremy Spewock        """Free all resources using :class:`weakref.finalize`."""
44*92439dc9SJeremy Spewock        self._finalizer()
45