def _goToAddress(self):
        q = QUrl(self._itAddress.text())
        if q.scheme() == "":
            q.setScheme("http")

        self._currentWeb.load(q)
        self._currentWeb.show()
def encode_uri(ds_uri, schema_name, project_name=None):
    u = QUrl()
    urlQuery = QUrlQuery()

    u.setScheme("postgresql")
    u.setHost(ds_uri.host())
    if ds_uri.port() != '':
        u.setPort(int(ds_uri.port()))
    if ds_uri.username() != '':
        u.setUserName(ds_uri.username())
    if ds_uri.password() != '':
        u.setPassword(ds_uri.password())

    if ds_uri.service() != '':
        urlQuery.addQueryItem("service", ds_uri.service())
    if ds_uri.authConfigId() != '':
        urlQuery.addQueryItem("authcfg", ds_uri.authConfigId())
    if ds_uri.sslMode() != QgsDataSourceUri.SslPrefer:
        urlQuery.addQueryItem("sslmode", QgsDataSourceUri.encodeSslMode(ds_uri.sslMode()))

    urlQuery.addQueryItem("dbname", ds_uri.database())

    urlQuery.addQueryItem("schema", schema_name)
    if project_name:
        urlQuery.addQueryItem("project", project_name)

    u.setQuery(urlQuery)
    return str(u.toEncoded(), 'utf-8')
Exemple #3
0
    def __init__(
        self, text, target_dir=None, title=None, base_dir=None
    ):
        import hashlib
        md = hashlib.md5()
        md.update(text.encode('utf-8'))
        self.hash_name = md.hexdigest()
        self.base_name = self.hash_name + '.ly'
        self.target_dir = target_dir or self._target_dir
        filename = os.path.join(self.target_dir, self.base_name)
        if os.path.exists(os.path.join(
            self.target_dir,
            self.hash_name + '.pdf')
        ):
            self._needs_compilation = False
        else:
            with open(filename, 'wb') as f:
                f.write(text.encode('utf-8'))
            self._needs_compilation = True
        url = QUrl(filename)
        url.setScheme('file')
        super(CachedPreviewJob, self).__init__(url, title=title)
        self.done.connect(self.remove_intermediate)

        if title:
            self.set_title(title)
        if base_dir:
            self.add_include_path(base_dir)
    def _on_history_trigger(self):
        try:
            self._widget.page()
        except RuntimeError:
            # Looks like this slot can be triggered on destroyed tabs:
            # https://crashes.qutebrowser.org/view/3abffbed (Qt 5.9.1)
            # wrapped C/C++ object of type WebEngineView has been deleted
            log.misc.debug("Ignoring history trigger for destroyed tab")
            return

        url = self.url()
        requested_url = self.url(requested=True)

        # Don't save the title if it's generated from the URL
        title = self.title()
        title_url = QUrl(url)
        title_url.setScheme('')
        if title == title_url.toDisplayString(QUrl.RemoveScheme).strip('/'):
            title = ""

        # Don't add history entry if the URL is invalid anyways
        if not url.isValid():
            log.misc.debug("Ignoring invalid URL being added to history")
            return

        self.add_history_item.emit(url, requested_url, title)
Exemple #5
0
    def __init__(
        self, text, title=None, base_dir=None):
        # TODO: ???
        #       I have the impression this "info" stuff
        #       is not used at all. And *if* it is used,
        #       shouldn't it be implemented in LilyPondJob???
        # Initialize default LilyPond version
        info = lilypondinfo.preferred()
        # Optionally infer a suitable LilyPond version from the content
        if QSettings().value("lilypond_settings/autoversion", True, bool):
            version = ly.docinfo.DocInfo(ly.document.Document(text, 'lilypond')).version()
            if version:
                info = lilypondinfo.suitable(version)
        # Create temporary (document.Document object and file)
        self.directory = util.tempdir()
        filename = os.path.join(self.directory, 'document.ly')
        with open(filename, 'wb') as f:
            f.write(text.encode('utf-8'))
        url = QUrl(filename)
        url.setScheme('file')
        super(VolatileTextJob, self).__init__(url, title=title)

        if title:
            self.set_title(title)
        if base_dir:
            self.add_include_path(base_dir)
Exemple #6
0
    def on_imagesTree_currentItemChanged(self, current, previous):
        """
        Private slot to show a preview of the selected image.
        
        @param current current image entry (QTreeWidgetItem)
        @param previous old current entry (QTreeWidgetItem)
        """
        if current is None:
            return

        imageUrl = QUrl(current.text(1))
        if not imageUrl.host():
            imageUrl.setHost(QUrl(self.siteAddressLabel.text()).host())
            imageUrl.setScheme(QUrl(self.siteAddressLabel.text()).scheme())

        import Helpviewer.HelpWindow
        cache = Helpviewer.HelpWindow.HelpWindow.networkAccessManager().cache()
        if cache:
            cacheData = cache.data(imageUrl)
        else:
            cacheData = None
        pixmap = QPixmap()
        invalidPixmap = False
        scene = QGraphicsScene(self.imagePreview)
        if not cacheData:
            invalidPixmap = True
        else:
            pixmap.loadFromData(cacheData.readAll())
            if pixmap.isNull():
                invalidPixmap = True
        if invalidPixmap:
            scene.addText(self.tr("Preview not available."))
        else:
            scene.addPixmap(pixmap)
        self.imagePreview.setScene(scene)
Exemple #7
0
    def url(self):
        """
        Public method to generate the URL for this subscription.
        
        @return AdBlock URL for the subscription (QUrl)
        """
        url = QUrl()
        url.setScheme("abp")
        url.setPath("subscribe")

        queryItems = []
        queryItems.append(("location", bytes(self.__location).decode()))
        queryItems.append(("title", self.__title))
        if self.__requiresLocation and self.__requiresTitle:
            queryItems.append(("requiresLocation", self.__requiresLocation))
            queryItems.append(("requiresTitle", self.__requiresTitle))
        if not self.__enabled:
            queryItems.append(("enabled", "false"))
        if self.__lastUpdate.isValid():
            queryItems.append(
                ("lastUpdate", self.__lastUpdate.toString(Qt.ISODate)))
        if qVersion() >= "5.0.0":
            from PyQt5.QtCore import QUrlQuery
            query = QUrlQuery()
            query.setQueryItems(queryItems)
            url.setQuery(query)
        else:
            url.setQueryItems(queryItems)
        return url
 def url(self):
     """
     Public method to generate the URL for this subscription.
     
     @return AdBlock URL for the subscription (QUrl)
     """
     url = QUrl()
     url.setScheme("abp")
     url.setPath("subscribe")
     
     queryItems = []
     queryItems.append(("location", bytes(self.__location).decode()))
     queryItems.append(("title", self.__title))
     if self.__requiresLocation and self.__requiresTitle:
         queryItems.append(("requiresLocation", self.__requiresLocation))
         queryItems.append(("requiresTitle", self.__requiresTitle))
     if not self.__enabled:
         queryItems.append(("enabled", "false"))
     if self.__lastUpdate.isValid():
         queryItems.append(("lastUpdate",
                            self.__lastUpdate.toString(Qt.ISODate)))
     if qVersion() >= "5.0.0":
         from PyQt5.QtCore import QUrlQuery
         query = QUrlQuery()
         query.setQueryItems(queryItems)
         url.setQuery(query)
     else:
         url.setQueryItems(queryItems)
     return url
    def _on_history_trigger(self):
        try:
            self._widget.page()
        except RuntimeError:
            # Looks like this slot can be triggered on destroyed tabs:
            # https://crashes.qutebrowser.org/view/3abffbed (Qt 5.9.1)
            # wrapped C/C++ object of type WebEngineView has been deleted
            log.misc.debug("Ignoring history trigger for destroyed tab")
            return

        url = self.url()
        requested_url = self.url(requested=True)

        # Don't save the title if it's generated from the URL
        title = self.title()
        title_url = QUrl(url)
        title_url.setScheme('')
        if title == title_url.toDisplayString(QUrl.RemoveScheme).strip('/'):
            title = ""

        # Don't add history entry if the URL is invalid anyways
        if not url.isValid():
            log.misc.debug("Ignoring invalid URL being added to history")
            return

        self.add_history_item.emit(url, requested_url, title)
