<commit_before>import logging as log
import os.path

from PySide import QtCore, QtGui, QtDeclarative

from ui.canvaswidget import CanvasWidget
from ui.fixturewidget import FixtureWidget
from util.config import Config
from models.scene import Scene
from controllers.scenecontroller import SceneController
from controllers.netcontroller import NetController


class FireSimGUI(QtCore.QObject):

    def __init__(self, args=None):
        QtCore.QObject.__init__(self)

        self.app = QtGui.QApplication(["FireSim"])
        self.args = args
        self.config = Config("data/config.json")

        if self.args.profile:
            try:
                import yappi
                yappi.start()
            except ImportError:
                log.error("Could not enable YaPPI profiling")

        self._selected_fixture_strand = 0
        self._selected_fixture_address = 0
        self._selected_fixture_pixels = 0

        self.selected_fixture = None
        self.is_blurred = False

        self.scene = Scene(os.path.join(self.config.get("scene_root"), self.args.scene) + ".json")
        self.scenecontroller = SceneController(app=self, scene=self.scene)

        QtDeclarative.qmlRegisterType(CanvasWidget, "FireSim", 1, 0, "SimCanvas")
        QtDeclarative.qmlRegisterType(FixtureWidget, "FireSim", 1, 0, "Fixture")

        self.view = QtDeclarative.QDeclarativeView()

        self.view.setWindowTitle("FireSim")
        self.view.setResizeMode(QtDeclarative.QDeclarativeView.SizeRootObjectToView)

        self.view.closeEvent = self.on_close

        self.context = self.view.rootContext()
        self.context.setContextProperty('main', self)

        self.fixture_info_list = []
        self.context.setContextProperty('fixtureInfoModel', self.fixture_info_list)

        self.view.setSource(QtCore.QUrl('ui/qml/FireSimGUI.qml'))

        self.root = self.view.rootObject()
        self.item_frame = self.root.findChild(QtDeclarative.QDeclarativeItem)
        self.canvas = self.root.findChild(CanvasWidget)
        self.canvas.gui = self

        cw, ch = self.scenecontroller.scene.extents()
        self.canvas.setWidth(cw)
        self.canvas.setHeight(ch)

        self.scenecontroller.set_canvas(self.canvas)



        self.root.backdrop_showhide_callback.connect(self.on_btn_backdrop_showhide)
        self.root.labels_showhide_callback.connect(self.on_btn_labels_showhide)
        self.root.lock_callback.connect(self.on_btn_lock)
        self.root.show_center_callback.connect(self.on_btn_show_center)
        self.root.toggle_blurred_callback.connect(self.on_btn_toggle_blurred)

        #self.net_thread = QtCore.QThread()
        #self.net_thread.start()
        self.netcontroller = NetController(self)
        #self.netcontroller.moveToThread(self.net_thread)
        #self.netcontroller.start.emit()

        self.net_stats_timer = QtCore.QTimer()
        self.net_stats_timer.setInterval(1000)
        self.net_stats_timer.timeout.connect(self.update_net_stats)
        self.net_stats_timer.start()

        self.netcontroller.data_received.connect(self.on_network_event)
        self.scenecontroller.new_frame.connect(self.netcontroller.frame_complete)
        self.netcontroller.data_received.connect(self.scenecontroller.process_command)

        self.view.setFixedSize(max(640, cw + 130), max(480, ch))

        log.info("FireSimGUI Ready.")
        self.view.show()
        #self.view.showFullScreen()
        #self.view.setGeometry(self.app.desktop().availableGeometry())

    @QtCore.Slot()
    def quit(self):
        self.app.quit()

    def run(self):
        return self.app.exec_()

    def on_close(self, e):
        self.netcontroller.running = False
        self.net_thread.quit()
        if self.args.profile:
            try:
                import yappi
                yappi.get_func_stats().print_all()
            except ImportError:
                pass

    @QtCore.Slot()
    def update_net_stats(self):
        self.canvas.net_stats = self.netcontroller.get_stats()<commit_msg>Fix crash on exit that breaks profiling<commit_after>import logging as log
import os.path

from PySide import QtCore, QtGui, QtDeclarative

