def process_get_call(self,
                         url,
                         url_query_items,
                         timeout=None,
                         report_url=True):
        """
        Run a GET request and return reply data
        :param url: url for request
        :param url_query_items:
        :param timeout: in ms
        :param report_url: True if URL should be reported to feedback
        :return: response or error message in json format
        """

        url_query = QUrl(url)
        if report_url:
            self.report_info('GET ' + url_query.toString())

        if url_query_items:
            url_query.setQuery(url_query_items)

        request = QNetworkRequest(url_query)
        if self.connection.auth_cfg != '':
            request.setRawHeader("Accept".encode("utf-8"),
                                 "*/*".encode("utf-8"))
        if timeout is not None and "setTransferTimeout" in dir(request):
            request.setTransferTimeout(timeout)

        reply = self.network_access_manager.blockingGet(
            request, self.connection.auth_cfg, True, self.feedback)
        return self.process_qgs_reply(reply)
    def prepare_url(self):
        """Prepare a query to be as an URL.

        if the query is not ready to be URL prepared, a None is returned.

        :return: The URL encoded with the query.
        :rtype: basestring
        """
        if not self._query_is_ready:
            return None

        if self._output_format:
            query = re.sub(r'output="[a-z]*"',
                           'output="{}"'.format(self._output_format),
                           self._query_prepared)
            query = re.sub(r'\[out:[a-z]*',
                           '[out:{}'.format(self._output_format), query)
        else:
            query = self._query_prepared

        url_query = QUrl(self._overpass)
        query_string = QUrlQuery()
        query_string.addQueryItem('data', query)
        query_string.addQueryItem('info', 'QgisQuickOSMPlugin')
        url_query.setQuery(query_string)
        return url_query.toString()
Esempio n. 3
0
    def prepare_url(self):
        """Prepare a query to be as an URL.

        if the query is not ready to be URL prepared, a None is returned.

        :return: The URL encoded with the query.
        :rtype: basestring
        """
        if not self._query_is_ready:
            return None

        if self._output_format:
            query = re.sub(
                r'output="[a-z]*"',
                'output="%s"' % self._output_format,
                self._query_prepared)
            query = re.sub(
                r'\[out:[a-z]*',
                '[out:%s' % self._output_format,
                query)
        else:
            query = self._query_prepared

        url_query = QUrl(self._overpass)
        query_string = QUrlQuery()
        query_string.addQueryItem('data', query)
        query_string.addQueryItem('info', 'QgisQuickOSMPlugin')
        url_query.setQuery(query_string)
        return url_query.toString()
    def test_resolve_path(self):
        """Test resolving the path works correctly."""
        collection_path = test_data_path('collections', 'test_collection')
        search_paths = []

        # Test case 1: local path
        img_path = test_data_path(
            'collections', 'test_collection', 'svg', 'blastoise.svg')
        fixed_path = resolve_path(img_path, collection_path, search_paths)
        self.assertEqual(img_path, fixed_path)

        # Test case 2: local url
        img_path = test_data_path(
            'collections', 'test_collection', 'svg', 'blastoise.svg')
        img_url = QUrl.fromLocalFile(img_path)
        fixed_path = resolve_path(
            img_url.toString(), collection_path, search_paths)
        self.assertEqual(fixed_path, img_path)

        # Test case 3:  http url
        img_path = 'http://qgis.org/test/image.svg'
        img_url = QUrl(img_path)
        fixed_path = resolve_path(
            img_url.toString(), collection_path, search_paths)
        self.assertEqual(fixed_path, img_path)

        # Test case 4: checking in the svg local collection path
        img_path = '/you/would/not/find/this/charizard.svg'
        fixed_path = resolve_path(img_path, collection_path, search_paths)
        expected_path = test_data_path(
            'collections', 'test_collection', 'svg', 'charizard.svg')
        self.assertEqual(fixed_path, expected_path)

        # Test case 5: checking in the image local collection path
        img_path = '/you/would/not/find/this/pikachu.png'
        fixed_path = resolve_path(img_path, collection_path, search_paths)
        expected_path = test_data_path(
            'collections', 'test_collection', 'image', 'pikachu.png')
        self.assertEqual(fixed_path, expected_path)

        # Test case 6: checking in the search paths
        search_paths = [
            test_data_path(
                'collections', 'test_collection', 'preview')
        ]
        img_path = 'prev_1.png'
        fixed_path = resolve_path(img_path, collection_path, search_paths)
        expected_path = test_data_path(
            'collections', 'test_collection', 'preview', 'prev_1.png')
        self.assertEqual(fixed_path, expected_path)

        # Test case 7: not finding anywhere (return the original path)
        img_path = '/you/would/not/find/this/anywhere.png'
        fixed_path = resolve_path(img_path, collection_path, search_paths)
        self.assertEqual(fixed_path, img_path)
