<commit_before>'''
(C) 2014-2016 <NAME> and contributors
Licensed under BSD license

http://github.com/r0x0r/pywebview/
'''

import os
import json
import logging
from uuid import uuid1
from copy import deepcopy
from threading import Semaphore, Event

from webview.localization import localization
from webview import _parse_api_js, _js_bridge_call, _convert_string, _escape_string
from webview import OPEN_DIALOG, FOLDER_DIALOG, SAVE_DIALOG


logger = logging.getLogger(__name__)


# Try importing Qt5 modules
try:
    from PyQt5 import QtCore

    # Check to see if we're running Qt > 5.5
    from PyQt5.QtCore import QT_VERSION_STR
    _qt_version = [int(n) for n in QT_VERSION_STR.split('.')]

    if _qt_version >= [5, 5]:
        from PyQt5.QtWebEngineWidgets import QWebEngineView as QWebView
        from PyQt5.QtWebChannel import QWebChannel
    else:
        from PyQt5.QtWebKitWidgets import QWebView

    from PyQt5.QtWidgets import QWidget, QMainWindow, QVBoxLayout, QApplication, QFileDialog, QMessageBox
    from PyQt5.QtGui import QColor

    logger.debug('Using Qt5')
except ImportError as e:
    logger.debug('PyQt5 or one of dependencies is not found', exc_info=True)
    _import_error = True
else:
    _import_error = False

if _import_error:
    # Try importing Qt4 modules
    try:
        from PyQt4 import QtCore
        from PyQt4.QtWebKit import QWebView, QWebFrame
        from PyQt4.QtGui import QWidget, QMainWindow, QVBoxLayout, QApplication, QDialog, QFileDialog, QMessageBox, QColor

        _qt_version = [4, 0]
        logger.debug('Using Qt4')
    except ImportError as e:
        _import_error = True
    else:
        _import_error = False

if _import_error:
    raise Exception('This module requires PyQt4 or PyQt5 to work under Linux.')


class BrowserView(QMainWindow):
    instances = {}

    create_window_trigger = QtCore.pyqtSignal(object)
    set_title_trigger = QtCore.pyqtSignal(str)
    load_url_trigger = QtCore.pyqtSignal(str)
    html_trigger = QtCore.pyqtSignal(str)
    dialog_trigger = QtCore.pyqtSignal(int, str, bool, str, str)
    destroy_trigger = QtCore.pyqtSignal()
    fullscreen_trigger = QtCore.pyqtSignal()
    current_url_trigger = QtCore.pyqtSignal()
    evaluate_js_trigger = QtCore.pyqtSignal(str, str)

    class JSBridge(QtCore.QObject):
        api = None
        parent_uid = None

        try:
            qtype = QtCore.QJsonValue  # QT5
        except AttributeError:
            qtype = str  # QT4

        def __init__(self):
            super(BrowserView.JSBridge, self).__init__()

        @QtCore.pyqtSlot(str, qtype, result=str)
        def call(self, func_name, param):
            func_name = BrowserView._convert_string(func_name)
            param = BrowserView._convert_string(param)

            return _js_bridge_call(self.parent_uid, self.api, func_name, param)

    def __init__(self, uid, title, url, width, height, resizable, fullscreen,
                 min_size, confirm_quit, background_color, debug, js_api, webview_ready):
        super(BrowserView, self).__init__()
        BrowserView.instances[uid] = self
        self.uid = uid

        self.js_bridge = BrowserView.JSBridge()
        self.js_bridge.api = js_api
        self.js_bridge.parent_uid = self.uid

        self.is_fullscreen = False
        self.confirm_quit = confirm_quit

        self._file_name_semaphore = Semaphore(0)
        self._current_url_semaphore = Semaphore(0)

        self.load_event = Event()

        self._js_results = {}
        self._current_url = None
        self._file_name = None

        self.resize(width, height)
        self.title = title
        self.setWindowTitle(title)

        # Set window background color
        self.background_color = QColor()
        self.background_color.setNamedColor(background_color)
        palette = self.palette()
        palette.setColor(self.backgroundRole(), self.background_color)
        self.setPalette(palette)

        if not resizable:
            self.setFixedSize(width, height)

        self.setMinimumSize(min_size[0], min_size[1])

        self.view = QWebView(self)

        if url is not None:
            self.view.setUrl(QtCore.QUrl(url))<commit_msg>[QT] Implement opening target=_blank links in a web browser for older QT versons<commit_after>'''
(C) 2014-2016 <NAME> and contributors
Licensed under BSD license

http://github.com/r0x0r/pywebview/
'''

import os
import json
import logging
import webbrowser
from uuid import uuid1
from copy import deepcopy
from threading import Semaphore, Event

