Esempio n. 1
0
 def test_add_url_params(self):
     from c2cgeoportal.lib import add_url_params
     params = {"Name": "Bob", "Age": 18, "Nationality": u"Việt Nam"}
     result = add_url_params("http://test/", params)
     self.assertEqual(
         result,
         "http://test/?Nationality=Vi%E1%BB%87t+Nam&Age=18&Name=Bob")
Esempio n. 2
0
 def wfscapabilities(self):
     _url = add_url_params(self.request.route_url("mapserverproxy"), {
         "SERVICE": "WFS",
         "VERSION": "1.1.0",
         "REQUEST": "GetCapabilities",
     })
     return self.make_response(self.testurl(_url))
Esempio n. 3
0
    def themes_errors(self):
        from c2cgeoportal.models import DBSession, Interface

        settings = self.settings.get("themes", {})

        url = self.request.route_url("themes")
        h = Http()
        default_params = settings.get("default", {}).get("params", {})
        results = []
        for interface, in DBSession.query(Interface.name).all():
            params = {}
            params.update(default_params)
            params.update(settings.get(interface, {}).get("params", {}))
            params["interface"] = interface
            interface_url = add_url_params(url, params)

            interface_url, headers = build_url("Check the theme",
                                               interface_url, self.request)

            resp, content = h.request(interface_url, headers=headers)

            if resp.status != httplib.OK:
                self.set_status(resp.status, resp.reason)
                results.append("{}: {}".format(interface, content))

            result = loads(content)

            if len(result["errors"]) != 0:
                self.set_status(500, "Theme with error")

                results.append("{}: Theme with error\n{}".format(
                    Interface.name, "\n".join(result["errors"])))

        return self.make_response("OK" if len(results) ==
                                  0 else urllib.unquote("\n\n".join(results)))
Esempio n. 4
0
    def themes_errors(self):
        from c2cgeoportal.models import DBSession, Interface

        settings = self.settings.get("themes", {})

        url = self.request.route_url("themes")
        h = Http()
        default_params = settings.get("default", {}).get("params", {})
        for (interface,) in DBSession.query(Interface.name).all():
            params = {}
            params.update(default_params)
            params.update(settings.get(interface, {}).get("params", {}))
            params["interface"] = interface
            interface_url = add_url_params(url, params)

            interface_url, headers = build_url("Check the theme", interface_url, self.request)

            resp, content = h.request(interface_url, headers=headers)

            if resp.status != httplib.OK:
                self.set_status(resp.status, resp.reason)
                return self.make_response(content)

            result = loads(content)

            if len(result["errors"]) != 0:
                self.set_status(500, "Theme with error")

                return self.make_response(
                    "Theme with error for interface '%s'\n%s" % (Interface.name, "\n".join(result["errors"]))
                )

        return self.make_response("OK")
Esempio n. 5
0
 def routes(self):
     named_urls = [(
         route["name"],
         add_url_params(
             self.request.route_url(route["name"]),
             route.get("params", {}),
         ),
     ) for route in self.settings["routes"]
                   if route["name"] not in self.settings["routes_disable"]]
     return self.testurls(named_urls)
