def add_vertex()

in src/graph_notebook/network/gremlin/GremlinNetwork.py [0:0]


    def add_vertex(self, v):
        """
        Adds a vertex to the network. If v is of :type Vertex, we will gather its id and label.
        If v comes from a valueMap, it will be a dict, with the keys T.label and T.ID being present in
        the dict for gathering the label and id, respectively.

        :param v: The vertex taken from a path traversal object.
        """
        node_id = ''
        if type(v) is Vertex:
            node_id = v.id
            title = v.label
            label_full = ''
            tooltip_display_is_set = True
            using_custom_tooltip = False
            if self.tooltip_property and self.tooltip_property != self.display_property:
                tooltip_display_is_set = False
                using_custom_tooltip = True
            vertex_dict = v.__dict__
            if not isinstance(self.group_by_property, dict):  # Handle string format group_by
                if str(self.group_by_property) in [T_LABEL, 'label']:  # this handles if it's just a string
                    # This sets the group key to the label if either "label" is passed in or
                    # T.label is set in order to handle the default case of grouping by label
                    # when no explicit key is specified
                    group = v.label
                elif str(self.group_by_property) in [T_ID, 'id']:
                    group = v.id
                else:
                    group = DEFAULT_GRP
            else:  # handle dict format group_by
                try:
                    if str(v.label) in self.group_by_property:
                        if self.group_by_property[str(v.label)] in [T_LABEL, 'label']:
                            group = v.label
                        elif self.group_by_property[str(v.label)] in [T_ID, 'id']:
                            group = v.id
                        else:
                            group = vertex_dict[self.group_by_property[str(v.label)]]
                    else:
                        group = DEFAULT_GRP
                except KeyError:
                    group = DEFAULT_GRP

            label = title if len(title) <= self.label_max_length else title[:self.label_max_length - 3] + '...'

            if self.display_property:
                label_property_raw_value = self.get_explicit_vertex_property_value(node_id, title,
                                                                                    self.display_property)
                if label_property_raw_value:
                    label_full, label = self.strip_and_truncate_label_and_title(label_property_raw_value,
                                                                                self.label_max_length)
                    if not using_custom_tooltip:
                        title = label_full

            if using_custom_tooltip:
                tooltip_property_raw_value = self.get_explicit_vertex_property_value(node_id, title,
                                                                                      self.tooltip_property)
                if tooltip_property_raw_value:
                    title, label_plc = self.strip_and_truncate_label_and_title(tooltip_property_raw_value,
                                                                               self.label_max_length)
                    tooltip_display_is_set = True

            if not tooltip_display_is_set and label_full:
                title = label_full

            data = {'label': str(label).strip("[]'"), 'title': title, 'group': group,
                    'properties': {'id': node_id, 'label': label_full}}
        elif type(v) is dict:
            properties = {}
            title = ''
            label = ''
            label_full = ''
            group = ''
            display_is_set = False
            using_custom_tooltip = False
            tooltip_display_is_set = True
            group_is_set = False
            if self.tooltip_property and self.tooltip_property != self.display_property:
                using_custom_tooltip = True
                tooltip_display_is_set = False
            # Before looping though properties, we first search for T.label in vertex dict, then set title = T.label
            # Otherwise, we will hit KeyError if we don't iterate through T.label first to set the title
            # Since it is needed for checking for the vertex label's desired grouping behavior in group_by_property
            if T.label in v.keys():
                title_plc = str(v[T.label])
                title, label = self.strip_and_truncate_label_and_title(title_plc, self.label_max_length)
            else:
                title_plc = ''
                group = DEFAULT_GRP
            for k in v:
                if str(k) == T_ID:
                    node_id = str(v[k])
                properties[k] = str(v[k]) if isinstance(v[k], dict) else v[k]
                if not group_is_set:
                    if isinstance(self.group_by_property, dict):
                        try:
                            if str(k) == self.group_by_property[title_plc]:
                                group = str(v[k])
                                group_is_set = True
                        except KeyError:
                            pass
                    elif str(k) == self.group_by_property:
                        group = str(v[k])
                        group_is_set = True
                if not display_is_set:
                    label_property_raw_value = self.get_dict_element_property_value(v, k, title_plc,
                                                                                    self.display_property)
                    if label_property_raw_value:
                        label_full, label = self.strip_and_truncate_label_and_title(label_property_raw_value,
                                                                                    self.label_max_length)
                        if not using_custom_tooltip:
                            title = label_full
                        display_is_set = True
                if not tooltip_display_is_set:
                    tooltip_property_raw_value = self.get_dict_element_property_value(v, k, title_plc,
                                                                                      self.tooltip_property)
                    if tooltip_property_raw_value:
                        title, label_plc = self.strip_and_truncate_label_and_title(tooltip_property_raw_value,
                                                                                   self.label_max_length)
                        tooltip_display_is_set = True

            if not tooltip_display_is_set and label_full:
                title = label_full

            # handle when there is no id in a node. In this case, we will generate one which
            # is consistently regenerated so that duplicate dicts will be reduced to the same vertex.
            if node_id == '':
                node_id = f'{generate_id_from_dict(v)}'

            # similarly, if there is no label, we must generate one. This will be a concatenation
            # of all values in the dict
            if title == '':
                for key in v:
                    title += str(v[key])
            if label == '':
                label = title if len(title) <= self.label_max_length else title[:self.label_max_length - 3] + '...'

            data = {'properties': properties, 'label': label, 'title': title, 'group': group}
        else:
            node_id = str(v)
            title = str(v)
            label = title if len(title) <= self.label_max_length else title[:self.label_max_length - 3] + '...'
            data = {'title': title, 'label': label, 'group': DEFAULT_GRP}

        if self.ignore_groups:
            data['group'] = DEFAULT_GRP
        self.add_node(node_id, data)