<commit_before># encoding: utf-8

"""
.. codeauthor:: <NAME> <<EMAIL>>
"""

from __future__ import absolute_import
from __future__ import unicode_literals

import re

from ._common import (
    _validate_null_string,
    _preprocess,
)
from ._error import (
    InvalidCharError,
    ValidReservedNameError,
    InvalidReservedNameError
)


__SQLITE_VALID_RESERVED_KEYWORDS = [
    'ABORT', 'ACTION', 'AFTER', 'ANALYZE', 'ASC', 'ATTACH',
    'BEFORE', 'BEGIN', 'BY',
    'CASCADE', 'CAST', 'COLUMN', 'CONFLICT', 'CROSS', 'CURRENT_DATE',
    'CURRENT_TIME', 'CURRENT_TIMESTAMP',
    'DATABASE', 'DEFERRED', 'DESC', 'DETACH',
    'EACH', 'END', 'EXCLUSIVE', 'EXPLAIN',
    'FAIL', 'FOR', 'FULL', 'GLOB',
    'IGNORE', 'IMMEDIATE', 'INDEXED', 'INITIALLY', 'INNER', 'INSTEAD',
    'KEY',
    'LEFT', 'LIKE',
    'MATCH',
    'NATURAL',
    'NO',
    'OF', 'OFFSET', 'OUTER',
    'PLAN', 'PRAGMA',
    'QUERY',
    'RAISE', 'RECURSIVE', 'REGEXP', 'REINDEX', 'RELEASE', 'RENAME', 'REPLACE',
    'RESTRICT', 'RIGHT', 'ROLLBACK', 'ROW',
    'SAVEPOINT',
    'TEMP', 'TEMPORARY', 'TRIGGER',
    'VACUUM', 'VIEW', 'VIRTUAL',
    'WITH', 'WITHOUT',
]
__SQLITE_INVALID_RESERVED_KEYWORDS = [
    'ADD', 'ALL', 'ALTER', 'AND', 'AS', 'AUTOINCREMENT',
    'BETWEEN',
    'CASE', 'CHECK', 'COLLATE', 'COMMIT', 'CONSTRAINT', 'CREATE',
    'DEFAULT', 'DEFERRABLE', 'DELETE', 'DISTINCT', 'DROP',
    'ELSE', 'ESCAPE', 'EXCEPT', 'EXISTS', 'FOREIGN',
    'FROM',
    'GROUP',
    'HAVING',
    'IN', 'INDEX', 'INSERT', 'INTERSECT', 'INTO', 'IS', 'ISNULL',
    'JOIN',
    'LIMIT',
    'NOT', 'NOTNULL', 'NULL',
    'ON', 'OR', 'ORDER',
    'PRIMARY',
    'REFERENCES',
    'SELECT', 'SET',
    'TABLE', 'THEN', 'TO', 'TRANSACTION',
    'UNION', 'UNIQUE', 'UPDATE', 'USING',
    'VALUES',
    'WHEN', 'WHERE',
]

__SQLITE_VALID_RESERVED_KEYWORDS_TABLE = (
    __SQLITE_VALID_RESERVED_KEYWORDS)
__SQLITE_INVALID_RESERVED_KEYWORDS_TABLE = (
    __SQLITE_INVALID_RESERVED_KEYWORDS + ['IF'])

__SQLITE_VALID_RESERVED_KEYWORDS_ATTR = (
    __SQLITE_VALID_RESERVED_KEYWORDS + ['IF'])
__SQLITE_INVALID_RESERVED_KEYWORDS_ATTR = (
    __SQLITE_INVALID_RESERVED_KEYWORDS)

__RE_INVALID_SQLITE_NAME_HEAD = re.compile("^[^a-zA-Z]+", re.UNICODE)


