<commit_before>#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014 Spotify AB

from __future__ import absolute_import, division, print_function

__all__ = ["validate_raml", "InvalidRamlFileError"]

from .parser import APIRoot
from .loader import RAMLLoader


VALID_RAML_VERSIONS = ["0.8"]


class InvalidRamlFileError(Exception):
    pass


def validate_raml_header(raml_file):
    """Validate Header of RAML File"""
    try:
        with open(raml_file, 'r') as r:
            # Returns None if empty file lines
            raml_header = r.readline().split('\n')[0]
            if not raml_header:
                msg = ("RAML header empty. Please make sure the first line "
                       "of the file contains a valid RAML file definition.")
                raise InvalidRamlFileError(msg)

            try:
                raml_def, version = raml_header.split()
            except ValueError:
                msg = ("Not a valid RAML header: {0}.".format(raml_header))
                raise InvalidRamlFileError(msg)

            if raml_def != "#%RAML":
                msg = "Not a valid RAML header: {0}.".format(raml_def)
                raise InvalidRamlFileError(msg)

            if version not in VALID_RAML_VERSIONS:
                msg = "Not a valid version of RAML: {0}.".format(version)
                raise InvalidRamlFileError(msg)
    except IOError as e:
        raise InvalidRamlFileError(e)


def validate_api_title(api):
    """Require an API Title."""
    title = api.title
    if not title:
        msg = 'RAML File does not define an API title.'
        raise InvalidRamlFileError(msg)


def validate_api_version(api, prod=True):
    """Require an API Version (e.g. api.foo.com/v1)."""
    if prod and not api.version:
        msg = 'RAML File does not define an API version.'
        raise InvalidRamlFileError(msg)


def validate_base_uri(api):
    """Require a Base URI."""
    base_uri = api.base_uri
    if not base_uri:
        msg = 'RAML File does not define the baseUri.'
        raise InvalidRamlFileError(msg)


def validate_base_uri_params(api):
    """
    Require that Base URI Parameters have a ``default`` parameter set.
    """
    base_uri_params = api.base_uri_parameters
    if base_uri_params:
        for param in base_uri_params:
            if not param.default:
                msg = "'{0}' needs a default parameter.".format(param.name)
                raise InvalidRamlFileError(msg)


def validate_resource_response(api):
    """
    Assert only ``body``, ``headers``, and ``description`` are keys
    defined for a ``Response``.
    """
    resources = api.resources
    valid_keys = ['body', 'headers', 'description']
    for resource in list(resources.values()):
        responses = resource.responses
        for resp in responses:
            for key in list(resp.data.keys()):
                if key not in valid_keys:
                    msg = "'{0}' not a valid Response parameter.".format(
                        key)
                    raise InvalidRamlFileError(msg)


def validate_root_documentation(api):
    """
    Assert that if there is ``documentation`` defined in the root of the
    RAML file, that it contains a ``title`` and ``content``.
    """
    docs = api.documentation
    if docs:<commit_msg>Small logic fixes<commit_after>#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (c) 2014 Spotify AB

from __future__ import absolute_import, division, print_function

__all__ = ["validate_raml", "InvalidRamlFileError"]

from .parser import APIRoot
from .loader import RAMLLoader


VALID_RAML_VERSIONS = ["0.8"]


class InvalidRamlFileError(Exception):
    pass


def validate_raml_header(raml_file):
    """Validate Header of RAML File"""
    try:
        with open(raml_file, 'r') as r:
            # Returns None if empty file lines
            raml_header = r.readline().split('\n')[0]
            if not raml_header:
                msg = ("RAML header empty. Please make sure the first line "
                       "of the file contains a valid RAML file definition.")
                raise InvalidRamlFileError(msg)

            try:
                raml_def, version = raml_header.split()
            except ValueError:
                msg = ("Not a valid RAML header: {0}.".format(raml_header))
                raise InvalidRamlFileError(msg)

            if raml_def != "#%RAML":
                msg = "Not a valid RAML header: {0}.".format(raml_def)
                raise InvalidRamlFileError(msg)

            if version not in VALID_RAML_VERSIONS:
                msg = "Not a valid version of RAML: {0}.".format(version)
                raise InvalidRamlFileError(msg)
    except IOError as e:
        raise InvalidRamlFileError(e)


def validate_api_title(api):
    """Require an API Title."""
    if not api.title:
        msg = 'RAML File does not define an API title.'
        raise InvalidRamlFileError(msg)


def validate_api_version(api, prod=True):
    """Require an API Version (e.g. api.foo.com/v1)."""
    if prod and not api.version:
        msg = 'RAML File does not define an API version.'
        raise InvalidRamlFileError(msg)


def validate_base_uri(api):
    """Require a Base URI."""
    if not api.base_uri:
        msg = 'RAML File does not define the baseUri.'
        raise InvalidRamlFileError(msg)


def validate_base_uri_params(api):
    """
    Require that Base URI Parameters have a ``default`` parameter set.
    """
    base_uri_params = api.base_uri_parameters
    if base_uri_params:
        for param in base_uri_params:
            if not param.default:
                msg = "'{0}' needs a default parameter.".format(param.name)
                raise InvalidRamlFileError(msg)


def validate_resource_response(api):
    """
    Assert only ``body``, ``headers``, and ``description`` are keys
    defined for a ``Response``.
    """
    valid_keys = ['body', 'headers', 'description']
    for resource in list(api.resources.values()):
        for resp in resource.responses:
            for key in list(resp.data.keys()):
                if key not in valid_keys:
                    msg = "'{0}' not a valid Response parameter.".format(
                        key)
                    raise InvalidRamlFileError(msg)


def validate_root_documentation(api):
    """
    Assert that if there is ``documentation`` defined in the root of the
    RAML file, that it contains a ``title`` and ``content``.
    """
    docs = api.documentation
    if docs:
{'fraction_non_alphanumeric': 0.10193429536383175, 'fraction_numerical': 0.004298434141848327, 'mean_word_length': 2.292066700353714, 'pattern_counts': {'":': 2, '<': 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': '5906400', 'n_tokens_mistral': 1930, 'n_tokens_neox': 1849, 'n_words': 593}