from ui.canvaswidget import CanvasWidget
from ui.fixturewidget import FixtureWidget
from util.config import Config
from models.scene import Scene
from controllers.scenecontroller import SceneController
from controllers.netcontroller import NetController


class FireSimGUI(QtCore.QObject):

    def __init__(self, args=None):
        QtCore.QObject.__init__(self)

        self.app = QtGui.QApplication(["FireSim"])
        self.args = args
        self.config = Config("data/config.json")

        if self.args.profile:
            try:
                import yappi
                yappi.start()
            except ImportError:
                log.error("Could not enable YaPPI profiling")

        self._selected_fixture_strand = 0
        self._selected_fixture_address = 0
        self._selected_fixture_pixels = 0

        self.selected_fixture = None
        self.is_blurred = False

        self.scene = Scene(os.path.join(self.config.get("scene_root"), self.args.scene) + ".json")
        self.scenecontroller = SceneController(app=self, scene=self.scene)

        QtDeclarative.qmlRegisterType(CanvasWidget, "FireSim", 1, 0, "SimCanvas")
        QtDeclarative.qmlRegisterType(FixtureWidget, "FireSim", 1, 0, "Fixture")

        self.view = QtDeclarative.QDeclarativeView()

        self.view.setWindowTitle("FireSim")
        self.view.setResizeMode(QtDeclarative.QDeclarativeView.SizeRootObjectToView)

        self.view.closeEvent = self.on_close

        self.context = self.view.rootContext()
        self.context.setContextProperty('main', self)

        self.fixture_info_list = []
        self.context.setContextProperty('fixtureInfoModel', self.fixture_info_list)

        self.view.setSource(QtCore.QUrl('ui/qml/FireSimGUI.qml'))

        self.root = self.view.rootObject()
        self.item_frame = self.root.findChild(QtDeclarative.QDeclarativeItem)
        self.canvas = self.root.findChild(CanvasWidget)
        self.canvas.gui = self

        cw, ch = self.scenecontroller.scene.extents()
        self.canvas.setWidth(cw)
        self.canvas.setHeight(ch)

        self.scenecontroller.set_canvas(self.canvas)



        self.root.backdrop_showhide_callback.connect(self.on_btn_backdrop_showhide)
        self.root.labels_showhide_callback.connect(self.on_btn_labels_showhide)
        self.root.lock_callback.connect(self.on_btn_lock)
        self.root.show_center_callback.connect(self.on_btn_show_center)
        self.root.toggle_blurred_callback.connect(self.on_btn_toggle_blurred)

        #self.net_thread = QtCore.QThread()
        #self.net_thread.start()
        self.netcontroller = NetController(self)
        #self.netcontroller.moveToThread(self.net_thread)
        #self.netcontroller.start.emit()

        self.net_stats_timer = QtCore.QTimer()
        self.net_stats_timer.setInterval(1000)
        self.net_stats_timer.timeout.connect(self.update_net_stats)
        self.net_stats_timer.start()

        self.netcontroller.data_received.connect(self.on_network_event)
        self.scenecontroller.new_frame.connect(self.netcontroller.frame_complete)
        self.netcontroller.data_received.connect(self.scenecontroller.process_command)

        self.view.setFixedSize(max(640, cw + 130), max(480, ch))

        log.info("FireSimGUI Ready.")
        self.view.show()
        #self.view.showFullScreen()
        #self.view.setGeometry(self.app.desktop().availableGeometry())

    @QtCore.Slot()
    def quit(self):
        self.app.quit()

    def run(self):
        return self.app.exec_()

    def on_close(self, e):
        self.netcontroller.running = False
        #self.net_thread.quit()
        if self.args.profile:
            try:
                import yappi
                yappi.get_func_stats().print_all()
            except ImportError:
                pass

    @QtCore.Slot()
    def update_net_stats(self):
        self.canvas.net_stats = self.netcontroller.get_stats()
{'fraction_non_alphanumeric': 0.10055521283158544, 'fraction_numerical': 0.004935225169648365, 'mean_word_length': 2.8581627796287483, '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': '1592258', 'n_tokens_mistral': 2553, 'n_tokens_neox': 2415, 'n_words': 353}