Exemplo n.º 1
0
    def create_style(self, name, data, overwrite=False, workspace=None):
        style = self.get_style(name, workspace)
        if not overwrite and style is not None:
            raise ConflictingDataError("There is already a style named %s" %
                                       name)

        if not overwrite or style is None:
            headers = {
                "Content-type": "application/xml",
                "Accept": "application/xml"
            }
            xml = "<style><name>{0}</name><filename>{0}.sld</filename></style>".format(
                name)
            style = Style(self, name, workspace)
            headers, response = self.http.request(style.create_href, "POST",
                                                  xml, headers)
            if headers.status < 200 or headers.status > 299:
                raise UploadError(response)

        headers = {
            "Content-type": "application/vnd.ogc.sld+xml",
            "Accept": "application/xml"
        }

        headers, response = self.http.request(style.body_href(), "PUT", data,
                                              headers)
        if headers.status < 200 or headers.status > 299:
            raise UploadError(response)

        self._cache.pop(style.href, None)
        self._cache.pop(style.body_href(), None)
Exemplo n.º 2
0
    def create_style(self, name, data, overwrite = False, workspace=None):
        style = self.get_style(name, workspace)
        if not overwrite and style is not None:
            raise ConflictingDataError("There is already a style named %s" % name)

        if not overwrite or style is None:
            headers = {
                "Content-type": "application/xml",
                "Accept": "application/xml"
            }
            xml = "<style><name>{0}</name><filename>{0}.sld</filename></style>".format(name)
            style = Style(self, name, workspace)
            headers, response = self.http.request(style.create_href, "POST", xml, headers)
            if headers.status < 200 or headers.status > 299: raise UploadError(response)

        headers = {
            "Content-type": "application/vnd.ogc.sld+xml",
            "Accept": "application/xml"
        }

        headers, response = self.http.request(style.body_href(), "PUT", data, headers)
        if headers.status < 200 or headers.status > 299: raise UploadError(response)

        self._cache.pop(style.href, None)
        self._cache.pop(style.body_href(), None)
Exemplo n.º 3
0
    def get_styles(self, names=None, workspaces=None):
        '''
        names and workspaces can be provided as a comma delimited strings or as arrays, and are used for filtering.
        If no workspaces are provided, will return all styles in the catalog (global and workspace specific).
        Will always return an array.
        '''
        all_styles = []
        if not workspaces:
            # Add global styles
            url = "{}/styles.xml".format(self.service_url)
            styles = self.get_xml(url)
            all_styles.extend([
                Style(self,
                      s.find('name').text) for s in styles.findall("style")
            ])
            workspaces = []
        elif isinstance(workspaces, string_types):
            workspaces = [
                s.strip() for s in workspaces.split(',') if s.strip()
            ]
        elif isinstance(workspaces, Workspace):
            workspaces = [workspaces]

        if not workspaces:
            workspaces = self.get_workspaces()

        for ws in workspaces:
            if ws:
                url = "{}/workspaces/{}/styles.xml".format(
                    self.service_url, _name(ws))
            else:
                url = "{}/styles.xml".format(self.service_url)
            try:
                styles = self.get_xml(url)
            except FailedRequestError as e:
                if "no such workspace" in str(e).lower():
                    continue
                elif "workspace {} not found".format(
                        _name(ws)) in str(e).lower():
                    continue
                else:
                    raise FailedRequestError(
                        "Failed to get styles: {}".format(e))

            all_styles.extend([
                Style(self,
                      s.find("name").text, _name(ws))
                for s in styles.findall("style")
            ])

        if names is None:
            names = []
        elif isinstance(names, string_types):
            names = [s.strip() for s in names.split(',') if s.strip()]

        if all_styles and names:
            return ([style for style in all_styles if style.name in names])

        return all_styles
Exemplo n.º 4
0
 def get_styles(self):
     styles_url = url(self.service_url, ["styles.xml"])
     description = self.get_xml(styles_url)
     return [
         Style(self,
               s.find('name').text) for s in description.findall("style")
     ]
Exemplo n.º 5
0
 def get_style(self, name):
     try:
         style_url = url(self.service_url, ["styles", name + ".xml"])
         dom = self.get_xml(style_url)
         return Style(self, dom.find("name").text)
     except FailedRequestError:
         return None
