<commit_before># Copyright 2019-present Kensho Technologies, LLC.
import re
import string

from graphql import build_ast_schema
from graphql.language.ast import NamedType
from graphql.language.visitor import Visitor, visit
from graphql.type.definition import GraphQLScalarType
import six

from ..exceptions import GraphQLError


class SchemaTransformError(GraphQLError):
    """Parent of specific error classes."""


class SchemaStructureError(SchemaTransformError):
    """Raised if an input schema's structure is illegal.

    This may happen if an AST cannot be built into a schema, if the schema contains disallowed
    components, or if the schema contains some field of the query type that is named differently
    from the type it queries.
    """


class InvalidTypeNameError(SchemaTransformError):
    """Raised if a type/field name is not valid.

    This may be raised if the input schema contains invalid names, or if the user attempts to
    rename a type/field to an invalid name. A name is considered valid if it consists of
    alphanumeric characters and underscores and doesn't start with a numeric character (as
    required by GraphQL), and doesn't start with double underscores as such type names are
    reserved for GraphQL internal use.
    """


class SchemaNameConflictError(SchemaTransformError):
    """Raised when renaming or merging types or fields cause name conflicts."""


_alphanumeric_and_underscore = frozenset(six.text_type(string.ascii_letters + string.digits + '_'))


def check_schema_identifier_is_valid(identifier):
    """Check if input is a valid identifier, made of alphanumeric and underscore characters.

    Args:
        identifier: str, used for identifying input schemas when merging multiple schemas

    Raises:
        - ValueError if the name is the empty string, or if it consists of characters other
          than alphanumeric characters and underscores
    """
    if not isinstance(identifier, str):
        raise ValueError(u'Schema identifier "{}" is not a string.'.format(identifier))
    if identifier == '':
        raise ValueError(u'Schema identifier must be a nonempty string.')
    illegal_characters = frozenset(identifier) - _alphanumeric_and_underscore
    if illegal_characters:
        raise ValueError(
            u'Schema identifier "{}" contains illegal characters: {}'.format(
                identifier, illegal_characters
            )
        )


_graphql_type_name_pattern = re.compile(r'^[_a-zA-Z][_a-zA-Z0-9]*$')


def check_type_name_is_valid(name):
    """Check if input is a valid, nonreserved GraphQL type name.

    Args:
        name: str

    Raises:
        - InvalidTypeNameError if the name doesn't consist of only alphanumeric characters and
          underscores, starts with a numeric character, or starts with double underscores
    """
    if not isinstance(name, str):
        raise InvalidTypeNameError(u'Name "{}" is not a string.'.format(name))
    if not _graphql_type_name_pattern.match(name):
        raise InvalidTypeNameError(u'"{}" is not a valid GraphQL name.'.format(name))
    if name.startswith('__'):
        raise InvalidTypeNameError(u'"{}" starts with two underscores, which is reserved for '
                                   u'GraphQL internal use and is not allowed.'.format(name))


def get_query_type_name(schema):
    """Get the name of the query type of the input schema.

    Args:
        schema: GraphQLSchema<commit_msg>Import COMPILED_NAME_PATTERN and remove _graphql_type_name_pattern (#418)<commit_after># Copyright 2019-present Kensho Technologies, LLC.
import string

from graphql import build_ast_schema
from graphql.language.ast import NamedType
from graphql.language.visitor import Visitor, visit
from graphql.type.definition import GraphQLScalarType
from graphql.utils.assert_valid_name import COMPILED_NAME_PATTERN
import six

from ..exceptions import GraphQLError


class SchemaTransformError(GraphQLError):
    """Parent of specific error classes."""


class SchemaStructureError(SchemaTransformError):
    """Raised if an input schema's structure is illegal.

    This may happen if an AST cannot be built into a schema, if the schema contains disallowed
    components, or if the schema contains some field of the query type that is named differently
    from the type it queries.
    """


class InvalidTypeNameError(SchemaTransformError):
    """Raised if a type/field name is not valid.

    This may be raised if the input schema contains invalid names, or if the user attempts to
    rename a type/field to an invalid name. A name is considered valid if it consists of
    alphanumeric characters and underscores and doesn't start with a numeric character (as
    required by GraphQL), and doesn't start with double underscores as such type names are
    reserved for GraphQL internal use.
    """


class SchemaNameConflictError(SchemaTransformError):
    """Raised when renaming or merging types or fields cause name conflicts."""


_alphanumeric_and_underscore = frozenset(six.text_type(string.ascii_letters + string.digits + '_'))


def check_schema_identifier_is_valid(identifier):
    """Check if input is a valid identifier, made of alphanumeric and underscore characters.

    Args:
        identifier: str, used for identifying input schemas when merging multiple schemas

    Raises:
        - ValueError if the name is the empty string, or if it consists of characters other
          than alphanumeric characters and underscores
    """
    if not isinstance(identifier, str):
        raise ValueError(u'Schema identifier "{}" is not a string.'.format(identifier))
    if identifier == '':
        raise ValueError(u'Schema identifier must be a nonempty string.')
    illegal_characters = frozenset(identifier) - _alphanumeric_and_underscore
    if illegal_characters:
        raise ValueError(
            u'Schema identifier "{}" contains illegal characters: {}'.format(
                identifier, illegal_characters
            )
        )


def check_type_name_is_valid(name):
    """Check if input is a valid, nonreserved GraphQL type name.

    Args:
        name: str

    Raises:
        - InvalidTypeNameError if the name doesn't consist of only alphanumeric characters and
          underscores, starts with a numeric character, or starts with double underscores
    """
    if not isinstance(name, str):
        raise InvalidTypeNameError(u'Name "{}" is not a string.'.format(name))
    if not COMPILED_NAME_PATTERN.match(name):
        raise InvalidTypeNameError(u'"{}" is not a valid GraphQL name.'.format(name))
    if name.startswith('__'):
        raise InvalidTypeNameError(u'"{}" starts with two underscores, which is reserved for '
                                   u'GraphQL internal use and is not allowed.'.format(name))


def get_query_type_name(schema):
    """Get the name of the query type of the input schema.

    Args:
        schema: GraphQLSchema
{'fraction_non_alphanumeric': 0.06815561959654179, 'fraction_numerical': 0.0018731988472622477, 'mean_word_length': 3.5336381450032657, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2132874', 'n_tokens_mistral': 1866, 'n_tokens_neox': 1778, 'n_words': 722}