def leave_playing_mode(manually_triggered=False):
    """Perform setups to leave playing mode.

    Sometimes, leaving playing mode means that we'll reenter it.
    For instance, when performing a system testing session (performing
    multiple system tests).

    Parameters
    ==========

    manually_triggered (bool)
        Whether leaving playing mode was triggered manually by the user
        or not. Only relevant when in the middle of a system testing
        session.
    """

    ### clear stored data
    clear_data()

    ### act depending on...
    ### - whether we are performing tests or not
    ### - whether there are more pending tests
    ### - whether leaving playing mode was manually or naturally triggered

    if PLAY_REFS.ongoing_test is not None:

        ## if leaving playing mode was triggered by the user

        if manually_triggered:

            ##

            APP_REFS.playsupport.finish_system_testing_session_earlier(
                PLAY_REFS.ongoing_test
            )

            ##

            PLAY_REFS.pending_test_cases.clear()
            PLAY_REFS.ongoing_test = None

            ##

            if APP_REFS.system_testing_set:
                APP_REFS.system_testing_set = False

            ##

            raise (
                ResetAppException(
                    mode='normal',
                    data={'left_system_testing_midway': True},
                )
            )

        else:

            # clear set of test frames
            PLAY_REFS.test_frames.clear()

            # perform final assertions, store test results and perform teardown

            APP_REFS.playsupport.finish_test_case(
                PLAY_REFS.ongoing_test,
                PLAY_REFS.last_frame_index,
            )

            # act according to existence of pending test cases

            if PLAY_REFS.pending_test_cases:
                raise ResetAppException(mode='play')

            else:

                PLAY_REFS.ongoing_test = None

                tests_report_data = (
                    APP_REFS
                    .playsupport
                    .finish_system_testing_session_and_get_report()
                )

                raise (

                    ResetAppException(
                        mode='normal',
                        data={'tests_report_data': tests_report_data},
                    )

                )

    else:
        raise ResetAppException(mode='normal')
Perform setups to leave playing mode.

    Sometimes, leaving playing mode means that we'll reenter it.
    For instance, when performing a system testing session (performing
    multiple system tests).

    Parameters
    ==========

    manually_triggered (bool)
        Whether leaving playing mode was triggered manually by the user
        or not. Only relevant when in the middle of a system testing
        session.
    
nodezator/pygamesetup/services/play.py