Esempio n. 6
0
def wms_structure(wms_url, host):
    url = urlsplit(wms_url)
    wms_url = add_url_params(wms_url, {
        "SERVICE": "WMS",
        "VERSION": "1.1.1",
        "REQUEST": "GetCapabilities",
    })

    # Forward request to target (without Host Header)
    http = httplib2.Http()
    headers = dict()
    if url.hostname == "localhost" and host is not None:  # pragma: no cover
        headers["Host"] = host
    try:
        resp, content = http.request(wms_url, method="GET", headers=headers)
    except:  # pragma: no cover
        raise HTTPBadGateway(
            "Unable to GetCapabilities from wms_url {0!s}".format(wms_url))

    if resp.status < 200 or resp.status >= 300:  # pragma: no cover
        raise HTTPBadGateway(
            "GetCapabilities from wms_url {0!s} return the error: {1:d} {2!s}".
            format(wms_url, resp.status, resp.reason))

    try:
        wms = WebMapService(None, xml=content)
        result = {}

        def _fill(name, parent):
            if parent is None:
                return
            if parent.name not in result:
                result[parent.name] = []
            result[parent.name].append(name)
            _fill(name, parent.parent)

        for layer in list(wms.contents.values()):
            _fill(layer.name, layer.parent)
        return result

    except AttributeError:  # pragma: no cover
        error = "WARNING! an error occurred while trying to " \
            "read the mapfile and recover the themes."
        error = "{0!s}\nurl: {1!s}\nxml:\n{2!s}".format(
            error, wms_url, content)
        log.exception(error)
        raise HTTPBadGateway(error)

    except SyntaxError:  # pragma: no cover
        error = "WARNING! an error occurred while trying to " \
            "read the mapfile and recover the themes."
        error = "{0!s}\nurl: {1!s}\nxml:\n{2!s}".format(
            error, wms_url, content)
        log.exception(error)
        raise HTTPBadGateway(error)
    def _layer_attributes(self, url, layer):
        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.capabilities_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.capabilities_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.getElementsByTagName("complexType"):
            if type_element.getAttribute("name") == "%sType" % layer:
                for element in type_element.getElementsByTagName("element"):
                    if not element.getAttribute("type").startswith("gml:"):
                        attributes.append(element.getAttribute("name"))
        return attributes
Esempio n. 8
0
 def _fill_legend_rule_query_string(self, l, layer, url):
     if layer.legend_rule and url:
         l["icon"] = add_url_params(url, {
             "SERVICE": "WMS",
             "VERSION": "1.1.1",
             "REQUEST": "GetLegendGraphic",
             "LAYER": layer.name,
             "FORMAT": "image/png",
             "TRANSPARENT": "TRUE",
             "RULE": layer.legend_rule,
         })
Esempio n. 9
0
    def _pdf(self):
        body = {
            "comment": "Foobar",
            "title": "Bouchon",
            "units": "m",
            "srs": "EPSG:%i" % self.request.registry.settings["srid"],
            "dpi": 254,
            "layers": [],
            "layout": self.settings["print_template"],
            "pages": [{
                "center": [self.settings["print_center_lon"], self.settings["print_center_lat"]],
                "col0": "",
                "rotation": 0,
                "scale": self.settings["print_scale"],
                "table": {
                    "columns": ["col0"],
                    "data": [{
                        "col0": ""
                    }]
                }
            }]
        }
        body = dumps(body)

        _url = add_url_params(self.request.route_url("printproxy_create"), {
            "url": self.request.route_url("printproxy"),
        })
        h = Http()

        log.info("Checker for printproxy request (create): %s" % _url)
        _url = _url.replace(self.request.environ.get("SERVER_NAME"), "localhost")
        headers = {
            "Content-Type": "application/json;charset=utf-8",
            "Host": self.request.environ.get("HTTP_HOST")
        }
        resp, content = h.request(_url, "POST", headers=headers, body=body)

        if resp.status != httplib.OK:
            self.set_status(resp.status, resp.reason)
            return "Failed creating PDF: " + content

        log.info("Checker for printproxy pdf (retrieve): %s" % _url)
        json = loads(content)
        _url = json["getURL"].replace(self.request.environ.get("SERVER_NAME"), "localhost")
        headers = {"Host": self.request.environ.get("HTTP_HOST")}
        resp, content = h.request(_url, headers=headers)

        if resp.status != httplib.OK:
            self.set_status(resp.status, resp.reason)
            return "Failed retrieving PDF: " + content

        return "OK"
Esempio n. 10
0
 def test_add_url_params_port(self):
     presult = urlparse(add_url_params(
         "http://example.com:8480/toto",
         {"1": "2"}
     ))
     self.assertEqual(presult.scheme, "http")
     self.assertEqual(presult.netloc, "example.com:8480")
     self.assertEqual(presult.path, "/toto")
     self.assertEqual(presult.params, "")
     self.assertEqual(presult.fragment, "")
     self.assertEqual(parse_qs(presult.query), {
         "1": ["2"],
     })