Exemple #10
0
def encode_uri(ds_uri, schema_name, project_name=None):
    u = QUrl()
    urlQuery = QUrlQuery()

    u.setScheme("postgresql")
    u.setHost(ds_uri.host())
    if ds_uri.port() != '':
        u.setPort(int(ds_uri.port()))
    if ds_uri.username() != '':
        u.setUserName(ds_uri.username())
    if ds_uri.password() != '':
        u.setPassword(ds_uri.password())

    if ds_uri.service() != '':
        urlQuery.addQueryItem("service", ds_uri.service())
    if ds_uri.authConfigId() != '':
        urlQuery.addQueryItem("authcfg", ds_uri.authConfigId())
    if ds_uri.sslMode() != QgsDataSourceUri.SslPrefer:
        urlQuery.addQueryItem("sslmode", QgsDataSourceUri.encodeSslMode(ds_uri.sslMode()))

    urlQuery.addQueryItem("dbname", ds_uri.database())

    urlQuery.addQueryItem("schema", schema_name)
    if project_name:
        urlQuery.addQueryItem("project", project_name)

    u.setQuery(urlQuery)
    return str(u.toEncoded(), 'utf-8')
Exemple #11
0
 def navigate_to_url(self):
     # print(self.urlbar.text())
     q = QUrl(self.urlbar.text())
     # print(q.scheme())
     if q.scheme() == '':
         q.setScheme('http')
     self.browser.setUrl(q)
    def open_help(self):

        url = QUrl()
        url.setScheme(DefaultConfig.Help.url_scheme)
        url.setHost(DefaultConfig.Help.host)
        url.setPath(DefaultConfig.Help.url_path_pattern) # % str(Version.babel))
        QDesktopServices.openUrl(url)
Exemple #13
0
 def on_imagesTree_currentItemChanged(self, current, previous):
     """
     Private slot to show a preview of the selected image.
     
     @param current current image entry (QTreeWidgetItem)
     @param previous old current entry (QTreeWidgetItem)
     """
     if current is None:
         return
     
     imageUrl = QUrl(current.text(1))
     if not imageUrl.host():
         imageUrl.setHost(QUrl(self.siteAddressLabel.text()).host())
         imageUrl.setScheme(QUrl(self.siteAddressLabel.text()).scheme())
     
     import Helpviewer.HelpWindow
     cache = Helpviewer.HelpWindow.HelpWindow.networkAccessManager().cache()
     if cache:
         cacheData = cache.data(imageUrl)
     else:
         cacheData = None
     pixmap = QPixmap()
     invalidPixmap = False
     scene = QGraphicsScene(self.imagePreview)
     if not cacheData:
         invalidPixmap = True
     else:
         pixmap.loadFromData(cacheData.readAll())
         if pixmap.isNull():
             invalidPixmap = True
     if invalidPixmap:
         scene.addText(self.tr("Preview not available."))
     else:
         scene.addPixmap(pixmap)
     self.imagePreview.setScene(scene)
Exemple #14
0
    def navigate_to_url(self, url): #todo: fix naming (url is an id right now) ... after the demo
        final_url = "{}/cards/checkin/{}/".format(self.baseUrl, url)
        print("navigate_to_url: [{}]".format(final_url))
        q = QUrl(final_url)
        if q.scheme() == "":
            q.setScheme("http")

        self.browser.setUrl(q)
Exemple #15
0
    def open_help(self):

        url = QUrl()
        url.setScheme(DefaultConfig.Help.url_scheme)
        url.setHost(DefaultConfig.Help.host)
        url.setPath(
            DefaultConfig.Help.url_path_pattern)  # % str(Version.babel))
        QDesktopServices.openUrl(url)
def data_for_url(url):
    """Get the data to show for the given URL.

    Args:
        url: The QUrl to show.

    Return:
        A (mimetype, data) tuple.
    """
    norm_url = url.adjusted(QUrl.NormalizePathSegments
                            | QUrl.StripTrailingSlash)
    if norm_url != url:
        raise Redirect(norm_url)

    path = url.path()
    host = url.host()
    query = urlutils.query_string(url)
    # A url like "qute:foo" is split as "scheme:path", not "scheme:host".
    log.misc.debug("url: {}, path: {}, host {}".format(url.toDisplayString(),
                                                       path, host))
    if not path or not host:
        new_url = QUrl()
        new_url.setScheme('qute')
        # When path is absent, e.g. qute://help (with no trailing slash)
        if host:
            new_url.setHost(host)
        # When host is absent, e.g. qute:help
        else:
            new_url.setHost(path)

        new_url.setPath('/')
        if query:
            new_url.setQuery(query)
        if new_url.host():  # path was a valid host
            raise Redirect(new_url)

    try:
        handler = _HANDLERS[host]
    except KeyError:
        raise NoHandlerFound(url)

    try:
        mimetype, data = handler(url)
    except OSError as e:
        # FIXME:qtwebengine how to handle this?
        raise QuteSchemeOSError(e)
    except QuteSchemeError as e:
        raise

    assert mimetype is not None, url
    if mimetype == 'text/html' and isinstance(data, str):
        # We let handlers return HTML as text
        data = data.encode('utf-8', errors='xmlcharrefreplace')

    return mimetype, data
Exemple #17
0
   def open_test_suite(self):
      """ Open a test suite file and loads the test cases from the suite into the tool.
          There is a chained process flow when you open a test suite:
             open_test_suite() calls select_target() 
             select_target then calls load_test_cases() 
          Once a test suite is opened, the user may later choose a new target for the same 
          test suite as teh select_target() method call load_test_cases().      """ 
      
      self.test_case_data_list = []
      url = QUrl()                                    # This File Dialog should start in  
      url.setScheme("file")                           # the test suites folder by default. 
      url.setPath(   "%s/../testsuites" %MY_PATH   )  # TODO: os.path.join()
      options = QFileDialog.Options()
      options |= QFileDialog.DontUseNativeDialog
      file_dialog = QFileDialog()
      file_dialog.setDirectoryUrl(url)
      self.testsuite_file, _ = file_dialog.getOpenFileName(self,"QFileDialog.getOpenFileName()", "","All Files (*)", options=options)
      if self.testsuite_file:
         self.test_suite_label.setText("Test Suite: %s" %self.testsuite_file.split('/')[LAST])
         message = "Loaded Test Suite:  %s" %self.testsuite_file 
         logger.info(message)
         # Crate a list of test cases from the test suite file. However, these 
         # test cases are only file names with not path. We'll have to add the path 
         # based on the target selected.   
         try:
            f = open(self.testsuite_file, 'r')
            lines = f.readlines()
            f.close()
            self.suite_text_area.clear()
            for line in lines:
               line = line.strip()
               self.suite_text_area.append(line)
         except:
            message = "Unalbe to read test cases from test suite %s" %self.testsuite_file
            logger.error(message)
            self.suite_text_area.setText(message) 
            lines = []

         self.test_case_file_list = [] # a list of test case file names with no paths
         for line in lines:
            line = line.strip()
            if len(line) < 1:
               pass
            elif line.startswith('#'):
               pass
            else:
               self.test_case_file_list.append(line)     

         self.test_case_count = len(self.test_case_file_list)
         message = "Found %d test cases in %s" %(self.test_case_count, self.testsuite_file.split('/')[LAST])
         logger.info(message)
         self.status_bar.showMessage(message)  

         # open the select a test target for this suite
         self.select_target()  
Exemple #18
0
def data_for_url(url):
    """Get the data to show for the given URL.

    Args:
        url: The QUrl to show.

    Return:
        A (mimetype, data) tuple.
    """
    norm_url = url.adjusted(QUrl.NormalizePathSegments |
                            QUrl.StripTrailingSlash)
    if norm_url != url:
        raise Redirect(norm_url)

    path = url.path()
    host = url.host()
    query = urlutils.query_string(url)
    # A url like "qute:foo" is split as "scheme:path", not "scheme:host".
    log.misc.debug("url: {}, path: {}, host {}".format(
        url.toDisplayString(), path, host))
    if not path or not host:
        new_url = QUrl()
        new_url.setScheme('qute')
        # When path is absent, e.g. qute://help (with no trailing slash)
        if host:
            new_url.setHost(host)
        # When host is absent, e.g. qute:help
        else:
            new_url.setHost(path)

        new_url.setPath('/')
        if query:
            new_url.setQuery(query)
        if new_url.host():  # path was a valid host
            raise Redirect(new_url)

    try:
        handler = _HANDLERS[host]
    except KeyError:
        raise NoHandlerFound(url)

    try:
        mimetype, data = handler(url)
    except OSError as e:
        # FIXME:qtwebengine how to handle this?
        raise QuteSchemeOSError(e)
    except QuteSchemeError as e:
        raise

    assert mimetype is not None, url
    if mimetype == 'text/html' and isinstance(data, str):
        # We let handlers return HTML as text
        data = data.encode('utf-8', errors='xmlcharrefreplace')

    return mimetype, data