from webview.localization import localization
from webview import _parse_api_js, _js_bridge_call, _convert_string, _escape_string
from webview import OPEN_DIALOG, FOLDER_DIALOG, SAVE_DIALOG


logger = logging.getLogger(__name__)


# Try importing Qt5 modules
try:
    from PyQt5 import QtCore

    # Check to see if we're running Qt > 5.5
    from PyQt5.QtCore import QT_VERSION_STR
    _qt_version = [int(n) for n in QT_VERSION_STR.split('.')]

    if _qt_version >= [5, 5]:
        from PyQt5.QtWebEngineWidgets import QWebEngineView as QWebView, QWebEnginePage as QWebPage
        from PyQt5.QtWebChannel import QWebChannel
    else:
        from PyQt5 import QtWebKitWidgets
        from PyQt5.QtWebKitWidgets import QWebView, QWebPage

    from PyQt5.QtWidgets import QWidget, QMainWindow, QVBoxLayout, QApplication, QFileDialog, QMessageBox
    from PyQt5.QtGui import QColor

    logger.debug('Using Qt5')
except ImportError as e:
    logger.debug('PyQt5 or one of dependencies is not found', exc_info=True)
    _import_error = True
else:
    _import_error = False

if _import_error:
    # Try importing Qt4 modules
    try:
        from PyQt4 import QtCore
        from PyQt4.QtWebKit import QWebView, QWebFrame
        from PyQt4.QtGui import QWidget, QMainWindow, QVBoxLayout, QApplication, QDialog, QFileDialog, QMessageBox, QColor

        _qt_version = [4, 0]
        logger.debug('Using Qt4')
    except ImportError as e:
        _import_error = True
    else:
        _import_error = False

if _import_error:
    raise Exception('This module requires PyQt4 or PyQt5 to work under Linux.')


class BrowserView(QMainWindow):
    instances = {}

    create_window_trigger = QtCore.pyqtSignal(object)
    set_title_trigger = QtCore.pyqtSignal(str)
    load_url_trigger = QtCore.pyqtSignal(str)
    html_trigger = QtCore.pyqtSignal(str)
    dialog_trigger = QtCore.pyqtSignal(int, str, bool, str, str)
    destroy_trigger = QtCore.pyqtSignal()
    fullscreen_trigger = QtCore.pyqtSignal()
    current_url_trigger = QtCore.pyqtSignal()
    evaluate_js_trigger = QtCore.pyqtSignal(str, str)

    class JSBridge(QtCore.QObject):
        api = None
        parent_uid = None

        try:
            qtype = QtCore.QJsonValue  # QT5
        except AttributeError:
            qtype = str  # QT4

        def __init__(self):
            super(BrowserView.JSBridge, self).__init__()

        @QtCore.pyqtSlot(str, qtype, result=str)
        def call(self, func_name, param):
            func_name = BrowserView._convert_string(func_name)
            param = BrowserView._convert_string(param)

            return _js_bridge_call(self.parent_uid, self.api, func_name, param)

    class WebPage(QWebPage):
        def __init__(self, parent=None):
            super().__init__()
            self.parent = parent

        def acceptNavigationRequest(self, frame, request, type):
            if frame is None:
                webbrowser.open(request.url().toString())
                return False

            return True

        def createWindow(self, type):
            pass

    def __init__(self, uid, title, url, width, height, resizable, fullscreen,
                 min_size, confirm_quit, background_color, debug, js_api, webview_ready):
        super(BrowserView, self).__init__()
        BrowserView.instances[uid] = self
        self.uid = uid

        self.js_bridge = BrowserView.JSBridge()
        self.js_bridge.api = js_api
        self.js_bridge.parent_uid = self.uid

        self.is_fullscreen = False
        self.confirm_quit = confirm_quit

        self._file_name_semaphore = Semaphore(0)
        self._current_url_semaphore = Semaphore(0)

        self.load_event = Event()

        self._js_results = {}
        self._current_url = None
        self._file_name = None

        self.resize(width, height)
        self.title = title
        self.setWindowTitle(title)

        # Set window background color
        self.background_color = QColor()
        self.background_color.setNamedColor(background_color)
        palette = self.palette()
        palette.setColor(self.backgroundRole(), self.background_color)
        self.setPalette(palette)

        if not resizable:
            self.setFixedSize(width, height)

        self.setMinimumSize(min_size[0], min_size[1])

        self.view = QWebView(self)
        self.view.setPage(BrowserView.WebPage(self.view))

        if url is not None:
            self.view.setUrl(QtCore.QUrl(url))
{'fraction_non_alphanumeric': 0.07948490230905861, 'fraction_numerical': 0.008992007104795738, 'mean_word_length': 2.87984496124031, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '600357', 'n_tokens_mistral': 2877, 'n_tokens_neox': 2758, 'n_words': 682}