<commit_before># -*- coding: utf-8 -*-
"""
/***************************************************************************
 Svir
                                 A QGIS plugin
 OpenQuake Social Vulnerability and Integrated Risk
                              -------------------
        begin                : 2013-10-24
        copyright            : (C) 2013 by GEM Foundation
        email                : <EMAIL>
 ***************************************************************************/

# Copyright (c) 2010-2013, GEM Foundation.
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake.  If not, see <http://www.gnu.org/licenses/>.
"""
import uuid
from types import NoneType
from PyQt4.QtCore import QVariant, QPyNullVariant
from qgis.core import (QgsMapLayer,
                       QGis,
                       QgsVectorLayer,
                       QgsMapLayerRegistry,
                       QgsField)
from utils import LayerEditingManager

from transformation_algs import (TRANSFORMATION_ALGS, transform)

from globals import DEBUG, DOUBLE_FIELD_TYPE_NAME


class ProcessLayer():

    def __init__(self, layer):
        self.layer = layer

    def add_attributes(self, attribute_list):
        """
        Add attributes to the layer

        :param attribute_list: list of QgsField to add to the layer
        :type attribute_list: list of QgsField

        :return: dict having as keys the elements of the list of attributes
        passed as input argument, and as values the actual names of the
        assigned attributes
        """
        with LayerEditingManager(self.layer, 'Add attributes', DEBUG):
            # add attributes
            layer_pr = self.layer.dataProvider()
            proposed_attribute_dict = {}
            proposed_attribute_list = []
            for input_attribute in attribute_list:
                input_attribute_name = input_attribute.name()
                proposed_attribute_name = input_attribute_name
                i = 1
                while True:
                    current_attribute_names = \
                        [attribute.name() for attribute in layer_pr.fields()]
                    if proposed_attribute_name in current_attribute_names:
                        # If the attribute is already assigned, change the
                        # proposed_attribute_name
                        proposed_attribute_name = '%s_%d' % (
                            input_attribute_name, i)
                        i += 1
                    else:
                        # If the attribute name is not already assigned,
                        # add it to the proposed_attribute_dict
                        proposed_attribute_dict[input_attribute_name] = \
                            proposed_attribute_name
                        input_attribute.setName(proposed_attribute_name)
                        proposed_attribute_list.append(input_attribute)
                        break
            added_ok = layer_pr.addAttributes(proposed_attribute_list)
            if not added_ok:
                raise AttributeError(
                    'Unable to add attributes %s' % proposed_attribute_list)
        return proposed_attribute_dict
	
    def delete_attributes(self, attribute_list):
        """
        Delete attributes from the layer

        :param attribute_list: list of id to remove from the layer
        :type attribute_list: list of int

        :return: true in case of success and false in case of failure
        """
        with LayerEditingManager(self.layer, 'Remove attributes', DEBUG):
            # remove attributes
            layer_pr = self.layer.dataProvider()
            print "REMOVING %s" % attribute_list
            #TODO fix this
            #return layer_pr.deleteAttributes(attribute_list)

    def transform_attribute(
            self, input_attr_name, algorithm_name, variant="",
            inverse=False, new_attr_name=None):
        """
        Use one of the available transformation algorithms to transform an
        attribute of the layer, and add a new attribute with the
        transformed data
        """
        # get the id of the attribute named input_attr_name
        input_attr_id = self.find_attribute_id(input_attr_name)

        # build the name of the output transformed attribute
        # WARNING! Shape files support max 10 chars for attribute names
        if not new_attr_name:
            new_attr_name = algorithm_name[:10]
        else:
            new_attr_name = new_attr_name[:10]
        field = QgsField(new_attr_name, QVariant.Double)
        field.setTypeName(DOUBLE_FIELD_TYPE_NAME)
        attr_names_dict = self.add_attributes([field])

        # get the name actually assigned to the new attribute
        actual_new_attr_name = attr_names_dict[new_attr_name]
        # get the id of the new attribute
        new_attr_id = self.find_attribute_id(actual_new_attr_name)

        # a dict will contain all the values for the chosen input attribute,
        # keeping as key, for each value, the id of the corresponding feature
        initial_dict = dict()
        for feat in self.layer.getFeatures():
            initial_dict[feat.id()] = feat[input_attr_id]

        # get the transformation algorithm from the register
        algorithm = TRANSFORMATION_ALGS[algorithm_name]

        # transform the values in the dictionary with the chosen algorithm
        try:
            transformed_dict = transform(
                initial_dict, algorithm, variant, inverse)
        except ValueError:
            raise
        except NotImplementedError:
            raise

        with LayerEditingManager(
                self.layer, 'Write transformed values', DEBUG):
            for feat in self.layer.getFeatures():
                feat_id = feat.id()
                value = transformed_dict[feat_id]
                if type(value) not in (QPyNullVariant, NoneType):
                    value = float(value)
                self.layer.changeAttributeValue(feat_id, new_attr_id, value)
        return actual_new_attr_name

    def find_attribute_id(self, attribute_name):
        """
        Get the id of the attribute called attribute_name
        @param attribute_name: name of the attribute
        @return: id of the attribute, or raise AttributeError
        exception if not found
        """
        attribute_id = None
        pr = self.layer.dataProvider()
        for field_id, field in enumerate(pr.fields()):
            if field.name() == attribute_name:
                attribute_id = field_id
                return attribute_id
        # In case the attribute has not been found, raise exception
        raise AttributeError('Attribute name %s not found' % attribute_name)

    def duplicate_in_memory(self, new_name='', add_to_registry=False):
        """
        Return a memory copy of the layer

        :param new_name: The name of the copied layer.
        :type new_name: str

        :param add_to_registry: if True, the new layer will be added to
        the QgsMapRegistry
        :type: bool

        :returns: An in-memory copy of a layer.
        :rtype: QgsMapLayer

        """
        if new_name is '':
            new_name = self.layer.name() + ' TMP'

        if self.layer.type() == QgsMapLayer.VectorLayer:
            v_type = self.layer.wkbType()
            if v_type == QGis.WKBPoint:
                type_str = "point"
            elif v_type == QGis.WKBLineString:
                type_str = "linestring"
            elif v_type == QGis.WKBPolygon:
                type_str = "polygon"
            elif v_type == QGis.WKBMultiPoint:
                type_str = "multipoint"
            elif v_type == QGis.WKBMultiLineString:
                type_str = "multilinestring"
            elif v_type == QGis.WKBMultiPolygon:
                type_str = "multipolygon"
            else:
                raise TypeError('Layer type %s can not be accepted' % v_type)
        else:
            raise RuntimeError('Layer is not a VectorLayer')

        crs = self.layer.crs().authid().lower()
        my_uuid = str(uuid.uuid4())
        uri = '%s?crs=%s&index=yes&uuid=%s' % (type_str, crs, my_uuid)
        mem_layer = QgsVectorLayer(uri, new_name, 'memory')
        with LayerEditingManager(mem_layer, 'Duplicating layer', DEBUG):
            mem_provider = mem_layer.dataProvider()

            provider = self.layer.dataProvider()
            v_fields = provider.fields()

            fields = []
            for i in v_fields:
                fields.append(i)

            mem_provider.addAttributes(fields)

            for ft in provider.getFeatures():
                mem_provider.addFeatures([ft])

        if add_to_registry:
            if mem_layer.isValid():
                QgsMapLayerRegistry.instance().addMapLayer(mem_layer)
            else:
                raise RuntimeError('Layer invalid')

        return mem_layer

    def is_type_in(self, type_list):
        """
        @param type_list: we want to check if the type of the layer is
        included in this list
        @return: True if the layer is a VectorLayer and its type is in the list
        """
        if self.layer.type() == QgsMapLayer.VectorLayer:
            v_type = self.layer.wkbType()
            if v_type == QGis.WKBPoint:
                type_str = "point"
            elif v_type == QGis.WKBLineString:
                type_str = "linestring"
            elif v_type == QGis.WKBPolygon:
                type_str = "polygon"
            elif v_type == QGis.WKBMultiPoint:
                type_str = "multipoint"
            elif v_type == QGis.WKBMultiLineString:
                type_str = "multilinestring"
            elif v_type == QGis.WKBMultiPolygon:
                type_str = "multipolygon"
            else:
                raise TypeError('Layer type %s can not be accepted' % v_type)
            return type_str in type_list
        else:
            return False