def validate_sqlite_table_name(name):
    """
    :param str name: Name to validate.
    :raises pathvalidate.NullNameError: If the ``name`` is empty.
    :raises pathvalidate.InvalidCharError:
        If the first character of the ``name`` is invalid
        (not an alphabetic character).
    :raises pathvalidate.InvalidReservedNameError:
        |raises_sqlite_keywords|
        And invalid as a table name.
    :raises pathvalidate.ValidReservedNameError:
        |raises_sqlite_keywords|
        However, valid as a table name.
    """

    _validate_null_string(name)

    if name.upper() in __SQLITE_INVALID_RESERVED_KEYWORDS_TABLE:
        raise InvalidReservedNameError(
            "'{:s}' is a reserved keyword by sqlite".format(name))

    if name.upper() in __SQLITE_VALID_RESERVED_KEYWORDS_TABLE:
        raise ValidReservedNameError(
            "'{:s}' is a reserved keyword by sqlite".format(name))

    match = __RE_INVALID_SQLITE_NAME_HEAD.search(_preprocess(name))
    if match is not None:
        name = match.group()

        try:
            name.encode("ascii")
        except UnicodeEncodeError:
            try:
                name.encode("utf8")
            except Exception:
                raise
            else:
                return

        raise InvalidCharError(
            "the first character of the sqlite name is invalid: '{}'".format(
                re.escape(name)))


def validate_sqlite_attr_name(name):
    """
    :param str name: Name to validate.
    :raises pathvalidate.NullNameError: If the ``name`` is empty.
    :raises pathvalidate.InvalidCharError:
        If the first character of the ``name`` is invalid
        (not an alphabetic character).
    :raises pathvalidate.InvalidReservedNameError:
        |raises_sqlite_keywords|
        And invalid as a attribute name.
    :raises pathvalidate.ValidReservedNameError:
        |raises_sqlite_keywords|
        However, valid as a attribute name.
    """

    _validate_null_string(name)<commit_msg>Fix typos<commit_after># encoding: utf-8

"""
.. codeauthor:: <NAME> <<EMAIL>>
"""

from __future__ import absolute_import
from __future__ import unicode_literals

import re

from ._common import (
    _validate_null_string,
    _preprocess,
)
from ._error import (
    InvalidCharError,
    ValidReservedNameError,
    InvalidReservedNameError
)


__SQLITE_VALID_RESERVED_KEYWORDS = [
    'ABORT', 'ACTION', 'AFTER', 'ANALYZE', 'ASC', 'ATTACH',
    'BEFORE', 'BEGIN', 'BY',
    'CASCADE', 'CAST', 'COLUMN', 'CONFLICT', 'CROSS', 'CURRENT_DATE',
    'CURRENT_TIME', 'CURRENT_TIMESTAMP',
    'DATABASE', 'DEFERRED', 'DESC', 'DETACH',
    'EACH', 'END', 'EXCLUSIVE', 'EXPLAIN',
    'FAIL', 'FOR', 'FULL', 'GLOB',
    'IGNORE', 'IMMEDIATE', 'INDEXED', 'INITIALLY', 'INNER', 'INSTEAD',
    'KEY',
    'LEFT', 'LIKE',
    'MATCH',
    'NATURAL',
    'NO',
    'OF', 'OFFSET', 'OUTER',
    'PLAN', 'PRAGMA',
    'QUERY',
    'RAISE', 'RECURSIVE', 'REGEXP', 'REINDEX', 'RELEASE', 'RENAME', 'REPLACE',
    'RESTRICT', 'RIGHT', 'ROLLBACK', 'ROW',
    'SAVEPOINT',
    'TEMP', 'TEMPORARY', 'TRIGGER',
    'VACUUM', 'VIEW', 'VIRTUAL',
    'WITH', 'WITHOUT',
]
__SQLITE_INVALID_RESERVED_KEYWORDS = [
    'ADD', 'ALL', 'ALTER', 'AND', 'AS', 'AUTOINCREMENT',
    'BETWEEN',
    'CASE', 'CHECK', 'COLLATE', 'COMMIT', 'CONSTRAINT', 'CREATE',
    'DEFAULT', 'DEFERRABLE', 'DELETE', 'DISTINCT', 'DROP',
    'ELSE', 'ESCAPE', 'EXCEPT', 'EXISTS', 'FOREIGN',
    'FROM',
    'GROUP',
    'HAVING',
    'IN', 'INDEX', 'INSERT', 'INTERSECT', 'INTO', 'IS', 'ISNULL',
    'JOIN',
    'LIMIT',
    'NOT', 'NOTNULL', 'NULL',
    'ON', 'OR', 'ORDER',
    'PRIMARY',
    'REFERENCES',
    'SELECT', 'SET',
    'TABLE', 'THEN', 'TO', 'TRANSACTION',
    'UNION', 'UNIQUE', 'UPDATE', 'USING',
    'VALUES',
    'WHEN', 'WHERE',
]

