Ejemplo n.º 1
0
    def test_get_url2(self):
        from c2cgeoportal.lib import get_url2

        request = create_dummy_request({
            "package": "my_project",
            "servers": {
                "srv": "https://example.com/test",
                "srv_alt": "https://example.com/test/",
            },
        })

        def static_url(path, **kwargs):
            return "http://server.org/" + path
        request.static_url = static_url

        self.assertEquals(get_url2("test", "static://pr:st/icon.png", request, set()), "http://server.org/pr:st/icon.png")
        self.assertEquals(get_url2("test", "static:///icon.png", request, set()), "http://server.org/my_project:static/icon.png")
        self.assertEquals(get_url2("test", "config://srv/icon.png", request, set()), "https://example.com/test/icon.png")
        self.assertEquals(get_url2("test", "config://srv/", request, set()), "https://example.com/test/")
        self.assertEquals(get_url2("test", "config://srv", request, set()), "https://example.com/test/")
        self.assertEquals(get_url2("test", "config://srv/icon.png?test=aaa", request, set()), "https://example.com/test/icon.png?test=aaa")
        self.assertEquals(get_url2("test", "config://srv_alt/icon.png", request, set()), "https://example.com/test/icon.png")
        self.assertEquals(get_url2("test", "http://example.com/icon.png", request, set()), "http://example.com/icon.png")
        self.assertEquals(get_url2("test", "https://example.com/icon.png", request, set()), "https://example.com/icon.png")
        errors = set()
        self.assertEquals(get_url2("test", "config://srv2/icon.png", request, errors=errors), None)
        self.assertEquals(errors, set(["The server 'srv2' isn't found in the config"]))
Ejemplo n.º 2
0
    def test_get_url2(self):
        from c2cgeoportal.lib import get_url2

        request = create_dummy_request({
            "package": "my_project",
            "servers": {
                "srv": "https://example.com/test",
                "srv_alt": "https://example.com/test/",
            },
        })

        def static_url(path, **kwargs):
            return "http://server.org/" + path

        request.static_url = static_url

        self.assertEquals(
            get_url2("test", "static://pr:st/icon.png", request, set()),
            "http://server.org/pr:st/icon.png")
        self.assertEquals(
            get_url2("test", "static:///icon.png", request, set()),
            "http://server.org/my_project:static/icon.png")
        self.assertEquals(
            get_url2("test", "config://srv/icon.png", request, set()),
            "https://example.com/test/icon.png")
        self.assertEquals(get_url2("test", "config://srv/", request, set()),
                          "https://example.com/test/")
        self.assertEquals(get_url2("test", "config://srv", request, set()),
                          "https://example.com/test/")
        self.assertEquals(
            get_url2("test", "config://srv/icon.png?test=aaa", request, set()),
            "https://example.com/test/icon.png?test=aaa")
        self.assertEquals(
            get_url2("test", "config://srv_alt/icon.png", request, set()),
            "https://example.com/test/icon.png")
        self.assertEquals(
            get_url2("test", "http://example.com/icon.png", request, set()),
            "http://example.com/icon.png")
        self.assertEquals(
            get_url2("test", "https://example.com/icon.png", request, set()),
            "https://example.com/icon.png")
        errors = set()
        self.assertEquals(
            get_url2("test", "config://srv2/icon.png", request, errors=errors),
            None)
        self.assertEquals(errors,
                          set(["The server 'srv2' isn't found in the config"]))
Ejemplo n.º 3
0
 def _get_wms_url(self):
     errors = set()
     url = get_url2(
         "The OGC server '{}'".format(self._get_ogc_server().name),
         self._get_ogc_server().url, self.request, errors)
     if len(errors) > 0:  # pragma: no cover
         log.error("\n".join(errors))
     return url
Ejemplo n.º 4
0
 def _get_wms_url(self):
     errors = set()
     url = get_url2(
         "The OGC server '{}'".format(self._get_ogc_server().name),
         self._get_ogc_server().url, self.request, errors
     )
     if len(errors) > 0:  # pragma: no cover
         log.error("\n".join(errors))
     return url
