Ejemplo n.º 1
0
    def _set_defaults(self):
        """Define a set of predefined keys"""
        self["lang"] = self.optionals.get("lang", settings.DEFAULT_LANG)

        self._set_if_optional(
            "context_name",
            self.optionals.get("context_name", "invalid_context"))
        self._set_if_optional(
            "class_name", self.optionals.get("class_name", "invalid_class"))
        self._set_if_optional(
            "instance_id", self.optionals.get("instance_id",
                                              "invalid_instance"))

        self["expand_uri"] = self.optionals.get("expand_uri",
                                                settings.DEFAULT_URI_EXPANSION)

        # if the context name is defined, the graph_uri should follow it by default, but it can be overriden
        if "context_name" in self:
            self["graph_uri"] = safe_slug_to_prefix(
                self.optionals["context_name"])

        self._set_if_optional("class_prefix",
                              self.optionals.get("graph_uri", ''))

        class_uri = self.optionals.get("class_uri")
        if class_uri is not None:
            self._set_if_optional("instance_prefix", class_uri + "/")
Ejemplo n.º 2
0
def convert_index_name_to_graph_uri(index_name):
    """
    Convert @index_name to the related graph uri, provided:
        @index_name: string representing ElasticSearch index name
    """
    graph_name = index_name.split("semantica.")[-1]
    graph_uri = safe_slug_to_prefix(graph_name)
    return graph_uri
Ejemplo n.º 3
0
def convert_index_name_to_graph_uri(index_name):
    """
    Convert @index_name to the related graph uri, provided:
        @index_name: string representing ElasticSearch index name
    """
    graph_name = index_name.split("semantica.")[-1]
    graph_uri = safe_slug_to_prefix(graph_name)
    return graph_uri
Ejemplo n.º 4
0
    def _set_defaults(self):
        """Define a set of predefined keys"""
        self["lang"] = self.optionals.get("lang", settings.DEFAULT_LANG)

        self._set_if_optional("context_name", self.optionals.get("context_name", "invalid_context"))
        self._set_if_optional("class_name", self.optionals.get("class_name", "invalid_class"))
        self._set_if_optional("instance_id", self.optionals.get("instance_id", "invalid_instance"))

        self["expand_uri"] = self.optionals.get("expand_uri", settings.DEFAULT_URI_EXPANSION)

        # if the context name is defined, the graph_uri should follow it by default, but it can be overriden
        if "context_name" in self:
            self["graph_uri"] = safe_slug_to_prefix(self.optionals["context_name"])

        self._set_if_optional("class_prefix", self.optionals.get("graph_uri", ''))

        class_uri = self.optionals.get("class_uri")
        if class_uri is not None:
            self._set_if_optional("instance_prefix", class_uri + "/")
Ejemplo n.º 5
0
    def __setitem__(self, key, value):
        """Process collateral effects in params that are related.
        Changes in *_prefix should reflect in *_uri.
        """
        def _key_is_undefined(key):
            try:
                value = dict.__getitem__(self, key)
                return not value or value == '_'
            except KeyError:
                return True

        if key == 'graph_uri':
            graph_uri_value = safe_slug_to_prefix(value)
            dict.__setitem__(self, key, graph_uri_value)
            with safe_split():
                if graph_uri_value and _key_is_undefined('context_name'):
                    # FIXME: the code below should disappear after #10602 - Normalização no tratamento de parâmetros no Brainiak
                    context_name_value = graph_uri_value.split("/")[-2]
                    dict.__setitem__(self, 'context_name', context_name_value)

        elif key == 'class_uri':
            class_uri_value = expand_uri(value)
            dict.__setitem__(self, key, class_uri_value)
            with safe_split():
                # FIXME: the code below should disappear after #10602 - Normalização no tratamento de parâmetros no Brainiak
                if class_uri_value and _key_is_undefined('class_name'):
                    class_name_value = class_uri_value.split("/")[-1]
                    dict.__setitem__(self, 'class_name', class_name_value)

                # FIXME: the code below should disappear after #10602 - Normalização no tratamento de parâmetros no Brainiak
                # class_prefix must be set when class_uri is defined, because it is used by json-schema logic.
                if class_uri_value:
                    class_prefix_value = "/".join(
                        class_uri_value.split("/")[:-1]) + "/"
                    dict.__setitem__(self, 'class_prefix', class_prefix_value)

        elif key == "context_name":
            dict.__setitem__(self, key, value)
            uri = safe_slug_to_prefix(value)
            dict.__setitem__(self, "graph_uri", uri)
            dict.__setitem__(self, "class_prefix", uri)

        elif key == "class_name":
            dict.__setitem__(self, key, value)
            class_prefix = self["class_prefix"]
            if not class_prefix.endswith('/'):
                class_prefix += "/"
            dict.__setitem__(self, "class_uri",
                             "{0}{1}".format(class_prefix, self["class_name"]))

        elif key == "instance_id":
            dict.__setitem__(self, key, value)
            dict.__setitem__(
                self, "instance_uri",
                u"{0}{1}/{2}".format(self["class_prefix"], self["class_name"],
                                     self["instance_id"]))
            dict.__setitem__(self, "instance_prefix",
                             extract_prefix(self["instance_uri"]))

        elif key == "class_prefix":
            dict.__setitem__(self, key, safe_slug_to_prefix(value))
            dict.__setitem__(
                self, "class_uri", u"{0}{1}".format(self["class_prefix"],
                                                    self["class_name"]))

        elif key == "instance_prefix":
            dict.__setitem__(self, key, safe_slug_to_prefix(value))
            dict.__setitem__(
                self, "instance_uri", u"{0}{1}".format(self["instance_prefix"],
                                                       self["instance_id"]))

        elif key == "instance_uri":
            dict.__setitem__(self, key, value)
            dict.__setitem__(self, "instance_prefix", extract_prefix(value))

        else:
            dict.__setitem__(self, key, value)