Esempio n. 5
0
    def wfs(self):
        name = self.connectionCombo.currentText()
        conn = QgsOwsConnection("wfs", name)
        uri = conn.uri().param('url')
        req_version = conn.uri().param('version')
        s = QgsSettings()
        checked_version = s.value(
            "qgis/connections-wfs/{}/checked_version".format(name), False)
        if req_version == "auto" or not checked_version:
            # detect version
            u = QUrlQuery()
            u.addQueryItem("request", "GetCapabilities")
            u.addQueryItem("service", "WFS")
            if req_version == "auto":
                u.addQueryItem("acceptversions", "2.0.0,1.1.0,1.0.0")
            elif not checked_version:
                u.addQueryItem("version", req_version)
            final_url = QUrl(uri)
            final_url.setQuery(u)

            xml, ns_map = xml_parse(remote_open_from_qgis(
                final_url.toString()))
            root = xml.getroot()
            if 'ows' in ns_map:
                versions = [
                    v.text for v in root.findall(
                        "./ows:ServiceIdentification/ows:ServiceTypeVersion",
                        ns_map)
                ]
            else:
                versions = [
                    v.text for v in root.findall(
                        "./ServiceIdentification/ServiceTypeVersion", ns_map)
                ]
            if not versions:
                if 'version' in root.attrib:
                    versions = [root.attrib['version']]
            if not versions:
                raise RuntimeError("Cannot determine WFS version")
            # take the greatest version, if more than one
            version = sorted(versions)[-1]

            if version != req_version:
                QgsMessageLog.logMessage(
                    "Requested WFS version {}, got {}".format(
                        req_version, version))
            else:
                s.setValue(
                    "qgis/connections-wfs/{}/checked_version".format(name),
                    True)
        else:
            version = req_version

        with qgis_proxy_settings():
            return WebFeatureService(url=uri, version=version)
    def test_resolve_path(self):
        """Test resolving the path works correctly."""
        collection_path = test_data_path("collections", "test_collection")
        search_paths = []

        # Test case 1: local path
        img_path = test_data_path(
            "collections", "test_collection", "svg", "blastoise.svg"
        )
        fixed_path = resolve_path(img_path, collection_path, search_paths)
        self.assertEqual(img_path, fixed_path)

        # Test case 2: local url
        img_path = test_data_path(
            "collections", "test_collection", "svg", "blastoise.svg"
        )
        img_url = QUrl.fromLocalFile(img_path)
        fixed_path = resolve_path(img_url.toString(), collection_path, search_paths)
        self.assertEqual(fixed_path, img_path)

        # Test case 3:  http url
        img_path = "http://qgis.org/test/image.svg"
        img_url = QUrl(img_path)
        fixed_path = resolve_path(img_url.toString(), collection_path, search_paths)
        self.assertEqual(fixed_path, img_path)

        # Test case 4: checking in the svg local collection path
        img_path = "/you/would/not/find/this/charizard.svg"
        fixed_path = resolve_path(img_path, collection_path, search_paths)
        expected_path = test_data_path(
            "collections", "test_collection", "svg", "charizard.svg"
        )
        self.assertEqual(fixed_path, expected_path)

        # Test case 5: checking in the image local collection path
        img_path = "/you/would/not/find/this/pikachu.png"
        fixed_path = resolve_path(img_path, collection_path, search_paths)
        expected_path = test_data_path(
            "collections", "test_collection", "image", "pikachu.png"
        )
        self.assertEqual(fixed_path, expected_path)

        # Test case 6: checking in the search paths
        search_paths = [test_data_path("collections", "test_collection", "preview")]
        img_path = "prev_1.png"
        fixed_path = resolve_path(img_path, collection_path, search_paths)
        expected_path = test_data_path(
            "collections", "test_collection", "preview", "prev_1.png"
        )
        self.assertEqual(fixed_path, expected_path)

        # Test case 7: not finding anywhere (return the original path)
        img_path = "/you/would/not/find/this/anywhere.png"
        fixed_path = resolve_path(img_path, collection_path, search_paths)
        self.assertEqual(fixed_path, img_path)
