import abc
import os
from time import sleep
from fortrace.core.qemu_monitor import QEMUMonitorSession
from fortrace.utility.applications.application import (
ApplicationType,
GenericApplication,
ParentNotifier,
)
[docs]
class GenericTextEditor(GenericApplication):
"""OS-independent representation of a graphical text editor."""
def __init__(
self, name: str, qs: QEMUMonitorSession, parent_notifier: ParentNotifier
):
super().__init__(name, ApplicationType.TEXT_EDITOR, qs, parent_notifier)
[docs]
def save(self):
"""Save the currently active file.
Might open a 'Save as...' dialogue, if destination is unknown. This behavior is undefined,
thus this method should not be used on newly created files without a name. Then use save_as
"""
self._qs.send_key_combination("ctrl-s")
[docs]
def save_as(self, path: os.PathLike, overwrite_file: bool = False):
"""Save the currently active file to the given location.
Args:
path: destination path of the file to be saved
overwrite_file: should another file with the same destination
address be overwritten?
"""
# TODO: handle overwrite popup
self._qs.send_key_combination("ctrl-shift-s")
self._qs.send_key_combination("ctrl-l")
self._qs.send_key_combination("ctrl-a")
sleep(0.5)
self._qs.send_text(str(path), True)
sleep(0.5) # wait for file dialog to close
[docs]
@abc.abstractmethod
def open_file(self, path: os.PathLike):
"""Open file in text editor.
Args:
path: path of the file to be opened
"""
pass
[docs]
def send_file(self, path: os.PathLike):
"""Send a whole file to be typed in at the guest.
Saving must be done via save_as method.
Args:
path: path to the file to be sent
"""
with open(path, "r") as file:
for line in file:
self._qs.send_text(
line, True
) # as '\n', '\r\n' is ignored by QEMUMonitor