Ejemplo n.º 5
0
    def _layer_attributes(self, url, layer):
        errors = set()

        class Registry:
            setting = None

        class Request:
            registry = Registry()

        request = Request()
        request.registry.settings = self.config
        # static schema will not be supported
        url = get_url2("Layer", url, request, errors)
        if len(errors) > 0:
            print("\n".join(errors))
            return []

        wms_getcap_url = add_url_params(url, {
            "SERVICE": "WMS",
            "VERSION": "1.1.1",
            "REQUEST": "GetCapabilities",
        })

        hostname = urlsplit(url).hostname
        if url not in self.wmscap_cache:
            print("Get WMS GetCapabilities for URL: {}".format(url))
            self.wmscap_cache[url] = None

            # forward request to target (without Host Header)
            http = httplib2.Http()
            h = {}
            if hostname == "localhost":  # pragma: no cover
                h["Host"] = self.package["host"]
            try:
                resp, content = http.request(wms_getcap_url,
                                             method="GET",
                                             headers=h)

                try:
                    self.wmscap_cache[url] = WebMapService(None, xml=content)
                except Exception as e:
                    print(
                        colorize(
                            "ERROR! an error occurred while trying to "
                            "parse the GetCapabilities document.", RED))
                    print(colorize(str(e), RED))
                    print("URL: {}\nxml:\n{}".format(wms_getcap_url, content))
                    if os.environ.get("IGNORE_I18N_ERRORS", "FALSE") != "TRUE":
                        raise
            except Exception as e:  # pragma: no cover
                print(colorize(str(e), RED))
                print(
                    colorize(
                        "ERROR! Unable to GetCapabilities from URL: {}".format(
                            wms_getcap_url),
                        RED,
                    ))
                if os.environ.get("IGNORE_I18N_ERRORS", "FALSE") != "TRUE":
                    raise

        wmscap = self.wmscap_cache[url]

        wfs_descrfeat_url = add_url_params(
            url, {
                "SERVICE": "WFS",
                "VERSION": "1.1.0",
                "REQUEST": "DescribeFeatureType",
            })

        if url not in self.featuretype_cache:
            print("Get WFS DescribeFeatureType for URL: {}".format(
                wfs_descrfeat_url))
            self.featuretype_cache[url] = None

            # forward request to target (without Host Header)
            http = httplib2.Http()
            h = {}
            if hostname == "localhost":  # pragma: no cover
                h["Host"] = self.package["host"]
            try:
                resp, content = http.request(wfs_descrfeat_url,
                                             method="GET",
                                             headers=h)
            except Exception as e:  # pragma: no cover
                print(colorize(str(e), RED))
                print(
                    colorize(
                        "ERROR! Unable to DescribeFeatureType from URL: {}".
                        format(wfs_descrfeat_url),
                        RED,
                    ))
                if os.environ.get("IGNORE_I18N_ERRORS", "FALSE") == "TRUE":
                    return []
                else:
                    raise

            if resp.status < 200 or resp.status >= 300:  # pragma: no cover
                print(
                    colorize(
                        "ERROR! DescribeFeatureType from URL {} return the error: {1:d} {}"
                        .format(wfs_descrfeat_url, resp.status, resp.reason),
                        RED,
                    ))
                if os.environ.get("IGNORE_I18N_ERRORS", "FALSE") == "TRUE":
                    return []
                else:
                    raise Exception("Aborted")

            try:
                describe = parseString(content)
                featurestype = {}
                self.featuretype_cache[url] = featurestype
                for type_element in describe.getElementsByTagNameNS(
                        "http://www.w3.org/2001/XMLSchema", "complexType"):
                    featurestype[type_element.getAttribute(
                        "name")] = type_element
            except ExpatError as e:
                print(
                    colorize(
                        "ERROR! an error occurred while trying to "
                        "parse the DescribeFeatureType document.", RED))
                print(colorize(str(e), RED))
                print("URL: {}\nxml:\n{}".format(wfs_descrfeat_url, content))
                if os.environ.get("IGNORE_I18N_ERRORS", "FALSE") == "TRUE":
                    return []
                else:
                    raise
            except AttributeError:
                print(
                    colorize(
                        "ERROR! an error occurred while trying to "
                        "read the Mapfile and recover the themes.", RED))
                print("URL: {}\nxml:\n{}".format(wfs_descrfeat_url, content))
                if os.environ.get("IGNORE_I18N_ERRORS", "FALSE") == "TRUE":
                    return []
                else:
                    raise
        else:
            featurestype = self.featuretype_cache[url]

        if featurestype is None:
            return []

        layers = [layer]
        if wmscap is not None and layer in list(wmscap.contents):
            layer_obj = wmscap[layer]
            if len(layer_obj.layers) > 0:
                layers = [l.name for l in layer_obj.layers]

        attributes = []
        for sub_layer in layers:
            # Should probably be adapted for other king of servers
            type_element = featurestype.get("{}Type".format(sub_layer))
            if type_element is not None:
                for element in type_element.getElementsByTagNameNS(
                        "http://www.w3.org/2001/XMLSchema", "element"):
                    if not element.getAttribute("type").startswith("gml:"):
                        attributes.append(element.getAttribute("name"))

        return attributes
