<commit_before># -*- coding: utf-8 -*-

import json
import traceback
import web

from nailgun.logger import logger
from nailgun.network.manager import NetworkManager
from nailgun.api.validators import NetworkGroupValidator
from nailgun.api.models import Cluster
from nailgun.api.models import NetworkGroup
from nailgun.api.handlers.base import JSONHandler, content_json
from nailgun.api.handlers.tasks import TaskHandler
from nailgun.task.helpers import update_task_status
from nailgun.task.manager import CheckNetworksTaskManager
from nailgun.task.manager import VerifyNetworksTaskManager

class NetworkConfigurationVerifyHandler(JSONHandler):

    validator = NetworkGroupValidator

    @content_json
    def PUT(self, cluster_id):
        cluster = self.get_object_or_404(Cluster, cluster_id)
        data = json.loads(web.data())
        networks = data['networks']
        self.validator.validate_collection_update(json.dumps(networks))
        vlan_ids = NetworkGroup.generate_vlan_ids_list(networks)
        task_manager = VerifyNetworksTaskManager(cluster_id=cluster.id)
        task = task_manager.execute(data, vlan_ids)
        return TaskHandler.render(task)


class NetworkConfigurationHandler(JSONHandler):
    fields = ('id', 'cluster_id', 'name', 'cidr',
              'vlan_start', 'network_size', 'amount')

    validator = NetworkGroupValidator

    @content_json
    def GET(self, cluster_id):
        cluster = self.get_object_or_404(Cluster, cluster_id)
        result = {}
        result['net_manager'] = cluster.net_manager
        result['networks'] = map(self.render, cluster.network_groups)

        return result

    @content_json
    def PUT(self, cluster_id):
        data = json.loads(web.data())
        cluster = self.get_object_or_404(Cluster, cluster_id)

        network_manager = NetworkManager()
        task_manager = CheckNetworksTaskManager(cluster_id=cluster.id)
        task = task_manager.execute(data)

        if 'net_manager' in data:
            setattr(cluster, 'net_manager', data['net_manager'])

        if 'networks' in data and task.status != 'error':
            new_nets = self.validator.validate_collection_update(
                json.dumps(data['networks']))

            for ng in new_nets:
                ng_db = self.db.query(NetworkGroup).get(ng['id'])
                for key, value in ng.iteritems():
                    setattr(ng_db, key, value)
                try:
                    network_manager.create_networks(ng_db)
                    ng_db.cluster.add_pending_changes('networks')
                except Exception as exc:
                    err = str(exc)
                    update_task_status(
                        task.uuid,
                        status="error",
                        progress=100,
                        msg=err
                    )
                    logger.error(traceback.format_exc())
                    break

        if task.status == 'error':
            self.db.rollback()
        else:
            self.db.commit()

        return TaskHandler.render(task)
<commit_msg>pep fix<commit_after># -*- coding: utf-8 -*-

import json
import traceback
import web

from nailgun.logger import logger
from nailgun.network.manager import NetworkManager
from nailgun.api.validators import NetworkGroupValidator
from nailgun.api.models import Cluster
from nailgun.api.models import NetworkGroup
from nailgun.api.handlers.base import JSONHandler, content_json
from nailgun.api.handlers.tasks import TaskHandler
from nailgun.task.helpers import update_task_status
from nailgun.task.manager import CheckNetworksTaskManager
from nailgun.task.manager import VerifyNetworksTaskManager


class NetworkConfigurationVerifyHandler(JSONHandler):

    validator = NetworkGroupValidator

    @content_json
    def PUT(self, cluster_id):
        cluster = self.get_object_or_404(Cluster, cluster_id)
        data = json.loads(web.data())
        networks = data['networks']
        self.validator.validate_collection_update(json.dumps(networks))
        vlan_ids = NetworkGroup.generate_vlan_ids_list(networks)
        task_manager = VerifyNetworksTaskManager(cluster_id=cluster.id)
        task = task_manager.execute(data, vlan_ids)
        return TaskHandler.render(task)


class NetworkConfigurationHandler(JSONHandler):
    fields = ('id', 'cluster_id', 'name', 'cidr',
              'vlan_start', 'network_size', 'amount')

    validator = NetworkGroupValidator

    @content_json
    def GET(self, cluster_id):
        cluster = self.get_object_or_404(Cluster, cluster_id)
        result = {}
        result['net_manager'] = cluster.net_manager
        result['networks'] = map(self.render, cluster.network_groups)

        return result

    @content_json
    def PUT(self, cluster_id):
        data = json.loads(web.data())
        cluster = self.get_object_or_404(Cluster, cluster_id)

        network_manager = NetworkManager()
        task_manager = CheckNetworksTaskManager(cluster_id=cluster.id)
        task = task_manager.execute(data)

        if 'net_manager' in data:
            setattr(cluster, 'net_manager', data['net_manager'])

        if 'networks' in data and task.status != 'error':
            new_nets = self.validator.validate_collection_update(
                json.dumps(data['networks']))

            for ng in new_nets:
                ng_db = self.db.query(NetworkGroup).get(ng['id'])
                for key, value in ng.iteritems():
                    setattr(ng_db, key, value)
                try:
                    network_manager.create_networks(ng_db)
                    ng_db.cluster.add_pending_changes('networks')
                except Exception as exc:
                    err = str(exc)
                    update_task_status(
                        task.uuid,
                        status="error",
                        progress=100,
                        msg=err
                    )
                    logger.error(traceback.format_exc())
                    break

        if task.status == 'error':
            self.db.rollback()
        else:
            self.db.commit()

        return TaskHandler.render(task)

{'fraction_non_alphanumeric': 0.08842105263157894, 'fraction_numerical': 0.004210526315789474, 'mean_word_length': 2.384109589041096, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4735564', 'n_tokens_mistral': 1722, 'n_tokens_neox': 1658, 'n_words': 345}