Exemple #19
0
    def _on_history_trigger(self):
        url = self.url()
        requested_url = self.url(requested=True)

        # Don't save the title if it's generated from the URL
        title = self.title()
        title_url = QUrl(url)
        title_url.setScheme('')
        if title == title_url.toDisplayString(QUrl.RemoveScheme).strip('/'):
            title = ""

        self.add_history_item.emit(url, requested_url, title)
Exemple #20
0
    def _on_history_trigger(self):
        url = self.url()
        requested_url = self.url(requested=True)

        # Don't save the title if it's generated from the URL
        title = self.title()
        title_url = QUrl(url)
        title_url.setScheme('')
        if title == title_url.toDisplayString(QUrl.RemoveScheme).strip('/'):
            title = ""

        self.add_history_item.emit(url, requested_url, title)
Exemple #21
0
def data_for_url(url: QUrl):
    """Get the data to show for the given URL.

    Args:
        url: The QUrl to show.

    Return:
        A (mimetype, data) tuple.
    """
    norm_url = url.adjusted(QUrl.NormalizePathSegments
                            | QUrl.StripTrailingSlash)
    if norm_url != url:
        raise Redirect(norm_url)

    path = url.path()
    host = url.host()
    query = urlutils.query_string(url)
    # A url like "luminos:foo" is split as "scheme:path", not "scheme:host".

    if not path or not host:
        new_url = QUrl()
        new_url.setScheme("luminos")
        # When path is absent, e.g. luminos://help (with no trailing slash)
        if host:
            new_url.setHost(host)
        # When host is absent, e.g. luminos:help
        else:
            new_url.setHost(path)

        new_url.setPath("/")
        if query:
            new_url.setQuery(query)
        if new_url.host():  # path was a valid host
            raise Redirect(new_url)

    try:
        handler = _HANDLERS[host]
    except KeyError:
        raise NotFoundError("No handler found for {}".format(
            url.toDisplayString()))

    try:
        mimetype, data = handler(url)
    except OSError as e:
        raise SchemeOSError(e)

    assert mimetype is not None, url
    if mimetype == "text/html" and isinstance(data, str):
        # We let handlers return HTML as text
        data = data.encode("utf-8", errors="xmlcharrefreplace")

    return mimetype, data
 def __saveImage(self):
     """
     Private slot to save the selected image to disk.
     """
     act = self.sender()
     index = act.data()
     itm = self.imagesTree.topLevelItem(index)
     if itm is None:
         return
     
     imageUrl = QUrl(itm.text(1))
     if not imageUrl.host():
         imageUrl.setHost(QUrl(self.siteAddressLabel.text()).host())
         imageUrl.setScheme(QUrl(self.siteAddressLabel.text()).scheme())
     
     import Helpviewer.HelpWindow
     cache = Helpviewer.HelpWindow.HelpWindow.networkAccessManager().cache()
     if cache:
         cacheData = cache.data(imageUrl)
     else:
         cacheData = None
     if not cacheData:
         E5MessageBox.critical(
             self,
             self.tr("Save Image"),
             self.tr("""This image is not available."""))
         return
     
     downloadDirectory = Helpviewer.HelpWindow.HelpWindow\
         .downloadManager().downloadDirectory()
     fn = os.path.join(downloadDirectory, os.path.basename(itm.text(1)))
     filename = E5FileDialog.getSaveFileName(
         self,
         self.tr("Save Image"),
         fn,
         self.tr("All Files (*)"),
         E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
     
     if not filename:
         return
     
     f = QFile(filename)
     if not f.open(QFile.WriteOnly):
         E5MessageBox.critical(
             self,
             self.tr("Save Image"),
             self.tr(
                 """<p>Cannot write to file <b>{0}</b>.</p>""")
             .format(filename))
         return
     f.write(cacheData.readAll())
     f.close()
Exemple #23
0
 def __saveImage(self):
     """
     Private slot to save the selected image to disk.
     """
     act = self.sender()
     index = act.data()
     itm = self.imagesTree.topLevelItem(index)
     if itm is None:
         return
     
     imageUrl = QUrl(itm.text(1))
     if not imageUrl.host():
         imageUrl.setHost(QUrl(self.siteAddressLabel.text()).host())
         imageUrl.setScheme(QUrl(self.siteAddressLabel.text()).scheme())
     
     import Helpviewer.HelpWindow
     cache = Helpviewer.HelpWindow.HelpWindow.networkAccessManager().cache()
     if cache:
         cacheData = cache.data(imageUrl)
     else:
         cacheData = None
     if not cacheData:
         E5MessageBox.critical(
             self,
             self.tr("Save Image"),
             self.tr("""This image is not available."""))
         return
     
     downloadDirectory = Helpviewer.HelpWindow.HelpWindow\
         .downloadManager().downloadDirectory()
     fn = os.path.join(downloadDirectory, os.path.basename(itm.text(1)))
     filename = E5FileDialog.getSaveFileName(
         self,
         self.tr("Save Image"),
         fn,
         self.tr("All Files (*)"),
         E5FileDialog.Options(E5FileDialog.DontConfirmOverwrite))
     
     if not filename:
         return
     
     f = QFile(filename)
     if not f.open(QFile.WriteOnly):
         E5MessageBox.critical(
             self,
             self.tr("Save Image"),
             self.tr(
                 """<p>Cannot write to file <b>{0}</b>.</p>""")
             .format(filename))
         return
     f.write(cacheData.readAll())
     f.close()
Exemple #24
0
def generic_blocklists(directory):
    """Return a generic list of files to be used in hosts-block-lists option.

    This list contains :
    - a remote zip file with 1 hosts file and 2 useless files
    - a remote zip file with only useless files
        (Should raise a FileNotFoundError)
    - a remote zip file with only one valid hosts file
    - a local text file with valid hosts
    - a remote text file without valid hosts format.
    """
    # remote zip file with 1 hosts file and 2 useless files
    file1 = create_blocklist(directory, blocked_hosts=CLEAN_HOSTS,
                             name='README', line_format='not_correct')
    file2 = create_blocklist(directory, blocked_hosts=BLOCKLIST_HOSTS[:3],
                             name='hosts', line_format='etc_hosts')
    file3 = create_blocklist(directory, blocked_hosts=CLEAN_HOSTS,
                             name='false_positive', line_format='one_per_line')
    files_to_zip = [file1, file2, file3]
    blocklist1 = QUrl(create_zipfile(directory, files_to_zip, 'block1'))

    # remote zip file without file named hosts
    # (Should raise a FileNotFoundError)
    file1 = create_blocklist(directory, blocked_hosts=CLEAN_HOSTS,
                             name='md5sum', line_format='etc_hosts')
    file2 = create_blocklist(directory, blocked_hosts=CLEAN_HOSTS,
                             name='README', line_format='not_correct')
    file3 = create_blocklist(directory, blocked_hosts=CLEAN_HOSTS,
                             name='false_positive', line_format='one_per_line')
    files_to_zip = [file1, file2, file3]
    blocklist2 = QUrl(create_zipfile(directory, files_to_zip, 'block2'))

    # remote zip file with only one valid hosts file inside
    blocklist3 = create_blocklist(directory,
                                  blocked_hosts=[BLOCKLIST_HOSTS[3]],
                                  name='malwarelist', line_format='etc_hosts')
    blocklist3 = QUrl(create_zipfile(directory, [blocklist3], 'block3'))

    # local text file with valid hosts
    blocklist4 = QUrl(create_blocklist(directory,
                                       blocked_hosts=[BLOCKLIST_HOSTS[4]],
                                       name='mycustomblocklist',
                                       line_format='one_per_line'))
    blocklist4.setScheme('file')

    # remote text file without valid hosts format
    blocklist5 = QUrl(create_blocklist(directory, blocked_hosts=CLEAN_HOSTS,
                                       name='notcorrectlist',
                                       line_format='not_correct'))

    return [blocklist1, blocklist2, blocklist3, blocklist4, blocklist5]
Exemple #25
0
def generic_blocklists(directory):
    """Return a generic list of files to be used in hosts-block-lists option.

    This list contains :
    - a remote zip file with 1 hosts file and 2 useless files
    - a remote zip file with only useless files
        (Should raise a FileNotFoundError)
    - a remote zip file with only one valid hosts file
    - a local text file with valid hosts
    - a remote text file without valid hosts format.
    """
    # remote zip file with 1 hosts file and 2 useless files
    file1 = create_blocklist(directory, blocked_hosts=CLEAN_HOSTS,
                             name='README', line_format='not_correct')
    file2 = create_blocklist(directory, blocked_hosts=BLOCKLIST_HOSTS[:3],
                             name='hosts', line_format='etc_hosts')
    file3 = create_blocklist(directory, blocked_hosts=CLEAN_HOSTS,
                             name='false_positive', line_format='one_per_line')
    files_to_zip = [file1, file2, file3]
    blocklist1 = QUrl(create_zipfile(directory, files_to_zip, 'block1'))

    # remote zip file without file named hosts
    # (Should raise a FileNotFoundError)
    file1 = create_blocklist(directory, blocked_hosts=CLEAN_HOSTS,
                             name='md5sum', line_format='etc_hosts')
    file2 = create_blocklist(directory, blocked_hosts=CLEAN_HOSTS,
                             name='README', line_format='not_correct')
    file3 = create_blocklist(directory, blocked_hosts=CLEAN_HOSTS,
                             name='false_positive', line_format='one_per_line')
    files_to_zip = [file1, file2, file3]
    blocklist2 = QUrl(create_zipfile(directory, files_to_zip, 'block2'))

    # remote zip file with only one valid hosts file inside
    blocklist3 = create_blocklist(directory,
                                  blocked_hosts=[BLOCKLIST_HOSTS[3]],
                                  name='malwarelist', line_format='etc_hosts')
    blocklist3 = QUrl(create_zipfile(directory, [blocklist3], 'block3'))

    # local text file with valid hosts
    blocklist4 = QUrl(create_blocklist(directory,
                                       blocked_hosts=[BLOCKLIST_HOSTS[4]],
                                       name='mycustomblocklist',
                                       line_format='one_per_line'))
    blocklist4.setScheme('file')

    # remote text file without valid hosts format
    blocklist5 = QUrl(create_blocklist(directory, blocked_hosts=CLEAN_HOSTS,
                                       name='notcorrectlist',
                                       line_format='not_correct'))

    return [blocklist1, blocklist2, blocklist3, blocklist4, blocklist5]
    def searchForUrl(self):
        """
        Make a request to load a url.
        """
        url_text = self.list_of_urls[self.tab_bar.currentIndex()].text()

        # Append http to url
        url = QUrl(url_text)
        if url.scheme() == "":
            url.setScheme("http")

        # Request url
        if url.isValid():
            self.list_of_web_pages[self.tab_bar.currentIndex()].page().load(url)
        else:
            url.clear()
    def _on_history_trigger(self):
        url = self.url()
        requested_url = self.url(requested=True)

        # Don't save the title if it's generated from the URL
        title = self.title()
        title_url = QUrl(url)
        title_url.setScheme('')
        if title == title_url.toDisplayString(QUrl.RemoveScheme).strip('/'):
            title = ""

        # Don't add history entry if the URL is invalid anyways
        if not url.isValid():
            log.misc.debug("Ignoring invalid URL being added to history")
            return

        self.add_history_item.emit(url, requested_url, title)
    def _on_history_trigger(self):
        url = self.url()
        requested_url = self.url(requested=True)

        # Don't save the title if it's generated from the URL
        title = self.title()
        title_url = QUrl(url)
        title_url.setScheme('')
        if title == title_url.toDisplayString(QUrl.RemoveScheme).strip('/'):
            title = ""

        # Don't add history entry if the URL is invalid anyways
        if not url.isValid():
            log.misc.debug("Ignoring invalid URL being added to history")
            return

        self.add_history_item.emit(url, requested_url, title)
Exemple #29
0
 def __init__(self, text, title=""):
     # Initialize default LilyPond version
     info = lilypondinfo.preferred()
     # Optionally infer a suitable LilyPond version from the content
     if QSettings().value("lilypond_settings/autoversion", True, bool):
         version = ly.docinfo.DocInfo(ly.document.Document(text, 'lilypond')).version()
         if version:
             info = lilypondinfo.suitable(version)
     # Create temporary (document.Document object and file)
     directory = util.tempdir()
     filename = os.path.join(directory, 'document.ly')
     with open(filename, 'wb') as f:
         f.write(text.encode('utf-8'))
     url = QUrl(filename)
     url.setScheme('file')
     doc = document.Document(url)
     super(VolatileTextJob, self).__init__(doc, title=title)
Exemple #30
0
 def __init__(self, text, title=""):
     # Initialize default LilyPond version
     info = lilypondinfo.preferred()
     # Optionally infer a suitable LilyPond version from the content
     if QSettings().value("lilypond_settings/autoversion", True, bool):
         version = ly.docinfo.DocInfo(ly.document.Document(
             text, 'lilypond')).version()
         if version:
             info = lilypondinfo.suitable(version)
     # Create temporary (document.Document object and file)
     directory = util.tempdir()
     filename = os.path.join(directory, 'document.ly')
     with open(filename, 'wb') as f:
         f.write(text.encode('utf-8'))
     url = QUrl(filename)
     url.setScheme('file')
     doc = document.Document(url)
     super(VolatileTextJob, self).__init__(doc, title=title)
Exemple #31
0
    def load_url(self, url, tab=None):
        '''Load the given URL in the specified tab, or current tab if tab=None.
           url is a str, not a QUrl.
           PDF URLs will be loaded in a new tab, because there doesn't
           seem to be a way of replacing the BrowserView with a BrowserPDFView.
        '''

        # If there are newlines, remove newlines plus all adjacent whitespace.
        if '\n' in url:
            lines = url.split('\n')
            url = ''.join([ l.strip() for l in lines ])

        # Note that tab=0 is a valid argument here.
        # When testing whether tab is set, be sure to test for None.

        if is_pdf(url):
            self.new_tab(url)
            return

        qurl = QUrl(url)

        if not qurl.scheme():
            if os.path.exists(url):
                qurl.setScheme('file')
                if not os.path.isabs(url):
                    # Is it better to use posixpath.join or os.path.join?
                    # Both work on Linux.
                    qurl.setPath(os.path.normpath(os.path.join(os.getcwd(),
                                                               url)))
            else:
                qurl.setScheme('http')

        if len(self.browserviews) == 0:
            self.new_tab()
            tab = 0
        elif tab == None:
            tab = self.active_tab

        self.set_tab_text(url[:self.init_tab_name_len],
                          self.browserviews[tab])
        if tab == self.active_tab:
            self.urlbar.setText(url)

        self.browserviews[tab].load(qurl)
Exemple #32
0
    def load_url(self, url, tab=None):
        """Load the given URL in the specified tab, or current tab if tab=None.
           url is a str, not a QUrl.
           PDF URLs will be loaded in a new tab, because there doesn't
           seem to be a way of replacing the BrowserView with a BrowserPDFView.
        """

        # If there are newlines, remove newlines plus all adjacent whitespace.
        if '\n' in url:
            lines = url.split('\n')
            url = ''.join([ l.strip() for l in lines ])

        # Note that tab=0 is a valid argument here.
        # When testing whether tab is set, be sure to test for None.

        if is_pdf(url):
            self.new_tab(url)
            return

        qurl = QUrl(url)

        if not qurl.scheme():
            if os.path.exists(url):
                qurl.setScheme('file')
                if not os.path.isabs(url):
                    # Is it better to use posixpath.join or os.path.join?
                    # Both work on Linux.
                    qurl.setPath(os.path.normpath(os.path.join(os.getcwd(),
                                                               url)))
            else:
                qurl.setScheme('http')

        if len(self.browserviews) == 0:
            self.new_tab()
            tab = 0
        elif tab is None:
            tab = self.active_tab

        self.set_tab_text(url[:self.init_tab_name_len],
                          self.browserviews[tab])
        if tab == self.active_tab:
            self.urlbar.setText(url)

        self.browserviews[tab].load(qurl)
Exemple #33
0
def resource_url(path, qutescheme=False):
    """Load images from a relative path (to qutebrowser).

    Arguments:
        path: The relative path to the image
        qutescheme: If the logo needs to be served via a qute:// scheme.
                    This is the case when we want to show an error page from
                    there.
    """
    if qutescheme:
        url = QUrl()
        url.setScheme('qute')
        url.setHost('resource')
        url.setPath('/' + path)
        qtutils.ensure_valid(url)
        return url.toString(QUrl.FullyEncoded)
    else:
        full_path = utils.resource_filename(path)
        return QUrl.fromLocalFile(full_path).toString(QUrl.FullyEncoded)
Exemple #34
0
    def slotActionInvoked(self, msg):
        notifyId, action = msg.arguments()
        task = self._notifications.get(notifyId, None)
        if not task:
            # other applications' notifications
            return
        name = task["name"] # filename
        path = task["path"] # location

        if action == "open":
            openPath = os.path.join(path, name)
        elif action == "openDir":
            openPath = path
        else:
            raise Exception("Unknown action from slotActionInvoked.")

        nativeOpenPath = self.app.mountsFaker.convertToNativePath(openPath)
        qUrl = QUrl(nativeOpenPath)
        qUrl.setScheme("file")
        QDesktopServices().openUrl(qUrl)
Exemple #35
0
    def iconForUrl(self, url):
        """
        Public method to get an icon for an URL.
        
        @param url URL to get icon for
        @type QUrl
        @return icon for the URL
        @rtype QIcon
        """
        scheme = url.scheme()
        if scheme in ["eric", "about"]:
            return UI.PixmapCache.getIcon("ericWeb.png")
        elif scheme == "qthelp":
            return UI.PixmapCache.getIcon("qthelp.png")
        elif scheme == "file":
            return UI.PixmapCache.getIcon("fileMisc.png")
        elif scheme == "abp":
            return UI.PixmapCache.getIcon("adBlockPlus.png")
        elif scheme == "ftp":
            return UI.PixmapCache.getIcon("network-server.png")

        self.load()

        urlStr = self.__urlToString(url)
        for iconUrlStr in self.__iconsDB:
            if iconUrlStr.startswith(urlStr):
                return self.__iconsDB[iconUrlStr]

        # try replacing http scheme with https scheme
        url = QUrl(url)
        if url.scheme() == "http":
            url.setScheme("https")
        urlStr = self.__urlToString(url)
        for iconUrlStr in self.__iconsDB:
            if iconUrlStr.startswith(urlStr):
                return self.__iconsDB[iconUrlStr]

        if scheme == "https":
            return UI.PixmapCache.getIcon("securityHigh32.png")
        else:
            return UI.PixmapCache.getIcon("defaultIcon.png")
Exemple #36
0
def data_for_url(url):
    """Get the data to show for the given URL.

    Args:
        url: The QUrl to show.

    Return:
        A (mimetype, data) tuple.
    """
    path = url.path()
    host = url.host()
    # A url like "qute:foo" is split as "scheme:path", not "scheme:host".
    log.misc.debug("url: {}, path: {}, host {}".format(
        url.toDisplayString(), path, host))
    if path and not host:
        new_url = QUrl()
        new_url.setScheme('qute')
        new_url.setHost(path)
        new_url.setPath('/')
        if new_url.host():  # path was a valid host
            raise Redirect(new_url)

    try:
        handler = _HANDLERS[host]
    except KeyError:
        raise NoHandlerFound(url)

    try:
        mimetype, data = handler(url)
    except OSError as e:
        # FIXME:qtwebengine how to handle this?
        raise QuteSchemeOSError(e)
    except QuteSchemeError as e:
        raise

    assert mimetype is not None, url
    if mimetype == 'text/html' and isinstance(data, str):
        # We let handlers return HTML as text
        data = data.encode('utf-8', errors='xmlcharrefreplace')

    return mimetype, data
Exemple #37
0
    def onAnchorClicked(self, url: QUrl):
        from notes.ui.MainWindow import MainWindow
        import webbrowser

        host = url.host()
        scheme = url.scheme()

        if scheme == "http" or scheme == "https":
            webbrowser.open(url.toString())
        elif scheme == "notesmanager":
            url.setScheme("file")

            local_path = url.toLocalFile()
            query = QUrlQuery(url)
            uuid = query.queryItemValue("uuid")

            if local_path == self.notebook.attachment_base.filename:
                self.noteViewManager.openNote(self.noteViewManager.notebook.get_note_by_uuid(uuid))
            else:
                spawn = MainWindow(None, local_path, uuid)
                spawn.show()
Exemple #38
0
    def url(self, player_id):
        if self.state == GameState.CLOSED:
            return None

        url = QUrl()
        url.setHost("lobby.faforever.com")
        query = QUrlQuery()
        query.addQueryItem("map", self.mapname)
        query.addQueryItem("mod", self.featured_mod)

        if self.state == GameState.OPEN:
            url.setScheme("fafgame")
            url.setPath("/" + str(player_id))
            query.addQueryItem("uid", str(self.uid))
        else:
            url.setScheme("faflive")
            url.setPath("/" + str(self.uid) + "/" + str(player_id) +
                        ".SCFAreplay")

        url.setQuery(query)
        return url
Exemple #39
0
 def __convertFromOldBookmarks(self):
     """
     Private method to convert the old bookmarks into the new ones.
     """
     bmNames = Preferences.Prefs.settings.value('Bookmarks/Names')
     bmFiles = Preferences.Prefs.settings.value('Bookmarks/Files')
     
     if bmNames is not None and bmFiles is not None:
         if len(bmNames) == len(bmFiles):
             convertedRootNode = BookmarkNode(BookmarkNode.Folder)
             convertedRootNode.title = self.tr("Converted {0}")\
                 .format(QDate.currentDate().toString(
                     Qt.SystemLocaleShortDate))
             for i in range(len(bmNames)):
                 node = BookmarkNode(BookmarkNode.Bookmark,
                                     convertedRootNode)
                 node.title = bmNames[i]
                 url = QUrl(bmFiles[i])
                 if not url.scheme():
                     url.setScheme("file")
                 node.url = url.toString()
             self.addBookmark(self.menu(), convertedRootNode)
             
             Preferences.Prefs.settings.remove('Bookmarks')
    def __convertFromOldBookmarks(self):
        """
        Private method to convert the old bookmarks into the new ones.
        """
        bmNames = Preferences.Prefs.settings.value('Bookmarks/Names')
        bmFiles = Preferences.Prefs.settings.value('Bookmarks/Files')

        if bmNames is not None and bmFiles is not None:
            if len(bmNames) == len(bmFiles):
                convertedRootNode = BookmarkNode(BookmarkNode.Folder)
                convertedRootNode.title = self.tr("Converted {0}")\
                    .format(QDate.currentDate().toString(
                        Qt.SystemLocaleShortDate))
                for i in range(len(bmNames)):
                    node = BookmarkNode(BookmarkNode.Bookmark,
                                        convertedRootNode)
                    node.title = bmNames[i]
                    url = QUrl(bmFiles[i])
                    if not url.scheme():
                        url.setScheme("file")
                    node.url = url.toString()
                self.addBookmark(self.menu(), convertedRootNode)

                Preferences.Prefs.settings.remove('Bookmarks')
Exemple #41
0
 def __procFinished(self, exitCode, exitStatus):
     """
     Private slot connected to the finished signal.
     
     @param exitCode exit code of the process (integer)
     @param exitStatus exit status of the process (QProcess.ExitStatus)
     """
     self.inputGroup.setEnabled(False)
     self.inputGroup.hide()
     
     self.contents.clear()
     
     lvers = 1
     for s in self.buf:
         rev_match = False
         if self.rx_rev.exactMatch(s):
             ver = self.rx_rev.cap(1)
             author = self.rx_rev.cap(2)
             date = self.rx_rev.cap(3)
             # number of lines is ignored
             rev_match = True
         elif self.rx_rev2.exactMatch(s):
             ver = self.rx_rev2.cap(1)
             author = self.rx_rev2.cap(2)
             date = self.rx_rev2.cap(3)
             # number of lines is ignored
             rev_match = True
         
         if rev_match:
             dstr = '<b>{0} {1}</b>'.format(self.revString, ver)
             try:
                 lv = self.revisions[lvers]
                 lvers += 1
                 url = QUrl()
                 url.setScheme("file")
                 url.setPath(self.filename)
                 if qVersion() >= "5.0.0":
                     query = lv + '_' + ver
                     url.setQuery(query)
                 else:
                     query = QByteArray()
                     query.append(lv).append('_').append(ver)
                     url.setEncodedQuery(query)
                 dstr += ' [<a href="{0}" name="{1}">{2}</a>]'.format(
                     url.toString(), query,
                     self.tr('diff to {0}').format(lv),
                 )
             except IndexError:
                 pass
             dstr += '<br />\n'
             self.contents.insertHtml(dstr)
             
             dstr = self.tr('<i>author: {0}</i><br />\n').format(author)
             self.contents.insertHtml(dstr)
             
             dstr = self.tr('<i>date: {0}</i><br />\n').format(date)
             self.contents.insertHtml(dstr)
         
         elif self.rx_sep.exactMatch(s) or self.rx_sep2.exactMatch(s):
             self.contents.insertHtml('<hr />\n')
         
         elif self.rx_flags.exactMatch(s):
             dstr = self.flags[self.rx_flags.cap(1)]
             dstr += self.rx_flags.cap(2)
             dstr += '<br />\n'
             self.contents.insertHtml(dstr)
         
         elif self.rx_changed.exactMatch(s):
             dstr = '<br />{0}<br />\n'.format(s)
             self.contents.insertHtml(dstr)
         
         else:
             if s == "":
                 s = self.contents.insertHtml('<br />\n')
             else:
                 self.contents.insertHtml(Utilities.html_encode(s))
                 self.contents.insertHtml('<br />\n')
     
     tc = self.contents.textCursor()
     tc.movePosition(QTextCursor.Start)
     self.contents.setTextCursor(tc)
     self.contents.ensureCursorVisible()
Exemple #42
0
    def navigate_to_url(self):  # Does not receive the Url
        q = QUrl(self.urlbar.text())
        if q.scheme() == "":
            q.setScheme("http")

        self.browser.setUrl(q)
    def navigate_to_url(self):  # Does not receive the Url
        q = QUrl(self.urlbar.text())
        if q.scheme() == "":
            q.setScheme("http")

        self.tabs.currentWidget().setUrl(q)
Exemple #44
0
    def start(self, fn, noEntries=0):
        """
        Public slot to start the svn log command.
        
        @param fn filename to show the log for (string)
        @param noEntries number of entries to show (integer)
        """
        self.errorGroup.hide()

        fetchLimit = 10

        QApplication.setOverrideCursor(QCursor(Qt.WaitCursor))
        QApplication.processEvents()

        self.filename = fn
        dname, fname = self.vcs.splitPath(fn)

        opts = self.vcs.options["global"] + self.vcs.options["log"]
        verbose = "--verbose" in opts

        self.activateWindow()
        self.raise_()

        locker = QMutexLocker(self.vcs.vcsExecutionMutex)
        cwd = os.getcwd()
        os.chdir(dname)
        try:
            nextRev = 0
            fetched = 0
            logs = []
            limit = noEntries or 9999999
            while fetched < limit:
                flimit = min(fetchLimit, limit - fetched)
                if fetched == 0:
                    revstart = pysvn.Revision(pysvn.opt_revision_kind.head)
                else:
                    revstart = pysvn.Revision(pysvn.opt_revision_kind.number, nextRev)
                allLogs = self.client.log(
                    fname,
                    revision_start=revstart,
                    discover_changed_paths=verbose,
                    limit=flimit + 1,
                    strict_node_history=False,
                )
                if len(allLogs) <= flimit or self._clientCancelCallback():
                    logs.extend(allLogs)
                    break
                else:
                    logs.extend(allLogs[:-1])
                    nextRev = allLogs[-1]["revision"].number
                    fetched += fetchLimit
            locker.unlock()

            self.contents.clear()
            self.__pegRev = None
            for log in logs:
                ver = "{0:d}".format(log["revision"].number)
                dstr = "<b>{0} {1}</b>".format(self.revString, ver)
                if self.__pegRev is None:
                    self.__pegRev = int(ver)
                try:
                    lv = "{0:d}".format(logs[logs.index(log) + 1]["revision"].number)
                    url = QUrl()
                    url.setScheme("file")
                    url.setPath(self.filename)
                    query = lv + "_" + ver
                    url.setQuery(query)
                    dstr += ' [<a href="{0}" name="{1}">{2}</a>]'.format(
                        url.toString(), query, self.tr("diff to {0}").format(lv)
                    )
                except IndexError:
                    pass
                dstr += "<br />\n"
                self.contents.insertHtml(dstr)

                author = log["author"]
                message = log["message"]
                if sys.version_info[0] == 2:
                    author = author.decode("utf-8")
                    message = message.decode("utf-8")
                dstr = self.tr("<i>author: {0}</i><br />\n").format(author)
                self.contents.insertHtml(dstr)

                dstr = self.tr("<i>date: {0}</i><br />\n").format(formatTime(log["date"]))
                self.contents.insertHtml(dstr)

                self.contents.insertHtml("<br />\n")

                for line in message.splitlines():
                    self.contents.insertHtml(Utilities.html_encode(line))
                    self.contents.insertHtml("<br />\n")

                if len(log["changed_paths"]) > 0:
                    self.contents.insertHtml("<br />\n")
                    for changeInfo in log["changed_paths"]:
                        action = changeInfo["action"]
                        path = changeInfo["path"]
                        if sys.version_info[0] == 2:
                            action = action.decode("utf-8")
                            path = path.decode("utf-8")
                        dstr = "{0} {1}".format(self.flags[action], path)
                        if changeInfo["copyfrom_path"] is not None:
                            copyfrom_path = changeInfo["copyfrom_path"]
                            if sys.version_info[0] == 2:
                                copyfrom_path = copyfrom_path.decode("utf-8")
                            dstr += self.tr(" (copied from {0}, revision {1})").format(
                                copyfrom_path, changeInfo["copyfrom_revision"].number
                            )
                        dstr += "<br />\n"
                        self.contents.insertHtml(dstr)

                self.contents.insertHtml("<hr /><br />\n")
        except pysvn.ClientError as e:
            locker.unlock()
            self.__showError(e.args[0])
        os.chdir(cwd)
        self.__finish()
Exemple #45
0
 def systemOpen(self, url):
     from PyQt5.QtGui import QDesktopServices
     url = self.app.mountsFaker.convertToNativePath(url)
     qurl = QUrl(url)
     qurl.setScheme("file")
     QDesktopServices().openUrl(qurl)
    def __finish(self):
        """
        Private slot called when the process finished or the user pressed
        the button.
        """
        self.inputGroup.setEnabled(False)
        self.inputGroup.hide()

        self.contents.clear()

        if not self.logEntries:
            self.errors.append(
                self.tr("No log available for '{0}'").format(self.filename))
            self.errorGroup.show()
            return

        html = ""

        if self.initialText:
            for line in self.initialText:
                html += Utilities.html_encode(line.strip())
                html += '<br />\n'
            html += '{0}<br/>\n'.format(80 * "=")

        for entry in self.logEntries:
            fileCopies = {}
            if entry["file_copies"]:
                for fentry in entry["file_copies"].split(", "):
                    newName, oldName = fentry[:-1].split(" (")
                    fileCopies[newName] = oldName

            rev, hexRev = entry["change"].split(":")
            dstr = '<p><b>{0} {1}</b>'.format(self.revString, entry["change"])
            if entry["parents"]:
                parents = entry["parents"].split()
            else:
                parents = self.__getParents(rev)
            for parent in parents:
                url = QUrl()
                url.setScheme("file")
                url.setPath(self.filename)
                if qVersion() >= "5.0.0":
                    query = parent.split(":")[0] + '_' + rev
                    url.setQuery(query)
                else:
                    query = QByteArray()
                    query.append(parent.split(":")[0]).append('_').append(rev)
                    url.setEncodedQuery(query)
                dstr += ' [<a href="{0}" name="{1}" id="{1}">{2}</a>]'.format(
                    url.toString(),
                    query,
                    self.tr('diff to {0}').format(parent),
                )
            dstr += '<br />\n'
            html += dstr

            if "phase" in entry:
                html += self.tr("Phase: {0}<br />\n")\
                    .format(entry["phase"])

            html += self.tr("Branch: {0}<br />\n")\
                .format(entry["branches"])

            html += self.tr("Tags: {0}<br />\n").format(entry["tags"])

            if "bookmarks" in entry:
                html += self.tr("Bookmarks: {0}<br />\n")\
                    .format(entry["bookmarks"])

            html += self.tr("Parents: {0}<br />\n")\
                .format(entry["parents"])

            html += self.tr('<i>Author: {0}</i><br />\n')\
                .format(Utilities.html_encode(entry["user"]))

            date, time = entry["date"].split()[:2]
            html += self.tr('<i>Date: {0}, {1}</i><br />\n')\
                .format(date, time)

            for line in entry["description"]:
                html += Utilities.html_encode(line.strip())
                html += '<br />\n'

            if entry["file_adds"]:
                html += '<br />\n'
                for f in entry["file_adds"].strip().split(", "):
                    if f in fileCopies:
                        html += self.tr(
                            'Added {0} (copied from {1})<br />\n')\
                            .format(Utilities.html_encode(f),
                                    Utilities.html_encode(fileCopies[f]))
                    else:
                        html += self.tr('Added {0}<br />\n')\
                            .format(Utilities.html_encode(f))

            if entry["files_mods"]:
                html += '<br />\n'
                for f in entry["files_mods"].strip().split(", "):
                    html += self.tr('Modified {0}<br />\n')\
                        .format(Utilities.html_encode(f))

            if entry["file_dels"]:
                html += '<br />\n'
                for f in entry["file_dels"].strip().split(", "):
                    html += self.tr('Deleted {0}<br />\n')\
                        .format(Utilities.html_encode(f))

            html += '</p>{0}<br/>\n'.format(60 * "=")

        self.contents.setHtml(html)
        tc = self.contents.textCursor()
        tc.movePosition(QTextCursor.Start)
        self.contents.setTextCursor(tc)
        self.contents.ensureCursorVisible()
class AuthWidget(QWebEngineView):

    def __init__(self, parent, config=None, credential_file=None, cookie_persistence=False, log_level=logging.INFO):
        super(AuthWidget, self).__init__(parent)

        self.parent = parent
        self.config = None
        self.config_file = DEFAULT_CONFIG_FILE
        self.credential = DEFAULT_CREDENTIAL
        self.credential_file = None
        self.cookie_file = None
        self.cookie_jar = None
        self.auth_url = None
        self.authn_session = None
        self.authn_session_page = None
        self.authn_cookie_name = None
        self.authn_expires = time.time()
        self._success_callback = None
        self._failure_callback = None
        self._session = requests.session()
        self.token = None
        self.default_profile = QWebEngineProfile("deriva-auth", self)
        self.private_profile = QWebEngineProfile(self)

        logging.getLogger().setLevel(log_level)
        info = "%s v%s [Python: %s (PyQt: %s), %s]" % (
            self.__class__.__name__, get_installed_version(VERSION),
            platform.python_version(), PYQT_VERSION_STR, platform.platform(aliased=True))
        logging.info("Initializing authorization provider: %s" % info)
        self.cookie_persistence = cookie_persistence
        self._timer = QTimer(self)
        self._timer.timeout.connect(self._onTimerFired)
        self.configure(config, credential_file)

    def configure(self, config, credential_file):
        self.config = config if config else read_config(self.config_file, create_default=True, default=DEFAULT_CONFIG)
        self.credential_file = credential_file
        host = self.config.get("host")
        if not host:
            self.set_current_html(ERROR_HTML % "Could not locate hostname parameter in configuration.")
            return
        self.auth_url = QUrl()
        self.auth_url.setScheme(config.get("protocol", "https"))
        self.auth_url.setHost(host)
        if config.get("port") is not None:
            self.auth_url.setPort(config["port"])
        self.authn_cookie_name = self.config.get("cookie_name", "webauthn")

        self.cookie_file = DEFAULT_SESSION_CONFIG.get("cookie_jar")
        self.cookie_jar = load_cookies_from_file(self.cookie_file)

        retries = Retry(connect=DEFAULT_SESSION_CONFIG['retry_connect'],
                        read=DEFAULT_SESSION_CONFIG['retry_read'],
                        backoff_factor=DEFAULT_SESSION_CONFIG['retry_backoff_factor'],
                        status_forcelist=DEFAULT_SESSION_CONFIG['retry_status_forcelist'])

        self._session.mount(self.auth_url.toString() + '/',
                            HTTPAdapter(max_retries=retries))

    def set_current_html(self, html):
        page = QWebEnginePage(self.parent)
        page.setHtml(html)
        self.setPage(page)
        self.update()
        qApp.processEvents()

    def authenticated(self):
        if self.authn_session is None:
            return False

        now = time.time()
        if now >= self.authn_expires:
            return False

        return True

    def login(self):
        if not (self.auth_url and (self.auth_url.host() and self.auth_url.scheme())):
            logging.error("Missing or invalid hostname parameter in configuration.")
            return
        logging.info("Authenticating with host: %s" % self.auth_url.toString())
        qApp.setOverrideCursor(Qt.WaitCursor)
        self._cleanup()
        self.authn_session_page = QWebEnginePage(self.private_profile, self.parent) \
            if not self.cookie_persistence else QWebEnginePage(self.default_profile, self.parent)
        self.authn_session_page.profile().setPersistentCookiesPolicy(
            QWebEngineProfile.ForcePersistentCookies if self.cookie_persistence else
            QWebEngineProfile.NoPersistentCookies)
        if self.cookie_persistence:
            logging.debug("QTWebEngine persistent storage located at: %s" %
                          self.authn_session_page.profile().persistentStoragePath())
        self.authn_session_page.profile().cookieStore().cookieAdded.connect(self._onCookieAdded)
        self.authn_session_page.profile().cookieStore().cookieRemoved.connect(self._onCookieRemoved)
        self.authn_session_page.loadProgress.connect(self._onLoadProgress)
        self.authn_session_page.loadFinished.connect(self._onLoadFinished)

        self.authn_session_page.setUrl(QUrl(self.auth_url.toString() + "/authn/preauth"))
        self.setPage(self.authn_session_page)

    def logout(self, delete_cookies=False):
        if not (self.auth_url and (self.auth_url.host() and self.auth_url.scheme())):
            return
        if self.authenticated():
            try:
                logging.info("Logging out of host: %s" % self.auth_url.toString())
                if delete_cookies and self.cookie_persistence:
                    self.authn_session_page.profile().cookieStore().deleteAllCookies()
                self._session.delete(self.auth_url.toString() + "/authn/session")
                if self.credential_file:
                    creds = read_credential(self.credential_file, create_default=True)
                    host = self.auth_url.host()
                    if creds.get(host):
                        del creds[host]
                    write_credential(self.credential_file, creds)
            except Exception as e:
                logging.warning("Logout error: %s" % format_exception(e))
        self._cleanup()

    def setSuccessCallback(self, callback=None):
        self._success_callback = callback

    def setFailureCallback(self, callback=None):
        self._failure_callback = callback

    def setStatus(self, message):
        if self.window().statusBar is not None:
            self.window().statusBar().showMessage(message)

    def _execSuccessCallback(self):
        if self._success_callback:
            self._success_callback(host=self.auth_url.host(), credential=self.credential)

    def _execFailureCallback(self, message):
        if self._failure_callback:
            self._failure_callback(host=self.auth_url.host(), message=message)

    def _onTimerFired(self):
        if not self.authenticated():
            self.authn_session = None
            return
        resp = self._session.put(self.auth_url.toString() + "/authn/session")
        seconds_remaining = self.authn_session['seconds_remaining']
        self.authn_expires = time.time() + seconds_remaining + 1
        if resp.ok:
            logging.trace("webauthn session:\n%s\n", resp.json())
            logging.info("Session refreshed for: %s" % self.auth_url.host())
        else:
            logging.warning(
                "Unable to refresh session for: %s. Server responded: %s" %
                (self.auth_url.host(),
                 str.format("%s %s: %s" % (resp.status_code, resp.reason, resp.content.decode()))))

    def _onSessionContent(self, content):
        try:
            qApp.restoreOverrideCursor()
            self.set_current_html(SUCCESS_HTML)
            try:
                self.authn_session = json.loads(content)
            except json.JSONDecodeError:
                raise RuntimeError("Unable to parse response from server: %s" % content)
            seconds_remaining = self.authn_session['seconds_remaining']
            if not self._timer.isActive():
                interval = seconds_remaining // 2
                logging.info("Authentication successful for [%s]: credential refresh in %d seconds." %
                             (self.auth_url.toString(), interval))
                self._timer.start(interval * 1000)
            self.authn_expires = time.time() + seconds_remaining + 1
            logging.trace("webauthn session:\n%s\n", json.dumps(self.authn_session, indent=2))
            QTimer.singleShot(100, self._execSuccessCallback)
        except (ValueError, Exception) as e:
            error = format_exception(e)
            logging.error(error)
            self.set_current_html(ERROR_HTML % content)
            self._execFailureCallback(error)

    def _onPreAuthContent(self, content):
        try:
            if not content:
                logging.debug("no preauth content")
                return
            preauth = json.loads(content)
            logging.trace("webauthn preauth:\n%s\n", json.dumps(preauth, indent=2))
            qApp.setOverrideCursor(Qt.WaitCursor)
            self.authn_session_page.setUrl(QUrl(preauth["redirect_url"]))
        except (ValueError, Exception) as e:
            logging.error(format_exception(e))
            self.set_current_html(ERROR_HTML % content)

    def _onLoadFinished(self, result):
        qApp.restoreOverrideCursor()
        qApp.processEvents()
        if not result:
            self.setPage(self.authn_session_page)
            logging.debug("Page load error: %s" % self.authn_session_page.url().toDisplayString())
            return
        self.set_current_html(DEFAULT_HTML % self.auth_url.host())
        path = self.authn_session_page.url().path()
        if path == "/authn/preauth":
            self.authn_session_page.toPlainText(self._onPreAuthContent)
        elif path == "/authn/session":
            self.authn_session_page.toPlainText(self._onSessionContent)
        else:
            if self.page() != self.authn_session_page:
                self.page().deleteLater()
                self.setPage(self.authn_session_page)

    def _onLoadProgress(self, progress):
        self.setStatus("Loading page: %s [%d%%]" % (self.page().url().host(), progress))

    def _onCookieAdded(self, cookie):
        cookie_str = str(cookie.toRawForm(QNetworkCookie.NameAndValueOnly), encoding='utf-8')
        cookie_name = str(cookie.name(), encoding='utf-8')
        cookie_val = str(cookie.value(), encoding='utf-8')
        if (cookie_name == self.authn_cookie_name) and (cookie.domain() == self.config.get("host")):
            logging.trace("%s cookie added:\n\n%s\n\n" % (self.authn_cookie_name, cookie_str))
            self.credential["cookie"] = "%s=%s" % (self.authn_cookie_name, cookie_val)
            host = self.auth_url.host()
            cred_entry = dict()
            cred_entry[host] = self.credential
            if self.credential_file:
                creds = read_credential(self.credential_file, create_default=True)
                creds.update(cred_entry)
                write_credential(self.credential_file, creds)
            self.token = cookie_val
            self._session.cookies.set(self.authn_cookie_name, cookie_val, domain=host, path='/')
            if self.cookie_jar is not None:
                self.cookie_jar.set_cookie(
                    create_cookie(self.authn_cookie_name,
                                  cookie_val,
                                  domain=host,
                                  path='/',
                                  expires=0,
                                  discard=False,
                                  secure=True))
                for path in self.config.get("cookie_jars", DEFAULT_CONFIG["cookie_jars"]):
                    path_dir = os.path.dirname(path)
                    if os.path.isdir(path_dir):
                        logging.debug("Saving cookie jar to: %s" % path)
                        self.cookie_jar.save(path, ignore_discard=True, ignore_expires=True)
                    else:
                        logging.debug("Cookie jar save path [%s] does not exist." % path_dir)

    def _onCookieRemoved(self, cookie):
        cookie_str = str(cookie.toRawForm(QNetworkCookie.NameAndValueOnly), encoding='utf-8')
        cookie_name = str(cookie.name(), encoding='utf-8')
        if cookie_name == self.authn_cookie_name and cookie.domain() == self.url().host():
            logging.trace("%s cookie removed:\n\n%s\n\n" % (self.authn_cookie_name, cookie_str))
            if self.cookie_jar:
                self.cookie_jar.clear(cookie_name, path=cookie.path(), domain=cookie.domain())

    def _cleanup(self):
        self._timer.stop()
        self.token = None
        self.authn_session = None
        self.authn_expires = time.time()
        if self.authn_session_page:
            self.authn_session_page.loadProgress.disconnect(self._onLoadProgress)
            self.authn_session_page.loadFinished.disconnect(self._onLoadFinished)
            self.authn_session_page.profile().cookieStore().cookieAdded.disconnect(self._onCookieAdded)
            self.authn_session_page.profile().cookieStore().cookieRemoved.disconnect(self._onCookieRemoved)
            self.authn_session_page.deleteLater()
            self.authn_session_page = None
Exemple #48
0
 def __finish(self):
     """
     Private slot called when the process finished or the user pressed
     the button.
     """
     self.inputGroup.setEnabled(False)
     self.inputGroup.hide()
     
     self.contents.clear()
     
     if not self.logEntries:
         self.errors.append(self.tr("No log available for '{0}'")
                            .format(self.filename))
         self.errorGroup.show()
         return
     
     html = ""
     
     if self.initialText:
         for line in self.initialText:
             html += Utilities.html_encode(line.strip())
             html += '<br />\n'
         html += '{0}<br/>\n'.format(80 * "=")
         
     for entry in self.logEntries:
         fileCopies = {}
         if entry["file_copies"]:
             for fentry in entry["file_copies"].split(", "):
                 newName, oldName = fentry[:-1].split(" (")
                 fileCopies[newName] = oldName
         
         rev, hexRev = entry["change"].split(":")
         dstr = '<p><b>{0} {1}</b>'.format(self.revString, entry["change"])
         if entry["parents"]:
             parents = entry["parents"].split()
         else:
             parents = self.__getParents(rev)
         for parent in parents:
             url = QUrl()
             url.setScheme("file")
             url.setPath(self.filename)
             if qVersion() >= "5.0.0":
                 query = parent.split(":")[0] + '_' + rev
                 url.setQuery(query)
             else:
                 query = QByteArray()
                 query.append(parent.split(":")[0]).append('_').append(rev)
                 url.setEncodedQuery(query)
             dstr += ' [<a href="{0}" name="{1}" id="{1}">{2}</a>]'.format(
                 url.toString(), query,
                 self.tr('diff to {0}').format(parent),
             )
         dstr += '<br />\n'
         html += dstr
         
         if "phase" in entry:
             html += self.tr("Phase: {0}<br />\n")\
                 .format(entry["phase"])
         
         html += self.tr("Branch: {0}<br />\n")\
             .format(entry["branches"])
         
         html += self.tr("Tags: {0}<br />\n").format(entry["tags"])
         
         if "bookmarks" in entry:
             html += self.tr("Bookmarks: {0}<br />\n")\
                 .format(entry["bookmarks"])
         
         html += self.tr("Parents: {0}<br />\n")\
             .format(entry["parents"])
         
         html += self.tr('<i>Author: {0}</i><br />\n')\
             .format(Utilities.html_encode(entry["user"]))
         
         date, time = entry["date"].split()[:2]
         html += self.tr('<i>Date: {0}, {1}</i><br />\n')\
             .format(date, time)
         
         for line in entry["description"]:
             html += Utilities.html_encode(line.strip())
             html += '<br />\n'
         
         if entry["file_adds"]:
             html += '<br />\n'
             for f in entry["file_adds"].strip().split(", "):
                 if f in fileCopies:
                     html += self.tr(
                         'Added {0} (copied from {1})<br />\n')\
                         .format(Utilities.html_encode(f),
                                 Utilities.html_encode(fileCopies[f]))
                 else:
                     html += self.tr('Added {0}<br />\n')\
                         .format(Utilities.html_encode(f))
         
         if entry["files_mods"]:
             html += '<br />\n'
             for f in entry["files_mods"].strip().split(", "):
                 html += self.tr('Modified {0}<br />\n')\
                     .format(Utilities.html_encode(f))
         
         if entry["file_dels"]:
             html += '<br />\n'
             for f in entry["file_dels"].strip().split(", "):
                 html += self.tr('Deleted {0}<br />\n')\
                     .format(Utilities.html_encode(f))
         
         html += '</p>{0}<br/>\n'.format(60 * "=")
     
     self.contents.setHtml(html)
     tc = self.contents.textCursor()
     tc.movePosition(QTextCursor.Start)
     self.contents.setTextCursor(tc)
     self.contents.ensureCursorVisible()