<commit_before># -*- coding: utf-8 -*-
"""Utilities for generating OpenAPI spec (fka Swagger) entities from
marshmallow :class:`Schemas <marshmallow.Schema>` and :class:`Fields <marshmallow.fields.Field>`.

OpenAPI 2.0 spec: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
"""
from __future__ import absolute_import, unicode_literals
import operator
import warnings
import functools
from collections import OrderedDict
import copy

import marshmallow
from marshmallow.utils import is_collection
from marshmallow.compat import text_type, binary_type, iteritems
from marshmallow.orderedset import OrderedSet

from apispec.lazy_dict import LazyDict

##### marshmallow #####

# marshmallow field => (JSON Schema type, format)
FIELD_MAPPING = {
    marshmallow.fields.Integer: ('integer', 'int32'),
    marshmallow.fields.Number: ('number', None),
    marshmallow.fields.Float: ('number', 'float'),
    marshmallow.fields.Decimal: ('number', None),
    marshmallow.fields.String: ('string', None),
    marshmallow.fields.Boolean: ('boolean', None),
    marshmallow.fields.UUID: ('string', 'uuid'),
    marshmallow.fields.DateTime: ('string', 'date-time'),
    marshmallow.fields.Date: ('string', 'date'),
    marshmallow.fields.Time: ('string', None),
    marshmallow.fields.Email: ('string', 'email'),
    marshmallow.fields.URL: ('string', 'url'),
    marshmallow.fields.Dict: ('object', None),
    # Assume base Field and Raw are strings
    marshmallow.fields.Field: ('string', None),
    marshmallow.fields.Raw: ('string', None),
    marshmallow.fields.List: ('array', None),
}

CUSTOM_FIELD_MAPPING_ATTR = '__custom_swagger_field_mapping'


def map_to_swagger_field(*args):
    """
    decorator to set mapping for custom fields.

    args can be:

    - a core marshmallow field type (in which case we reuse that type's mapping)
    - a pair of string|None
    """
    if len(args) == 1 and args[0] in FIELD_MAPPING:
        swagger_type_field = FIELD_MAPPING[args[0]]
    elif len(args) == 2:
        swagger_type_field = args
    else:
        raise ArgumentError('pass core marshmallow field type or (type, fmt) pair')

    def inner(field_type):
        setattr(field_type, CUSTOM_FIELD_MAPPING_ATTR, swagger_type_field)

        return field_type

    return inner


class OrderedLazyDict(LazyDict, OrderedDict):
    pass<commit_msg>fix ex type, msg<commit_after># -*- coding: utf-8 -*-
"""Utilities for generating OpenAPI spec (fka Swagger) entities from
marshmallow :class:`Schemas <marshmallow.Schema>` and :class:`Fields <marshmallow.fields.Field>`.

OpenAPI 2.0 spec: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md
"""
from __future__ import absolute_import, unicode_literals
import operator
import warnings
import functools
from collections import OrderedDict
import copy

import marshmallow
from marshmallow.utils import is_collection
from marshmallow.compat import text_type, binary_type, iteritems
from marshmallow.orderedset import OrderedSet

from apispec.lazy_dict import LazyDict

##### marshmallow #####

# marshmallow field => (JSON Schema type, format)
FIELD_MAPPING = {
    marshmallow.fields.Integer: ('integer', 'int32'),
    marshmallow.fields.Number: ('number', None),
    marshmallow.fields.Float: ('number', 'float'),
    marshmallow.fields.Decimal: ('number', None),
    marshmallow.fields.String: ('string', None),
    marshmallow.fields.Boolean: ('boolean', None),
    marshmallow.fields.UUID: ('string', 'uuid'),
    marshmallow.fields.DateTime: ('string', 'date-time'),
    marshmallow.fields.Date: ('string', 'date'),
    marshmallow.fields.Time: ('string', None),
    marshmallow.fields.Email: ('string', 'email'),
    marshmallow.fields.URL: ('string', 'url'),
    marshmallow.fields.Dict: ('object', None),
    # Assume base Field and Raw are strings
    marshmallow.fields.Field: ('string', None),
    marshmallow.fields.Raw: ('string', None),
    marshmallow.fields.List: ('array', None),
}

CUSTOM_FIELD_MAPPING_ATTR = '__custom_swagger_field_mapping'


def map_to_swagger_field(*args):
    """
    decorator to set mapping for custom fields.

    args can be:

    - a core marshmallow field type (in which case we reuse that type's mapping)
    - a pair of string|None
    """
    if len(args) == 1 and args[0] in FIELD_MAPPING:
        swagger_type_field = FIELD_MAPPING[args[0]]
    elif len(args) == 2:
        swagger_type_field = args
    else:
        raise TypeError('Pass core marshmallow field type or (type, fmt) pair')

    def inner(field_type):
        setattr(field_type, CUSTOM_FIELD_MAPPING_ATTR, swagger_type_field)

        return field_type

    return inner


class OrderedLazyDict(LazyDict, OrderedDict):
    pass
{'fraction_non_alphanumeric': 0.12497356735039121, 'fraction_numerical': 0.004652146331148234, 'mean_word_length': 4.314606741573034, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '503245', 'n_tokens_mistral': 1561, 'n_tokens_neox': 1508, 'n_words': 384}