__SQLITE_VALID_RESERVED_KEYWORDS_TABLE = (
    __SQLITE_VALID_RESERVED_KEYWORDS)
__SQLITE_INVALID_RESERVED_KEYWORDS_TABLE = (
    __SQLITE_INVALID_RESERVED_KEYWORDS + ['IF'])

__SQLITE_VALID_RESERVED_KEYWORDS_ATTR = (
    __SQLITE_VALID_RESERVED_KEYWORDS + ['IF'])
__SQLITE_INVALID_RESERVED_KEYWORDS_ATTR = (
    __SQLITE_INVALID_RESERVED_KEYWORDS)

__RE_INVALID_SQLITE_NAME_HEAD = re.compile("^[^a-zA-Z]+", re.UNICODE)


def validate_sqlite_table_name(name):
    """
    :param str name: Name to validate.
    :raises pathvalidate.NullNameError: If the ``name`` is empty.
    :raises pathvalidate.InvalidCharError:
        If the first character of the ``name`` is invalid
        (not an alphabetic character).
    :raises pathvalidate.InvalidReservedNameError:
        |raises_sqlite_keywords|
        And invalid as a table name.
    :raises pathvalidate.ValidReservedNameError:
        |raises_sqlite_keywords|
        However, valid as a table name.
    """

    _validate_null_string(name)

    if name.upper() in __SQLITE_INVALID_RESERVED_KEYWORDS_TABLE:
        raise InvalidReservedNameError(
            "'{:s}' is a reserved keyword by sqlite".format(name))

    if name.upper() in __SQLITE_VALID_RESERVED_KEYWORDS_TABLE:
        raise ValidReservedNameError(
            "'{:s}' is a reserved keyword by sqlite".format(name))

    match = __RE_INVALID_SQLITE_NAME_HEAD.search(_preprocess(name))
    if match is not None:
        name = match.group()

        try:
            name.encode("ascii")
        except UnicodeEncodeError:
            try:
                name.encode("utf8")
            except Exception:
                raise
            else:
                return

        raise InvalidCharError(
            "the first character of the sqlite name is invalid: '{}'".format(
                re.escape(name)))


def validate_sqlite_attr_name(name):
    """
    :param str name: Name to validate.
    :raises pathvalidate.NullNameError: If the ``name`` is empty.
    :raises pathvalidate.InvalidCharError:
        If the first character of the ``name`` is invalid
        (not an alphabetic character).
    :raises pathvalidate.InvalidReservedNameError:
        |raises_sqlite_keywords|
        And invalid as an attribute name.
    :raises pathvalidate.ValidReservedNameError:
        |raises_sqlite_keywords|
        However, valid as an attribute name.
    """

    _validate_null_string(name)
{'fraction_non_alphanumeric': 0.1438388625592417, 'fraction_numerical': 0.00047393364928909954, 'mean_word_length': 3.284771573604061, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '626164', 'n_tokens_mistral': 2995, 'n_tokens_neox': 2899, 'n_words': 644}