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

import json
import types

import web

from nailgun.db import orm
from nailgun.logger import logger
from nailgun.settings import settings
from nailgun.api.models import Release
from nailgun.api.models import Cluster
from nailgun.api.models import ClusterChanges
from nailgun.api.models import Attributes
from nailgun.api.models import Node
from nailgun.api.models import NetworkGroup
from nailgun.api.models import Network
from nailgun.api.models import Notification
from nailgun.volumes.manager import VolumeManager
from netaddr import IPNetwork, AddrFormatError


class BasicValidator(object):
    @classmethod
    def validate_json(cls, data):
        if data:
            try:
                res = json.loads(data)
            except:
                raise web.webapi.badrequest(
                    message="Invalid json format"
                )
        else:
            raise web.webapi.badrequest(
                message="Empty request received"
            )
        return res

    @classmethod
    def validate(cls, data):
        raise NotImplementedError("You should override this method")


class MetaInterfacesValidator(BasicValidator):
    @classmethod
    def validate(cls, interfaces):
        if not isinstance(interfaces, list):
            raise web.webapi.badrequest(
                message="Meta.interfaces should be list"
            )
        for nic in interfaces:
            for key in ('mac', 'name'):
                if key in nic and isinstance(nic[key], basestring) and\
                        nic[key]:
                    continue
                raise web.webapi.badrequest(
                    message="Interface in meta.interfaces should have"
                            " key %r with nonempty string value" % key
                )
            for key in ('max_speed', 'current_speed'):
                if key not in nic or isinstance(nic[key], types.NoneType) or\
                        (isinstance(nic[key], int) and nic[key] > 0):
                    continue
                raise web.webapi.badrequest(
                    message="Interface in meta.interfaces should have key %r"
                            " with positive integer or Null value" % key
                )


class MetaValidator(BasicValidator):
    @classmethod
    def validate(cls, meta):
        if not isinstance(meta, dict):
            raise web.webapi.badrequest(message="Meta should be dict")
        if 'interfaces' in meta:
            MetaInterfacesValidator.validate(meta['interfaces'])<commit_msg>current speed fix<commit_after># -*- coding: utf-8 -*-

import json
import types

import web

from nailgun.db import orm
from nailgun.logger import logger
from nailgun.settings import settings
from nailgun.api.models import Release
from nailgun.api.models import Cluster
from nailgun.api.models import ClusterChanges
from nailgun.api.models import Attributes
from nailgun.api.models import Node
from nailgun.api.models import NetworkGroup
from nailgun.api.models import Network
from nailgun.api.models import Notification
from nailgun.volumes.manager import VolumeManager
from netaddr import IPNetwork, AddrFormatError


class BasicValidator(object):
    @classmethod
    def validate_json(cls, data):
        if data:
            try:
                res = json.loads(data)
            except:
                raise web.webapi.badrequest(
                    message="Invalid json format"
                )
        else:
            raise web.webapi.badrequest(
                message="Empty request received"
            )
        return res

    @classmethod
    def validate(cls, data):
        raise NotImplementedError("You should override this method")


class MetaInterfacesValidator(BasicValidator):
    @classmethod
    def validate(cls, interfaces):
        if not isinstance(interfaces, list):
            raise web.webapi.badrequest(
                message="Meta.interfaces should be list"
            )
        for nic in interfaces:
            for key in ('mac', 'name'):
                if key in nic and isinstance(nic[key], basestring) and\
                        nic[key]:
                    continue
                raise web.webapi.badrequest(
                    message="Interface in meta.interfaces should have"
                            " key %r with nonempty string value" % key
                )
            for key in ('max_speed', 'current_speed'):
                if key not in nic or isinstance(nic[key], types.NoneType) or\
                        (isinstance(nic[key], int) and nic[key] >= 0):
                    continue
                raise web.webapi.badrequest(
                    message="Interface in meta.interfaces should have key %r"
                            " with positive integer or Null value" % key
                )


class MetaValidator(BasicValidator):
    @classmethod
    def validate(cls, meta):
        if not isinstance(meta, dict):
            raise web.webapi.badrequest(message="Meta should be dict")
        if 'interfaces' in meta:
            MetaInterfacesValidator.validate(meta['interfaces'])
{'fraction_non_alphanumeric': 0.07422559906487435, 'fraction_numerical': 0.0007792713812585232, 'mean_word_length': 1.9590778097982708, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4102542', 'n_tokens_mistral': 1287, 'n_tokens_neox': 1232, 'n_words': 405}