Exemplo n.º 6
0
 def _get_alternate_styles(self):
     if "alternate_styles" in self.dirty:
         return self.dirty["alternate_styles"]
     if self.dom is None:
         self.fetch()
     styles = self.dom.findall("styles/style/name")
     return [Style(self.catalog, s.text) for s in styles]
Exemplo n.º 7
0
 def create_style(self, name, data, overwrite=False, workspace=None,
                  style_format="sld10", raw=False):
     style = self.get_style(name, workspace)
     if not overwrite and style is not None:
         msg = "There is already a style named {}".format(name)
         raise ConflictingDataError(msg)
     if not overwrite or style is None:
         headers = {
             "Content-type": "text/xml",
             #"Accept": "application/xml"
         }
         xml = "<style><name>{0}</name><filename>{0}.sld"\
               + "</filename></style>"
         xml = xml.format(name)
         style = Style(self, name, workspace, style_format)
         r = self.session.post(style.create_href, data=xml, headers=headers)
         if r.status_code < 200 or r.status_code > 299:
             raise UploadError(r.text)
     headers = {
         "Content-type": style.content_type,
         "Accept": "application/xml"
     }
     body_href = style.body_href
     if raw:
         body_href += "?raw=true"
     r = self.session.put(body_href, data=data, headers=headers)
     if r.status_code < 200 or r.status_code > 299:
         raise UploadError(r.text)
     self._cache.pop(style.href, None)
     self._cache.pop(style.body_href, None)
Exemplo n.º 8
0
    def get_style(self, name, workspace=None):
        """Find a Style in the catalog if one exists that matches the given name.
        If name is fully qualified in the form of `workspace:name` the workspace
        may be ommitted.

        :param name: name of the style to find
        :param workspace: optional workspace to search in
        """
        if ':' in name:
            workspace, name = name.split(':', 1)
        try:
            style = Style(self, name, _name(workspace))
            style.fetch()
        except FailedRequestError:
            style = None
        return style
Exemplo n.º 9
0
 def get_styles(self, workspace=None):
     styles_xml = "styles.xml"
     if workspace is not None:
         styles_xml = "workspaces/{0}/styles.xml".format(_name(workspace))
     styles_url = urljoin(self.service_url, styles_xml)
     description = self.get_xml(styles_url)
     return [Style(self, s.find("name").text) for s in description.findall("style")]
Exemplo n.º 10
0
    def get_style(self, name, workspace=None):
        '''Find a Style in the catalog if one exists that matches the given name.
        If name is fully qualified in the form of `workspace:name` the workspace
        may be ommitted.

        :param name: name of the style to find
        :param workspace: optional workspace to search in
        '''
        style = None
        if ':' in name:
            workspace, name = name.split(':', 1)
        try:
            style = Style(self, name, _name(workspace))
            style.fetch()
        except FailedRequestError:
            style = None
        return style
Exemplo n.º 11
0
 def get_style_by_url(self, style_workspace_url):
     try:
         dom = self.get_xml(style_workspace_url)
     except FailedRequestError:
         return None
     rest_parts = style_workspace_url.replace(self.service_url, '').split('/')
     # check for /workspaces/<ws>/styles/<stylename>
     workspace = None
     if 'workspaces' in rest_parts:
         workspace = rest_parts[rest_parts.index('workspaces') + 1]
     return Style(self, dom.find("name").text, workspace)
Exemplo n.º 12
0
 def get_styles(self, workspace=None):
     if workspace == None:
         styles_xml = "styles.xml"
     else:
         styles_xml = "workspaces/" + _name(workspace) + "/styles.xml"
     styles_url = url(self.service_url, [styles_xml])
     description = self.get_xml(styles_url)
     return [
         Style(self,
               s.find('name').text) for s in description.findall("style")
     ]