Esempio n. 7
0
def clean_ows_url(url):
    """clean an OWS URL of added basic service parameters"""

    url = QUrl(url)
    query_string = url.query()

    if query_string:
        query_string = QUrlQuery(query_string)
        query_string.removeQueryItem('service')
        query_string.removeQueryItem('SERVICE')
        query_string.removeQueryItem('request')
        query_string.removeQueryItem('REQUEST')
        url.setQuery(query_string)

    return url.toString()
Esempio n. 8
0
def clean_ows_url(url):
    """clean an OWS URL of added basic service parameters"""

    url = QUrl(url)
    query_string = url.query()

    if query_string:
        query_string = QUrlQuery(query_string)
        query_string.removeQueryItem('service')
        query_string.removeQueryItem('SERVICE')
        query_string.removeQueryItem('request')
        query_string.removeQueryItem('REQUEST')
        url.setQuery(query_string)

    return url.toString()
    def process_post_call(self,
                          url,
                          url_query_items,
                          data,
                          is_read_only=True,
                          report_url=True):
        """
        Run a POST request and return reply data
        :param url: url for request
        :param url_query_items:
        :param data:
        :param is_read_only: True if the request does not update data
        :param report_url: True if URL should be reported to feedback
        :return: response or error message in json format
        """

        if self.connection.read_only and not is_read_only:
            return {
                "error": {
                    "msg": "Graphium connection is set to read-only!"
                }
            }

        url_query = QUrl(url)
        if report_url:
            self.report_info('POST ' + url_query.toString())

        if url_query_items:
            url_query.setQuery(url_query_items)

        # data_byte_array = json.dumps(data).encode('utf8')
        # data = QtCore.QByteArray( json.dumps( json_request ) )
        data_byte_array = QJsonDocument.fromVariant(data)

        request = QNetworkRequest(url_query)
        if self.connection.auth_cfg != '':
            request.setRawHeader("Accept".encode("utf-8"),
                                 "*/*".encode("utf-8"))
        request.setHeader(QNetworkRequest.ContentTypeHeader,
                          "application/json")
        reply = self.network_access_manager.blockingPost(
            request, data_byte_array.toJson(), self.connection.auth_cfg, True,
            self.feedback)
        return self.process_qgs_reply(reply)
    def process_put_call(self, url, data=None, report_url=True):
        """
        Run a PUT request and return reply data
        :param url: url for request
        :param data:
        :param report_url: True if URL should be reported to feedback
        :return: response or error message in json format
        """

        if self.connection.read_only:
            return {
                "error": {
                    "msg": "Graphium connection is set to read-only!"
                }
            }

        url_query = QUrl(url)
        if report_url:
            self.report_info('PUT ' + url_query.toString())

        data_byte_array = QJsonDocument.fromVariant(data)

        request = QNetworkRequest(url_query)
        if self.connection.auth_cfg != '':
            self.auth = 0
            config = QgsAuthMethodConfig()
            QgsApplication.authManager().loadAuthenticationConfig(
                self.connection.auth_cfg, config, True)
            concatenated = config.configMap(
            )['username'] + ":" + config.configMap()['password']

            data = base64.encodebytes(concatenated.encode("utf-8")).replace(
                '\n'.encode("utf-8"), ''.encode("utf-8"))
            request.setRawHeader("Authorization".encode("utf-8"),
                                 ("Basic %s" % data).encode("utf-8"))
            request.setRawHeader("Accept".encode("utf-8"),
                                 "*/*".encode("utf-8"))
        loop = QEventLoop()  # https://stackoverflow.com/a/46514984
        reply = self.network_access_manager.put(request,
                                                data_byte_array.toJson())
        reply.finished.connect(loop.quit)
        loop.exec_()

        return self.process_q_reply(reply)
    class MyUrl:

        def __init__(self, url):
            self.url = QUrl(url)
            self.query = QUrlQuery()

        @classmethod
        def fromLocalFile(cls, filename):
            return cls(QUrl.fromLocalFile(filename))

        def addQueryItem(self, k, v):
            self.query.addQueryItem(k, v)

        def toString(self):
            urlstr = self.url.toString()
            querystr = self.query.toString(QUrl.FullyDecoded)
            if querystr != '':
                urlstr += '?'
                urlstr += querystr
            return urlstr
    def process_delete_call(self, url, report_url=True):
        """
        Run a DELETE request and return reply data
        :param url: url for request
        :param report_url: True if URL should be reported to feedback
        :return: response or error message in json format
        """

        if self.connection.read_only:
            return {
                "error": {
                    "msg": "Graphium connection is set to read-only!"
                }
            }

        url_query = QUrl(url)
        if report_url:
            self.report_info('DELETE ' + url_query.toString())

        request = QNetworkRequest(url_query)
        if self.connection.auth_cfg != '':
            self.auth = 0
            config = QgsAuthMethodConfig()
            QgsApplication.authManager().loadAuthenticationConfig(
                self.connection.auth_cfg, config, True)
            concatenated = config.configMap(
            )['username'] + ":" + config.configMap()['password']

            data = base64.encodebytes(concatenated.encode("utf-8")).replace(
                '\n'.encode("utf-8"), ''.encode("utf-8"))
            request.setRawHeader("Authorization".encode("utf-8"),
                                 ("Basic %s" % data).encode("utf-8"))
            request.setRawHeader("Accept".encode("utf-8"),
                                 "*/*".encode("utf-8"))
        loop = QEventLoop()
        reply = self.network_access_manager.deleteResource(request)
        reply.finished.connect(loop.quit)
        loop.exec_()

        return self.process_q_reply(reply)