Esempio n. 11
0
 def test_add_url_params_encode1(self):
     presult = urlparse(add_url_params(
         "http://example.com/toto",
         {"à": "é"}
     ))
     self.assertEqual(presult.scheme, "http")
     self.assertEqual(presult.netloc, "example.com")
     self.assertEqual(presult.path, "/toto")
     self.assertEqual(presult.params, "")
     self.assertEqual(presult.fragment, "")
     self.assertEqual(parse_qs(presult.query), {
         "à": ["é"],
     })
Esempio n. 12
0
 def routes(self):
     named_urls = [
         (
             route["name"],
             add_url_params(
                 self.request.route_url(route["name"]),
                 route.get("params", {}),
             ),
         )
         for route in self.settings["routes"]
         if route["name"] not in self.settings["routes_disable"]
     ]
     return self.testurls(named_urls)
Esempio n. 13
0
 def _fill_wms(self, l, layer, version=1):
     l["imageType"] = layer.image_type
     if version == 1 and layer.legend_rule:
         l["icon"] = add_url_params(self.request.route_url("mapserverproxy"), {
             "SERVICE": "WMS",
             "VERSION": "1.1.1",
             "REQUEST": "GetLegendGraphic",
             "LAYER": layer.name,
             "FORMAT": "image/png",
             "TRANSPARENT": "TRUE",
             "RULE": layer.legend_rule,
         })
     if layer.style:
         l["style"] = layer.style
Esempio n. 14
0
    def _pdf(self):
        body = {
            "comment": "Foobar",
            "title": "Bouchon",
            "units": "m",
            "srs": "EPSG:%i" % self.request.registry.settings["srid"],
            "dpi": 254,
            "layers": [],
            "layout": self.settings["print_template"],
            "pages": [{
                "center": [self.settings["print_center_lon"], self.settings["print_center_lat"]],
                "col0": "",
                "rotation": 0,
                "scale": self.settings["print_scale"],
                "table": {
                    "columns": ["col0"],
                    "data": [{
                        "col0": ""
                    }]
                }
            }]
        }
        body = dumps(body)

        url = add_url_params(self.request.route_url("printproxy_create"), {
            "url": self.request.route_url("printproxy"),
        })
        url, headers = build_url("Check the printproxy request (create)", url, self.request, {
            "Content-Type": "application/json;charset=utf-8",
        })

        h = Http()
        resp, content = h.request(url, "POST", headers=headers, body=body)

        if resp.status != httplib.OK:
            self.set_status(resp.status, resp.reason)
            return "Failed creating PDF: " + content

        json = loads(content)
        url, headers = build_url(
            "Check the printproxy pdf (retrieve)", json["getURL"], self.request
        )

        resp, content = h.request(url, headers=headers)

        if resp.status != httplib.OK:
            self.set_status(resp.status, resp.reason)
            return "Failed retrieving PDF: " + content

        return "OK"
Esempio n. 15
0
def _wms_structure(wms_url, host):
    url = urlsplit(wms_url)
    wms_url = add_url_params(wms_url, {
        "SERVICE": "WMS",
        "VERSION": "1.1.1",
        "REQUEST": "GetCapabilities",
    })

    log.info("Get WMS GetCapabilities for URL: %s" % wms_url)

    # forward request to target (without Host Header)
    http = httplib2.Http()
    headers = dict()
    if url.hostname == "localhost" and host is not None:  # pragma: no cover
        headers["Host"] = host
    try:
        resp, content = http.request(wms_url, method="GET", headers=headers)
    except:  # pragma: no cover
        raise HTTPBadGateway("Unable to GetCapabilities from wms_url %s" % wms_url)

    if resp.status < 200 or resp.status >= 300:  # pragma: no cover
        raise HTTPBadGateway(
            "GetCapabilities from wms_url %s return the error: %i %s" %
            (wms_url, resp.status, resp.reason)
        )

    try:
        wms = WebMapService(None, xml=content)
        result = {}

        def _fill(name, parent):
            if parent is None:
                return
            if parent.name not in result:
                result[parent.name] = []
            result[parent.name].append(name)
            _fill(name, parent.parent)

        for layer in wms.contents.values():
            _fill(layer.name, layer.parent)
        return result

    except AttributeError:  # pragma: no cover
        error = "WARNING! an error occured while trying to " \
            "read the mapfile and recover the themes."
        error = "%s\nurl: %s\nxml:\n%s" % (error, wms_url, content)
        log.exception(error)
        raise HTTPBadGateway(error)