Ejemplo n.º 6
0
 def test_safe_slug_to_prefix_when_prefix_is_not_defined(self):
     self.assertEqual(safe_slug_to_prefix('bigoletinha'), 'bigoletinha')
Ejemplo n.º 7
0
 def test_safe_slug_to_prefix_when_prefix_is_defined(self):
     self.assertEqual(safe_slug_to_prefix('rdf'),
                      'http://www.w3.org/1999/02/22-rdf-syntax-ns#')
Ejemplo n.º 8
0
    def __setitem__(self, key, value):
        """Process collateral effects in params that are related.
        Changes in *_prefix should reflect in *_uri.
        """
        def _key_is_undefined(key):
            try:
                value = dict.__getitem__(self, key)
                return not value or value == '_'
            except KeyError:
                return True

        if key == 'graph_uri':
            graph_uri_value = safe_slug_to_prefix(value)
            dict.__setitem__(self, key, graph_uri_value)
            with safe_split():
                if graph_uri_value and _key_is_undefined('context_name'):
                    # FIXME: the code below should disappear after #10602 - Normalização no tratamento de parâmetros no Brainiak
                    context_name_value = _MAP_PREFIX_TO_SLUG.get(graph_uri_value, '_')
                    dict.__setitem__(self, 'context_name', context_name_value)

        elif key == 'class_uri':
            class_uri_value = expand_uri(value)
            dict.__setitem__(self, key, class_uri_value)
            with safe_split():
                # FIXME: the code below should disappear after #10602 - Normalização no tratamento de parâmetros no Brainiak
                if class_uri_value and _key_is_undefined('class_name'):
                    class_name_value = class_uri_value.split("/")[-1]
                    dict.__setitem__(self, 'class_name', class_name_value)

                # FIXME: the code below should disappear after #10602 - Normalização no tratamento de parâmetros no Brainiak
                # class_prefix must be set when class_uri is defined, because it is used by json-schema logic.
                if class_uri_value:
                    class_prefix_value = "/".join(class_uri_value.split("/")[:-1]) + "/"
                    dict.__setitem__(self, 'class_prefix', class_prefix_value)

        elif key == "context_name":
            dict.__setitem__(self, key, value)
            uri = safe_slug_to_prefix(value)
            dict.__setitem__(self, "graph_uri", uri)
            dict.__setitem__(self, "class_prefix", uri)

        elif key == "class_name":
            dict.__setitem__(self, key, value)
            class_prefix = self["class_prefix"]
            if not class_prefix.endswith('/'):
                class_prefix += "/"
            dict.__setitem__(self, "class_uri", "{0}{1}".format(class_prefix, self["class_name"]))

        elif key == "instance_id":
            dict.__setitem__(self, key, value)
            dict.__setitem__(self, "instance_uri", u"{0}{1}/{2}".format(self["class_prefix"], self["class_name"], self["instance_id"]))
            dict.__setitem__(self, "instance_prefix", extract_prefix(self["instance_uri"]))

        elif key == "class_prefix":
            dict.__setitem__(self, key, safe_slug_to_prefix(value))
            dict.__setitem__(self, "class_uri", u"{0}{1}".format(self["class_prefix"], self["class_name"]))

        elif key == "instance_prefix":
            dict.__setitem__(self, key, safe_slug_to_prefix(value))
            dict.__setitem__(self, "instance_uri", u"{0}{1}".format(self["instance_prefix"], self["instance_id"]))

        elif key == "instance_uri":
            dict.__setitem__(self, key, value)
            dict.__setitem__(self, "instance_prefix", extract_prefix(value))

        else:
            dict.__setitem__(self, key, value)