<commit_msg>print an allert for method not zet implemented<commit_after># -*- coding: utf-8 -*-
"""
/***************************************************************************
 Svir
                                 A QGIS plugin
 OpenQuake Social Vulnerability and Integrated Risk
                              -------------------
        begin                : 2013-10-24
        copyright            : (C) 2013 by GEM Foundation
        email                : <EMAIL>
 ***************************************************************************/

# Copyright (c) 2010-2013, GEM Foundation.
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake.  If not, see <http://www.gnu.org/licenses/>.
"""
import uuid
from types import NoneType
from PyQt4.QtCore import QVariant, QPyNullVariant
from qgis.core import (QgsMapLayer,
                       QGis,
                       QgsVectorLayer,
                       QgsMapLayerRegistry,
                       QgsField)
from utils import LayerEditingManager

from transformation_algs import (TRANSFORMATION_ALGS, transform)

from globals import DEBUG, DOUBLE_FIELD_TYPE_NAME


class ProcessLayer():

    def __init__(self, layer):
        self.layer = layer

    def add_attributes(self, attribute_list):
        """
        Add attributes to the layer

        :param attribute_list: list of QgsField to add to the layer
        :type attribute_list: list of QgsField

        :return: dict having as keys the elements of the list of attributes
        passed as input argument, and as values the actual names of the
        assigned attributes
        """
        with LayerEditingManager(self.layer, 'Add attributes', DEBUG):
            # add attributes
            layer_pr = self.layer.dataProvider()
            proposed_attribute_dict = {}
            proposed_attribute_list = []
            for input_attribute in attribute_list:
                input_attribute_name = input_attribute.name()
                proposed_attribute_name = input_attribute_name
                i = 1
                while True:
                    current_attribute_names = \
                        [attribute.name() for attribute in layer_pr.fields()]
                    if proposed_attribute_name in current_attribute_names:
                        # If the attribute is already assigned, change the
                        # proposed_attribute_name
                        proposed_attribute_name = '%s_%d' % (
                            input_attribute_name, i)
                        i += 1
                    else:
                        # If the attribute name is not already assigned,
                        # add it to the proposed_attribute_dict
                        proposed_attribute_dict[input_attribute_name] = \
                            proposed_attribute_name
                        input_attribute.setName(proposed_attribute_name)
                        proposed_attribute_list.append(input_attribute)
                        break
            added_ok = layer_pr.addAttributes(proposed_attribute_list)
            if not added_ok:
                raise AttributeError(
                    'Unable to add attributes %s' % proposed_attribute_list)
        return proposed_attribute_dict

    def delete_attributes(self, attribute_list):
        """
        Delete attributes from the layer

        :param attribute_list: list of id to remove from the layer
        :type attribute_list: list of int

        :return: true in case of success and false in case of failure
        """
        with LayerEditingManager(self.layer, 'Remove attributes', DEBUG):
            # remove attributes
            layer_pr = self.layer.dataProvider()
            print "REMOVING %s" % attribute_list
            #TODO fix this
            print "TODO fix ProcessLayer.delete_attributes()"
            #return layer_pr.deleteAttributes(attribute_list)

    def transform_attribute(
            self, input_attr_name, algorithm_name, variant="",
            inverse=False, new_attr_name=None):
        """
        Use one of the available transformation algorithms to transform an
        attribute of the layer, and add a new attribute with the
        transformed data
        """
        # get the id of the attribute named input_attr_name
        input_attr_id = self.find_attribute_id(input_attr_name)

        # build the name of the output transformed attribute
        # WARNING! Shape files support max 10 chars for attribute names
        if not new_attr_name:
            new_attr_name = algorithm_name[:10]
        else:
            new_attr_name = new_attr_name[:10]
        field = QgsField(new_attr_name, QVariant.Double)
        field.setTypeName(DOUBLE_FIELD_TYPE_NAME)
        attr_names_dict = self.add_attributes([field])

        # get the name actually assigned to the new attribute
        actual_new_attr_name = attr_names_dict[new_attr_name]
        # get the id of the new attribute
        new_attr_id = self.find_attribute_id(actual_new_attr_name)

        # a dict will contain all the values for the chosen input attribute,
        # keeping as key, for each value, the id of the corresponding feature
        initial_dict = dict()
        for feat in self.layer.getFeatures():
            initial_dict[feat.id()] = feat[input_attr_id]

        # get the transformation algorithm from the register
        algorithm = TRANSFORMATION_ALGS[algorithm_name]

        # transform the values in the dictionary with the chosen algorithm
        try:
            transformed_dict = transform(
                initial_dict, algorithm, variant, inverse)
        except ValueError:
            raise
        except NotImplementedError:
            raise

        with LayerEditingManager(
                self.layer, 'Write transformed values', DEBUG):
            for feat in self.layer.getFeatures():
                feat_id = feat.id()
                value = transformed_dict[feat_id]
                if type(value) not in (QPyNullVariant, NoneType):
                    value = float(value)
                self.layer.changeAttributeValue(feat_id, new_attr_id, value)
        return actual_new_attr_name

    def find_attribute_id(self, attribute_name):
        """
        Get the id of the attribute called attribute_name
        @param attribute_name: name of the attribute
        @return: id of the attribute, or raise AttributeError
        exception if not found
        """
        attribute_id = None
        pr = self.layer.dataProvider()
        for field_id, field in enumerate(pr.fields()):
            if field.name() == attribute_name:
                attribute_id = field_id
                return attribute_id
        # In case the attribute has not been found, raise exception
        raise AttributeError('Attribute name %s not found' % attribute_name)

    def duplicate_in_memory(self, new_name='', add_to_registry=False):
        """
        Return a memory copy of the layer

        :param new_name: The name of the copied layer.
        :type new_name: str

        :param add_to_registry: if True, the new layer will be added to
        the QgsMapRegistry
        :type: bool

        :returns: An in-memory copy of a layer.
        :rtype: QgsMapLayer

        """
        if new_name is '':
            new_name = self.layer.name() + ' TMP'

        if self.layer.type() == QgsMapLayer.VectorLayer:
            v_type = self.layer.wkbType()
            if v_type == QGis.WKBPoint:
                type_str = "point"
            elif v_type == QGis.WKBLineString:
                type_str = "linestring"
            elif v_type == QGis.WKBPolygon:
                type_str = "polygon"
            elif v_type == QGis.WKBMultiPoint:
                type_str = "multipoint"
            elif v_type == QGis.WKBMultiLineString:
                type_str = "multilinestring"
            elif v_type == QGis.WKBMultiPolygon:
                type_str = "multipolygon"
            else:
                raise TypeError('Layer type %s can not be accepted' % v_type)
        else:
            raise RuntimeError('Layer is not a VectorLayer')

        crs = self.layer.crs().authid().lower()
        my_uuid = str(uuid.uuid4())
        uri = '%s?crs=%s&index=yes&uuid=%s' % (type_str, crs, my_uuid)
        mem_layer = QgsVectorLayer(uri, new_name, 'memory')
        with LayerEditingManager(mem_layer, 'Duplicating layer', DEBUG):
            mem_provider = mem_layer.dataProvider()

            provider = self.layer.dataProvider()
            v_fields = provider.fields()

            fields = []
            for i in v_fields:
                fields.append(i)

            mem_provider.addAttributes(fields)

            for ft in provider.getFeatures():
                mem_provider.addFeatures([ft])

        if add_to_registry:
            if mem_layer.isValid():
                QgsMapLayerRegistry.instance().addMapLayer(mem_layer)
            else:
                raise RuntimeError('Layer invalid')

        return mem_layer

    def is_type_in(self, type_list):
        """
        @param type_list: we want to check if the type of the layer is
        included in this list
        @return: True if the layer is a VectorLayer and its type is in the list
        """
        if self.layer.type() == QgsMapLayer.VectorLayer:
            v_type = self.layer.wkbType()
            if v_type == QGis.WKBPoint:
                type_str = "point"
            elif v_type == QGis.WKBLineString:
                type_str = "linestring"
            elif v_type == QGis.WKBPolygon:
                type_str = "polygon"
            elif v_type == QGis.WKBMultiPoint:
                type_str = "multipoint"
            elif v_type == QGis.WKBMultiLineString:
                type_str = "multilinestring"
            elif v_type == QGis.WKBMultiPolygon:
                type_str = "multipolygon"
            else:
                raise TypeError('Layer type %s can not be accepted' % v_type)
            return type_str in type_list
        else:
            return False

{'fraction_non_alphanumeric': 0.08104135053110774, 'fraction_numerical': 0.0030349013657056147, 'mean_word_length': 1.9225332594235034, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1963821', 'n_tokens_mistral': 5538, 'n_tokens_neox': 5295, 'n_words': 1681}