<commit_before>"""GUI prompt to bind a new server"""
import os
from nxdrive.client import Unauthorized
from nxdrive.logging_config import get_logger

log = get_logger(__name__)

# Keep QT an optional dependency for now
QtGui, QDialog = None, object
try:
    from PySide import QtGui
    QDialog = QtGui.QDialog
except ImportError:
    log.warn("QT / PySide is not installed: GUI is disabled")
    pass


def find_icon(icon_filename):
    import nxdrive
    nxdrive_path = os.path.dirname(nxdrive.__file__)
    icons_path = os.path.join(nxdrive_path, 'data', 'icons')

    frozen_suffix = os.path.join('library.zip', 'nxdrive')
    if nxdrive_path.endswith(frozen_suffix):
        # Frozen distribution of nxdrive, data is out of the zip
        icons_path = nxdrive_path.replace(frozen_suffix, 'icons')

    if not os.path.exists(icons_path):
        raise IOError("Could not find the icons folder at: " + icons_path)

    icon_filepath = os.path.join(icons_path, icon_filename)
    if not os.path.exists(icon_filepath):
        raise IOError("Could not find icon file: " + icon_filepath)

    return icon_filepath


class Dialog(QDialog):
    """Dialog box to prompt the user for Server Bind credentials"""

    def __init__(self, fields_spec, title=None, fields_title=None,
                 callback=None):
        super(Dialog, self).__init__()
        if QtGui is None:
            raise RuntimeError("PySide is not installed.")
        self.create_authentication_box(fields_spec)
        self.callback = callback
        buttonBox = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok
                                           | QtGui.QDialogButtonBox.Cancel)
        buttonBox.accepted.connect(self.accept)
        buttonBox.rejected.connect(self.reject)

        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addWidget(self.authentication_group_box)
        self.message_area = QtGui.QLabel()
        self.message_area.setWordWrap(True)
        mainLayout.addWidget(self.message_area)
        mainLayout.addWidget(buttonBox)
        self.setLayout(mainLayout)
        if title is not None:
            self.setWindowTitle(title)
        icon = find_icon('nuxeo_drive_icon_64.png')
        if hasattr(QtGui, 'QIcon'):
            self.setWindowIcon(QtGui.QIcon(icon))
        else:
            log.warn('QtGui.QIcon is not available.')
        self.resize(600, -1)
        self.accepted = False

    def create_authentication_box(self, fields_spec):
        self.authentication_group_box = QtGui.QGroupBox()
        layout = QtGui.QGridLayout()
        self.fields = {}
        for i, spec in enumerate(fields_spec):
            label = QtGui.QLabel(spec['label'])
            line_edit = QtGui.QLineEdit()
            value = spec.get('value')
            if value is not None:
                line_edit.setText(value)
            if spec.get('is_password', False):
                line_edit.setEchoMode(QtGui.QLineEdit.Password)
            if spec.get('is_readonly', False):
                line_edit.setReadOnly(True)
            line_edit.textChanged.connect(self.clear_message)
            layout.addWidget(label, i + 1, 0)
            layout.addWidget(line_edit, i + 1, 1)
            self.fields[spec['id']] = line_edit

        self.authentication_group_box.setLayout(layout)

    def clear_message(self, *args, **kwargs):
        self.message_area.setText(None)<commit_msg>NXP-10343: the QIcon stuff does no cause anyproblem with frozen app: enable it by default<commit_after>"""GUI prompt to bind a new server"""
import os
from nxdrive.client import Unauthorized
from nxdrive.logging_config import get_logger

log = get_logger(__name__)

# Keep QT an optional dependency for now
QtGui, QDialog = None, object
try:
    from PySide import QtGui
    QDialog = QtGui.QDialog
except ImportError:
    log.warn("QT / PySide is not installed: GUI is disabled")
    pass


def find_icon(icon_filename):
    import nxdrive
    nxdrive_path = os.path.dirname(nxdrive.__file__)
    icons_path = os.path.join(nxdrive_path, 'data', 'icons')

    frozen_suffix = os.path.join('library.zip', 'nxdrive')
    if nxdrive_path.endswith(frozen_suffix):
        # Frozen distribution of nxdrive, data is out of the zip
        icons_path = nxdrive_path.replace(frozen_suffix, 'icons')

    if not os.path.exists(icons_path):
        raise IOError("Could not find the icons folder at: " + icons_path)

    icon_filepath = os.path.join(icons_path, icon_filename)
    if not os.path.exists(icon_filepath):
        raise IOError("Could not find icon file: " + icon_filepath)

    return icon_filepath


class Dialog(QDialog):
    """Dialog box to prompt the user for Server Bind credentials"""

    def __init__(self, fields_spec, title=None, fields_title=None,
                 callback=None):
        super(Dialog, self).__init__()
        if QtGui is None:
            raise RuntimeError("PySide is not installed.")
        self.create_authentication_box(fields_spec)
        self.callback = callback
        buttonBox = QtGui.QDialogButtonBox(QtGui.QDialogButtonBox.Ok
                                           | QtGui.QDialogButtonBox.Cancel)
        buttonBox.accepted.connect(self.accept)
        buttonBox.rejected.connect(self.reject)

        mainLayout = QtGui.QVBoxLayout()
        mainLayout.addWidget(self.authentication_group_box)
        self.message_area = QtGui.QLabel()
        self.message_area.setWordWrap(True)
        mainLayout.addWidget(self.message_area)
        mainLayout.addWidget(buttonBox)
        self.setLayout(mainLayout)
        if title is not None:
            self.setWindowTitle(title)
        icon = find_icon('nuxeo_drive_icon_64.png')
        self.setWindowIcon(QtGui.QIcon(icon))
        self.resize(600, -1)
        self.accepted = False

    def create_authentication_box(self, fields_spec):
        self.authentication_group_box = QtGui.QGroupBox()
        layout = QtGui.QGridLayout()
        self.fields = {}
        for i, spec in enumerate(fields_spec):
            label = QtGui.QLabel(spec['label'])
            line_edit = QtGui.QLineEdit()
            value = spec.get('value')
            if value is not None:
                line_edit.setText(value)
            if spec.get('is_password', False):
                line_edit.setEchoMode(QtGui.QLineEdit.Password)
            if spec.get('is_readonly', False):
                line_edit.setReadOnly(True)
            line_edit.textChanged.connect(self.clear_message)
            layout.addWidget(label, i + 1, 0)
            layout.addWidget(line_edit, i + 1, 1)
            self.fields[spec['id']] = line_edit

        self.authentication_group_box.setLayout(layout)

    def clear_message(self, *args, **kwargs):
        self.message_area.setText(None)
{'fraction_non_alphanumeric': 0.09490672194255256, 'fraction_numerical': 0.0037015102161681965, 'mean_word_length': 2.681198910081744, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7652560', 'n_tokens_mistral': 2073, 'n_tokens_neox': 2000, 'n_words': 445}