Ejemplo n.º 6
0
    def _layer_attributes(self, url, layer):
        errors = set()

        class Registry:
            setting = None

        class Request:
            registry = Registry()

        request = Request()
        request.registry.settings = self.config
        # static schema will not be supported
        url = get_url2("Layer", url, request, errors)
        if len(errors) > 0:
            print("\n".join(errors))
            return []

        url = add_url_params(
            url, {
                "SERVICE": "WFS",
                "VERSION": "1.1.0",
                "REQUEST": "DescribeFeatureType",
            })

        if url not in self.featuretype_cache:
            print("Get DescribeFeatureType for url: %s" % url)
            self.featuretype_cache[url] = None

            # forward request to target (without Host Header)
            http = httplib2.Http()
            h = {}
            if urlsplit(url).hostname == "localhost":  # pragma: no cover
                h["Host"] = self.package["host"]
            try:
                resp, content = http.request(url, method="GET", headers=h)
            except:  # pragma: no cover
                print("Unable to DescribeFeatureType from URL %s" % url)
                self.featuretype_cache[url] = None
                return []

            if resp.status < 200 or resp.status >= 300:  # pragma: no cover
                print(
                    "DescribeFeatureType from URL %s return the error: %i %s" %
                    (url, resp.status, resp.reason))
                self.featuretype_cache[url] = None
                return []

            try:
                describe = parseString(content)
                self.featuretype_cache[url] = describe
            except AttributeError:
                print("WARNING! an error occured while trying to "
                      "read the Mapfile and recover the themes.")
                print("URL: %s\nxml:\n%s" % (url, content))
        else:
            describe = self.featuretype_cache[url]

        if describe is None:
            return []

        attributes = []
        # Should probably be adapted for other king of servers
        for type_element in describe.getElementsByTagNameNS(
                "http://www.w3.org/2001/XMLSchema", "complexType"):
            if type_element.getAttribute("name") == "%sType" % layer:
                for element in type_element.getElementsByTagNameNS(
                        "http://www.w3.org/2001/XMLSchema", "element"):
                    if not element.getAttribute("type").startswith("gml:"):
                        attributes.append(element.getAttribute("name"))
        return attributes
Ejemplo n.º 7
0
    def _layer_attributes(self, url, layer):
        errors = set()

        class Registry:
            setting = None

        class Request:
            registry = Registry()

        request = Request()
        request.registry.settings = self.config
        # static schema will not be supported
        url = get_url2("Layer", url, request, errors)
        if len(errors) > 0:
            print("\n".join(errors))
            return []

        url = add_url_params(url, {
            "SERVICE": "WFS",
            "VERSION": "1.1.0",
            "REQUEST": "DescribeFeatureType",
        })

        if url not in self.featuretype_cache:
            print("Get DescribeFeatureType for url: %s" % url)
            self.featuretype_cache[url] = None

            # forward request to target (without Host Header)
            http = httplib2.Http()
            h = {}
            if urlsplit(url).hostname == "localhost":  # pragma: no cover
                h["Host"] = self.package["host"]
            try:
                resp, content = http.request(url, method="GET", headers=h)
            except:  # pragma: no cover
                print("Unable to DescribeFeatureType from URL %s" % url)
                self.featuretype_cache[url] = None
                return []

            if resp.status < 200 or resp.status >= 300:  # pragma: no cover
                print(
                    "DescribeFeatureType from URL %s return the error: %i %s" %
                    (url, resp.status, resp.reason)
                )
                self.featuretype_cache[url] = None
                return []

            try:
                describe = parseString(content)
                self.featuretype_cache[url] = describe
            except AttributeError:
                print(
                    "WARNING! an error occured while trying to "
                    "read the Mapfile and recover the themes."
                )
                print("URL: %s\nxml:\n%s" % (url, content))
        else:
            describe = self.featuretype_cache[url]

        if describe is None:
            return []

        attributes = []
        # Should probably be adapted for other king of servers
        for type_element in describe.getElementsByTagNameNS(
            "http://www.w3.org/2001/XMLSchema", "complexType"
        ):
            if type_element.getAttribute("name") == "%sType" % layer:
                for element in type_element.getElementsByTagNameNS(
                    "http://www.w3.org/2001/XMLSchema", "element"
                ):
                    if not element.getAttribute("type").startswith("gml:"):
                        attributes.append(element.getAttribute("name"))
        return attributes