|
1 | | -"""Package for the GUI. |
| 1 | +"""openadapt.app module. |
2 | 2 |
|
3 | | -Module: __init__.py |
| 3 | +This module provides core functionality for the OpenAdapt application. |
4 | 4 | """ |
| 5 | + |
| 6 | +from datetime import datetime |
| 7 | +import multiprocessing |
| 8 | +import pathlib |
| 9 | +import time |
| 10 | + |
| 11 | +from openadapt.record import record |
| 12 | +from openadapt.utils import WrapStdout |
| 13 | + |
| 14 | +__all__ = [ |
| 15 | + "RecordProc", |
| 16 | + "record_proc", |
| 17 | + "stop_record", |
| 18 | + "is_recording", |
| 19 | + "quick_record", |
| 20 | + "FPATH", |
| 21 | +] |
| 22 | + |
| 23 | +# Define FPATH |
| 24 | +FPATH = pathlib.Path(__file__).parent |
| 25 | + |
| 26 | + |
| 27 | +class RecordProc: |
| 28 | + """Class to manage the recording process.""" |
| 29 | + |
| 30 | + def __init__(self) -> None: |
| 31 | + """Initialize the RecordProc class.""" |
| 32 | + self.terminate_processing = multiprocessing.Event() |
| 33 | + self.terminate_recording = multiprocessing.Event() |
| 34 | + self.record_proc: multiprocessing.Process = None |
| 35 | + self.has_initiated_stop = False |
| 36 | + |
| 37 | + def set_terminate_processing(self) -> multiprocessing.Event: |
| 38 | + """Set the terminate event.""" |
| 39 | + return self.terminate_processing.set() |
| 40 | + |
| 41 | + def terminate(self) -> None: |
| 42 | + """Terminate the recording process.""" |
| 43 | + self.record_proc.terminate() |
| 44 | + |
| 45 | + def reset(self) -> None: |
| 46 | + """Reset the recording process.""" |
| 47 | + self.terminate_processing.clear() |
| 48 | + self.terminate_recording.clear() |
| 49 | + self.record_proc = None |
| 50 | + self.has_initiated_stop = False |
| 51 | + |
| 52 | + def wait(self) -> None: |
| 53 | + """Wait for the recording process to finish.""" |
| 54 | + while True: |
| 55 | + if self.terminate_recording.is_set(): |
| 56 | + self.record_proc.terminate() |
| 57 | + return |
| 58 | + time.sleep(0.1) |
| 59 | + |
| 60 | + def is_running(self) -> bool: |
| 61 | + """Check if the recording process is running.""" |
| 62 | + if self.record_proc is not None and not self.record_proc.is_alive(): |
| 63 | + self.reset() |
| 64 | + return self.record_proc is not None |
| 65 | + |
| 66 | + def start(self, func: callable, args: tuple, kwargs: dict) -> None: |
| 67 | + """Start the recording process.""" |
| 68 | + self.record_proc = multiprocessing.Process( |
| 69 | + target=WrapStdout(func), |
| 70 | + args=args, |
| 71 | + kwargs=kwargs, |
| 72 | + ) |
| 73 | + self.record_proc.start() |
| 74 | + |
| 75 | + |
| 76 | +record_proc = RecordProc() |
| 77 | + |
| 78 | + |
| 79 | +def stop_record() -> None: |
| 80 | + """Stop the current recording session.""" |
| 81 | + global record_proc |
| 82 | + if record_proc.is_running() and not record_proc.has_initiated_stop: |
| 83 | + record_proc.set_terminate_processing() |
| 84 | + |
| 85 | + # wait for process to terminate |
| 86 | + record_proc.wait() |
| 87 | + record_proc.reset() |
| 88 | + |
| 89 | + |
| 90 | +def is_recording() -> bool: |
| 91 | + """Check if a recording session is currently active.""" |
| 92 | + global record_proc |
| 93 | + return record_proc.is_running() |
| 94 | + |
| 95 | + |
| 96 | +def quick_record( |
| 97 | + task_description: str | None = None, |
| 98 | + status_pipe: multiprocessing.connection.Connection | None = None, |
| 99 | +) -> None: |
| 100 | + """Run a recording session.""" |
| 101 | + global record_proc |
| 102 | + task_description = task_description or datetime.now().strftime("%d/%m/%Y %H:%M:%S") |
| 103 | + record_proc.start( |
| 104 | + record, |
| 105 | + ( |
| 106 | + task_description, |
| 107 | + record_proc.terminate_processing, |
| 108 | + record_proc.terminate_recording, |
| 109 | + status_pipe, |
| 110 | + ), |
| 111 | + { |
| 112 | + "log_memory": False, |
| 113 | + }, |
| 114 | + ) |
0 commit comments