<commit_before>import logging
import logging.handlers
import pickle
import sys
import traceback
import multiprocessing
from typing import Optional

import click
import colorlog
from PyQt5 import QtWidgets

from cct.core2.instrument.instrument import Instrument
from cct.qtgui2.main.mainwindow import MainWindow
from cct.qtgui2.main.logindialog import LoginDialog
from cct.core2.config import Config
import cct.qtgui2.processingmain
from cct.core2.instrument.components.datareduction.datareductionpipeline import DataReductionPipeLine
from cct.core2.instrument.components.io import IO

# logging.basicConfig()
logging.root.setLevel(logging.DEBUG)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

logging.getLogger('matplotlib').setLevel(logging.INFO)
#logging.getLogger('matplotlib.font_manager').setLevel(logging.INFO)
handler = colorlog.StreamHandler()
handler.setFormatter(colorlog.ColoredFormatter(
    '%(log_color)s%(asctime)s %(levelname)s:%(name)s:%(message)s'
))

logging.root.addHandler(handler)


def excepthook(exctype, exc, tb):
    logging.root.critical(f'Uncaught exception: {repr(exc)}\nTraceback:\n{"".join(traceback.format_tb(tb))}')


@click.group()
def main():
    pass


@main.command()
@click.option('--config', '-c', default='config/cct.pickle', help='Config file', type=click.Path(exists=True, file_okay=True, dir_okay=False, writable=False, readable=True, allow_dash=False, ))
@click.option('--online/--offline', default=False, help='Connect to devices')
@click.option('--root/--no-root', '-r', default=False, help='Skip login', type=bool, is_flag=True)
def daq(config:str, online: bool, root: bool):
    """Open the data acquisition GUI mode"""
    handler = logging.handlers.TimedRotatingFileHandler('log/cct4.log', 'D', 1, encoding='utf-8', backupCount=0)
    handler.addFilter(logging.Filter('cct'))
    formatter = logging.Formatter('%(asctime)s:%(name)s:%(levelname)s:%(message)s')
    handler.setFormatter(formatter)
    logging.root.addHandler(handler)

    multiprocessing.set_start_method('forkserver')  # the default 'fork' method is not appropriate for multi-threaded programs, e.g. with PyQt.
    sys.excepthook = excepthook
    app = QtWidgets.QApplication(sys.argv)
    logger.debug('Instantiating Instrument()')
    instrument = Instrument(configfile=config)
    if not root:
        logindialog = LoginDialog()
        logindialog.setOffline(not online)
        while True:
            logger.debug('Starting new logindialog iteration')
            result = logindialog.exec()
            if result == QtWidgets.QDialog.Accepted:<commit_msg>New command-line argument: --die-on-error.<commit_after>import logging
import logging.handlers
import pickle
import sys
import traceback
import multiprocessing
from typing import Optional

import click
import colorlog
from PyQt5 import QtWidgets

from cct.core2.instrument.instrument import Instrument
from cct.qtgui2.main.mainwindow import MainWindow
from cct.qtgui2.main.logindialog import LoginDialog
from cct.core2.config import Config
import cct.qtgui2.processingmain
from cct.core2.instrument.components.datareduction.datareductionpipeline import DataReductionPipeLine
from cct.core2.instrument.components.io import IO

# logging.basicConfig()
logging.root.setLevel(logging.DEBUG)
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

logging.getLogger('matplotlib').setLevel(logging.INFO)
#logging.getLogger('matplotlib.font_manager').setLevel(logging.INFO)
handler = colorlog.StreamHandler()
handler.setFormatter(colorlog.ColoredFormatter(
    '%(log_color)s%(asctime)s %(levelname)s:%(name)s:%(message)s'
))

logging.root.addHandler(handler)


def excepthook(exctype, exc, tb):
    logging.root.critical(f'Uncaught exception: {repr(exc)}\nTraceback:\n{"".join(traceback.format_tb(tb))}')


@click.group()
def main():
    pass


@main.command()
@click.option('--config', '-c', default='config/cct.pickle', help='Config file', type=click.Path(exists=True, file_okay=True, dir_okay=False, writable=False, readable=True, allow_dash=False, ))
@click.option('--online/--offline', default=False, help='Connect to devices')
@click.option('--root/--no-root', '-r', default=False, help='Skip login', type=bool, is_flag=True)
@click.option('--die-on-error/--dont-die-on-error', '-d', default=False, help='Die on an unhandled exception (for debugging)', type=bool, is_flag=True)
def daq(config:str, online: bool, root: bool, die_on_error: bool):
    """Open the data acquisition GUI mode"""
    handler = logging.handlers.TimedRotatingFileHandler('log/cct4.log', 'D', 1, encoding='utf-8', backupCount=0)
    handler.addFilter(logging.Filter('cct'))
    formatter = logging.Formatter('%(asctime)s:%(name)s:%(levelname)s:%(message)s')
    handler.setFormatter(formatter)
    logging.root.addHandler(handler)

    multiprocessing.set_start_method('forkserver')  # the default 'fork' method is not appropriate for multi-threaded programs, e.g. with PyQt.
    if not die_on_error:
        sys.excepthook = excepthook
    app = QtWidgets.QApplication(sys.argv)
    logger.debug('Instantiating Instrument()')
    instrument = Instrument(configfile=config)
    if not root:
        logindialog = LoginDialog()
        logindialog.setOffline(not online)
        while True:
            logger.debug('Starting new logindialog iteration')
            result = logindialog.exec()
            if result == QtWidgets.QDialog.Accepted:
{'fraction_non_alphanumeric': 0.1332351858667648, 'fraction_numerical': 0.004416635995583364, 'mean_word_length': 5.371629542790153, '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': '864475', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1716, 'n_words': 341}