Exemplo n.º 13
0
    def create_style(self,
                     name,
                     data,
                     overwrite=False,
                     workspace=None,
                     style_format="sld10",
                     raw=False):
        styles = self.get_styles(names=name, workspaces=workspace)
        if len(styles) > 0:
            style = styles[0]
        else:
            style = None

        if not overwrite and style is not None:
            raise ConflictingDataError("There is already a style named %s" %
                                       name)

        if style is None:
            headers = {
                "Content-type": "application/xml",
                "Accept": "application/xml"
            }
            xml = "<style><name>{0}</name><filename>{0}.sld</filename></style>".format(
                name)
            style = Style(self, name, workspace, style_format)

            resp = self.http_request(style.create_href,
                                     method='post',
                                     data=xml,
                                     headers=headers)
            if resp.status_code not in (200, 201, 202):
                FailedRequestError('Failed to create style {} : {}, {}'.format(
                    name, resp.status_code, resp.text))

        headers = {
            "Content-type": style.content_type,
            "Accept": "application/xml"
        }

        body_href = style.body_href
        if raw:
            body_href += "?raw=true"

        resp = self.http_request(body_href,
                                 method='put',
                                 data=data,
                                 headers=headers)
        if resp.status_code not in (200, 201, 202):
            FailedRequestError('Failed to create style {} : {}, {}'.format(
                name, resp.status_code, resp.text))

        self._cache.pop(style.href, None)
        self._cache.pop(style.body_href, None)
Exemplo n.º 14
0
    def get_style_by_url(self, style_workspace_url):
        try:
            dom = self.get_xml(style_workspace_url)
            rest_path = style_workspace_url[
                re.search(self.service_url, style_workspace_url).end():]
            rest_segments = re.split("\/", rest_path)
            for i, s in enumerate(rest_segments):
                if s == "workspaces": workspace_name = rest_segments[i + 1]
            #create an instance of Workspace_Style if a workspace is contained in the
            # REST API style path (should always be the case /workspaces/<ws>/styles/<stylename>:
            if isinstance(workspace_name, basestring):
                workspace = self.get_workspace(workspace_name)
                return Workspace_Style(self, workspace, dom.find("name").text)
            else:
                return Style(self, dom.find("name").text)

        except FailedRequestError:
            return None
Exemplo n.º 15
0
    def create_style(self,
                     name,
                     data,
                     overwrite=False,
                     workspace=None,
                     style_format="sld10",
                     raw=False):
        style = self.get_style(name, workspace)
        if not overwrite and style is not None:
            raise ConflictingDataError("There is already a style named %s" %
                                       name)

        # No existing style with given name, so create new one with POST (overwrite does not apply)
        if style is None:
            style = Style(self, name, workspace, style_format)
            # Create style using two-step approach:
            # 1. Create Style in Catalog
            headers = {"Content-type": "text/xml", "Accept": "application/xml"}
            xml = "<style><name>{0}</name><filename>{1}</filename></style>".format(
                name, name + '.sld')
            create_href = style.create_href
            response = self.request(method='post',
                                    url=create_href,
                                    headers=headers,
                                    data=xml)

            if response.status_code < 200 or response.status_code > 299:
                raise UploadError('{0} - "{1}"'.format(response.status_code,
                                                       response.text))

            # 2. Upload file for style
            headers = {
                "Content-type": style.content_type,
                "Accept": "application/xml"
            }

            body_href = style.body_href
            response = self.request(method='put',
                                    url=body_href,
                                    headers=headers,
                                    data=data)
            if response.status_code < 200 or response.status_code > 299:
                raise UploadError('{0} - "{1}"'.format(response.status_code,
                                                       response.text))

        # Style with given name already exists, so update if overwrite is True
        elif style is not None and overwrite:
            headers = {
                "Content-type": style.content_type,
                "Accept": "application/xml"
            }

            body_href = style.body_href
            if raw:
                body_href += "?raw=true"
            response = self.request(method='put',
                                    url=body_href,
                                    headers=headers,
                                    data=data)
            if response.status_code < 200 or response.status_code > 299:
                raise UploadError('{0} - "{1}"'.format(response.status_code,
                                                       response.text))

            self._cache.pop(style.href, None)
            self._cache.pop(style.body_href, None)
        # Style with given name already exists, but overwrite not allowed, so raise exception
        else:
            raise ConflictingDataError(
                'Style already exists with name: "{0}"'.format(style.fqn))