<commit_before>"""
The functions in this module provide a way of pausing code execution until
canvases are closed. This can be useful when testing code and you don't want to
keep the objects alive outside of your function.

The wait function can be called repeatedly to pause multiple times.

wait_for_zero_canvases()
    Keeps root alive until CTRL-c is pressed or all canvases are closed

wait_for_zero_canvases(middle_mouse_close=True)
    allows canvases to be closed with the middle mouse button (see below)

wait is shorthand for wait_for_zero_canvases
"""

import threading

import ROOT

from .canvas_events import attach_event_handler

_processRootEvents = getattr(ROOT.PyGUIThread, "_Thread__target", None)
_finishSchedule = getattr(ROOT.PyGUIThread, "finishSchedule", None)

def start_new_gui_thread():
    """
    Attempt to start a new GUI thread, if possible.

    It is only possible to start one if there was one running on module import.
    """
    assert not ROOT.PyGUIThread.isAlive(), "GUI thread already running!"

    assert _processRootEvents, (
        "GUI thread wasn't started when rootwait was imported, "
        "so it can't be restarted")

    ROOT.keeppolling = 1
    ROOT.PyGUIThread = threading.Thread(None, _processRootEvents, None, (ROOT,))

    ROOT.PyGUIThread.finishSchedule = _finishSchedule
    ROOT.PyGUIThread.setDaemon( 1 )
    ROOT.PyGUIThread.start()

def stop_gui_thread():
    """
    Try to stop the GUI thread. If it was running returns True, otherwise False.
    """
    if not ROOT.PyGUIThread.isAlive():
        return False

    ROOT.keeppolling = 0
    ROOT.PyGUIThread.finishSchedule()
    ROOT.PyGUIThread.join()
    return True

@ROOT.TPyDispatcher
def count_canvases():
    """
    Count the number of active canvases and finish gApplication.Run() if there
    are none remaining.

    incpy.ignore
    """
    if not get_visible_canvases():
        ROOT.gSystem.ExitLoop()

@ROOT.TPyDispatcher
def exit_application_loop():
    """
    Signal handler for CTRL-c to cause gApplication.Run() to finish.

    incpy.ignore
    """
    print " caught, will stop waiting"
    ROOT.gSystem.ExitLoop()

def get_visible_canvases():
    """
    Return a list of active GUI canvases
    (as opposed to invisible Batch canvases)
    """
    return [c for c in ROOT.gROOT.GetListOfCanvases() if not c.IsBatch()]

def run_application_until_done():
    print "There are canvases open. Waiting until exit."
    had_gui_thread = stop_gui_thread()

    ROOT.gApplication._threaded = True
    ROOT.gApplication.Run(True)
<commit_msg>rm print during wait for canvases to close<commit_after>"""
The functions in this module provide a way of pausing code execution until
canvases are closed. This can be useful when testing code and you don't want to
keep the objects alive outside of your function.

The wait function can be called repeatedly to pause multiple times.

wait_for_zero_canvases()
    Keeps root alive until CTRL-c is pressed or all canvases are closed

wait_for_zero_canvases(middle_mouse_close=True)
    allows canvases to be closed with the middle mouse button (see below)

wait is shorthand for wait_for_zero_canvases
"""

import threading

import ROOT

from .canvas_events import attach_event_handler

_processRootEvents = getattr(ROOT.PyGUIThread, "_Thread__target", None)
_finishSchedule = getattr(ROOT.PyGUIThread, "finishSchedule", None)

def start_new_gui_thread():
    """
    Attempt to start a new GUI thread, if possible.

    It is only possible to start one if there was one running on module import.
    """
    assert not ROOT.PyGUIThread.isAlive(), "GUI thread already running!"

    assert _processRootEvents, (
        "GUI thread wasn't started when rootwait was imported, "
        "so it can't be restarted")

    ROOT.keeppolling = 1
    ROOT.PyGUIThread = threading.Thread(None, _processRootEvents, None, (ROOT,))

    ROOT.PyGUIThread.finishSchedule = _finishSchedule
    ROOT.PyGUIThread.setDaemon( 1 )
    ROOT.PyGUIThread.start()

def stop_gui_thread():
    """
    Try to stop the GUI thread. If it was running returns True, otherwise False.
    """
    if not ROOT.PyGUIThread.isAlive():
        return False

    ROOT.keeppolling = 0
    ROOT.PyGUIThread.finishSchedule()
    ROOT.PyGUIThread.join()
    return True

@ROOT.TPyDispatcher
def count_canvases():
    """
    Count the number of active canvases and finish gApplication.Run() if there
    are none remaining.

    incpy.ignore
    """
    if not get_visible_canvases():
        ROOT.gSystem.ExitLoop()

@ROOT.TPyDispatcher
def exit_application_loop():
    """
    Signal handler for CTRL-c to cause gApplication.Run() to finish.

    incpy.ignore
    """
    print " caught, will stop waiting"
    ROOT.gSystem.ExitLoop()

def get_visible_canvases():
    """
    Return a list of active GUI canvases
    (as opposed to invisible Batch canvases)
    """
    return [c for c in ROOT.gROOT.GetListOfCanvases() if not c.IsBatch()]

def run_application_until_done():

    had_gui_thread = stop_gui_thread()

    ROOT.gApplication._threaded = True
    ROOT.gApplication.Run(True)

{'fraction_non_alphanumeric': 0.07853096307872631, 'fraction_numerical': 0.001172103926548154, 'mean_word_length': 3.6084608460846086, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4015556', 'n_tokens_mistral': 1633, 'n_tokens_neox': 1521, 'n_words': 523}