Esempio n. 13
0
 def viewurl(self, url: QUrl) -> None:
     """
     Open a URL in Roam
     :param url: The URL to view. bb
     :return:
     """
     key = url.toString().lstrip('file://')
     try:
         # Hack. Eww fix me.
         data, imagetype = roam.htmlviewer.images[os.path.basename(key)]
         pix = QPixmap()
         if imagetype == 'base64':
             pix.loadFromData(data)
         else:
             pix.load(data)
         self.openimage(pix)
     except KeyError:
         pix = QPixmap()
         pix.load(key)
         if pix.isNull():
             QDesktopServices.openUrl(url)
             return
         self.openimage(pix)
Esempio n. 14
0
    def test_real_wrong_request(self):
        """Test wrong request.

        This is test is using internet.
        """
        url = QUrl(OVERPASS_SERVERS[0])
        query_string = QUrlQuery()
        query_string.addQueryItem('data', 'fake_query')
        url.setQuery(query_string)
        overpass = ConnexionOAPI(url.toString())

        self.assertListEqual(overpass.errors, [])

        # We don't want the FileNotFoundError
        try:
            overpass.run()
        except OverpassBadRequestException:
            self.assertTrue(True)
        except FileNotFoundError:
            self.assertFalse(True)
        else:
            self.assertFalse(True)

        self.assertEqual(len(overpass.errors), 1)
Esempio n. 15
0
    def prepare_url(self):
        """Prepare a query to be as an URL.

        :return: The URL encoded with the query.
        :rtype: basestring
        """
        if not self._query_prepared:
            return ''

        if self._output_format:
            query = re.sub(r'output="[a-z]*"',
                           'output="%s"' % self._output_format,
                           self._query_prepared)
            query = re.sub(r'\[out:[a-z]*', '[out:%s' % self._output_format,
                           query)
        else:
            query = self._query_prepared

        url_query = QUrl(self._overpass)
        query_string = QUrlQuery()
        query_string.addQueryItem('data', query)
        query_string.addQueryItem('info', 'QgisQuickOSMPlugin')
        url_query.setQuery(query_string)
        return url_query.toString()
Esempio n. 16
0
 def open_link(url: QUrl) -> bool:
     return webbrowser.open_new_tab(url.toString())