Esempio n. 16
0
 def test_add_url_params(self):
     from c2cgeoportal.lib import add_url_params
     from urllib.parse import urlparse, parse_qs
     params = {"Name": "Bob", "Age": 18, "Nationality": "Việt Nam"}
     result = add_url_params("http://test/", params)
     presult = urlparse(result)
     self.assertEqual(presult.scheme, "http")
     self.assertEqual(presult.netloc, "test")
     self.assertEqual(presult.path, "/")
     self.assertEqual(presult.params, "")
     self.assertEqual(presult.fragment, "")
     self.assertEqual(parse_qs(presult.query), {
         "Name": ["Bob"],
         "Age": ["18"],
         "Nationality": ["Việt Nam"],
     })
Esempio n. 17
0
    def _layer_attributes(self, url, layer):
        url = add_url_params(url, {
            "SERVICE": "WFS",
            "VERSION": "1.1.0",
            "REQUEST": "DescribeFeatureType",
            "FEATURES": layer,
        })

        print("Get DescribeFeatureType for url: %s" % url)

        # 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.capabilities_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.capabilities_cache[url] = None
            return []

        try:
            describe = parseString(content)
        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))

        attributes = []
        for element in describe.getElementsByTagName("element"):
            if element.getAttribute("type") not in [
                "gml:PointPropertyType", "gml:LineStringPropertyType", "gml:PolygonPropertyType"
            ]:
                attributes.append(element.getAttribute("name"))
        return attributes
Esempio n. 18
0
    def _layer_attributes(self, url, layer):
        url = add_url_params(
            url, {
                "SERVICE": "WFS",
                "VERSION": "1.1.0",
                "REQUEST": "DescribeFeatureType",
                "TYPENAME": layer,
            })

        print("Get DescribeFeatureType for url: %s" % url)

        # forward request to target (without Host Header)
        http = httplib2.Http()
        h = dict(self.request.headers)
        if urlsplit(url).hostname != "localhost":  # pragma: no cover
            h.pop("Host")
        try:
            resp, content = http.request(url, method="GET", headers=h)
        except:  # pragma: no cover
            print("Unable to DescribeFeatureType from url %s" % url)
            self.capabilities_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.capabilities_cache[url] = None
            return []

        try:
            describe = parseString(content)
        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))

        attributes = []
        for element in describe.getElementsByTagName("element"):
            if element.getAttribute("type") not in [
                    "gml:PointPropertyType", "gml:LineStringPropertyType",
                    "gml:PolygonPropertyType"
            ]:
                attributes.append(element.getAttribute("name"))
Esempio n. 19
0
    def _fts(self):
        url = add_url_params(
            self.request.route_url("fulltextsearch"), {"query": self.settings["fulltextsearch"], "limit": "1"}
        )
        url, headers = build_url("Check the fulltextsearch", url, self.request)

        h = Http()
        resp, content = h.request(url, headers=headers)

        if resp.status != httplib.OK:
            self.set_status(resp.status, resp.reason)
            return content

        result = loads(content)

        if len(result["features"]) == 0:
            self.set_status(httplib.BAD_REQUEST, httplib.responses[httplib.BAD_REQUEST])
            return "No result"

        return "OK"
Esempio n. 20
0
    def _wms_getcap_cached(self, url):
        errors = set()
        wms = None

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

        log.info("Get WMS GetCapabilities for url: %s" % url)

        # forward request to target (without Host Header)
        http = httplib2.Http()
        h = dict(self.request.headers)
        if urlsplit(url).hostname != "localhost":  # pragma: no cover
            h.pop("Host")
        try:
            resp, content = http.request(url, method="GET", headers=h)
        except:  # pragma: no cover
            errors.add("Unable to GetCapabilities from url %s" % url)
            return None, errors

        if resp.status < 200 or resp.status >= 300:  # pragma: no cover
            errors.add(
                "GetCapabilities from url %s return the error: %i %s" %
                (url, resp.status, resp.reason)
            )
            return None, errors

        try:
            wms = WebMapService(None, xml=content)
        except AttributeError:
            error = _(
                "WARNING! an error occured while trying to "
                "read the mapfile and recover the themes."
            )
            error = "%s\nurl: %s\nxml:\n%s" % (error, url, content)
            errors.add(error)
            log.exception(error)
        return wms, errors
Esempio n. 21
0
    def _fts(self):
        url = add_url_params(self.request.route_url("fulltextsearch"), {
            "query": self.settings["fulltextsearch"],
            "limit": "1",
        })
        url, headers = build_url("Check the fulltextsearch", url, self.request)

        h = Http()
        resp, content = h.request(url, headers=headers)

        if resp.status != httplib.OK:
            self.set_status(resp.status, resp.reason)
            return content

        result = loads(content)

        if len(result["features"]) == 0:
            self.set_status(httplib.BAD_REQUEST,
                            httplib.responses[httplib.BAD_REQUEST])
            return "No result"

        return "OK"
Esempio n. 22
0
    def themes_errors(self):
        from c2cgeoportal.models import DBSession, Interface

        settings = self.settings.get("themes", {})

        _url = self.request.route_url("themes")
        h = Http()
        default_params = settings.get("default", {}).get("params", {})
        for interface, in DBSession.query(Interface.name).all():
            params = {}
            params.update(default_params)
            params.update(settings.get(interface, {}).get("params", {}))
            params["interface"] = interface
            interface_url = add_url_params(_url, params)

            log.info("Checker for theme: %s" % interface_url)
            interface_url = interface_url.replace(
                self.request.environ.get("SERVER_NAME"),
                "localhost"
            )
            headers = {"host": self.request.environ.get("HTTP_HOST")}

            resp, content = h.request(interface_url, headers=headers)

            if resp.status != httplib.OK:
                self.set_status(resp.status, resp.reason)
                return self.make_response(content)

            result = loads(content)

            if len(result["errors"]) != 0:
                self.set_status(500, "Theme with error")

                return self.make_response("Theme with error for interface '%s'\n%s" % (
                    Interface.name,
                    "\n".join(result["errors"])
                ))

        return self.make_response("OK")
Esempio n. 23
0
    def _fts(self):
        _url = add_url_params(self.request.route_url("fulltextsearch"), {
            "query": self.settings["fulltextsearch"],
            "limit": "1",
        })
        h = Http()

        log.info("Checker for fulltextsearch: %s" % _url)
        _url = _url.replace(self.request.environ.get("SERVER_NAME"), "localhost")
        headers = {"host": self.request.environ.get("HTTP_HOST")}

        resp, content = h.request(_url, headers=headers)

        if resp.status != httplib.OK:
            self.set_status(resp.status, resp.reason)
            return content

        result = loads(content)

        if len(result["features"]) == 0:
            self.set_status(httplib.BAD_REQUEST, httplib.responses[httplib.BAD_REQUEST])
            return "No result"

        return "OK"
Esempio n. 24
0
 def test_add_url_params_noparam(self):
     self.assertEqual(add_url_params(
         "http://example.com/", {}
     ), "http://example.com/")
Esempio n. 25
0
 def test_add_url_params(self):
     from c2cgeoportal.lib import add_url_params
     params = {"Name": "Bob", "Age": 18, "Nationality": u"Việt Nam"}
     result = add_url_params("http://test/", params)
     self.assertEqual(result, "http://test/?Nationality=Vi%E1%BB%87t+Nam&Age=18&Name=Bob")
Esempio n. 26
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
Esempio n. 27
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
Esempio n. 28
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