<commit_before># coding=utf-8
from __future__ import unicode_literals

import re
from functools import wraps
from six.moves.urllib.parse import urlsplit
from textwrap import dedent

import sqlalchemy as sa
from seamless_karma.extensions import db
from flask import request
from flask.ext.restful import abort, marshal
from .utils import update_url_query


def parse_sqlalchemy_exception(exception, model=None):
    """
    Given a SQLAlchemy exception, return a string to nicely display to the
    client that explains the error.
    """
    message = exception.orig.args[0]
    if not model:
        return message
    if db.engine.name == 'postgresql':
        re_strs = [
            r"""
            duplicate key value violates unique constraint "[^"]+"
            DETAIL:  Key \((?P<column>[^)]+)\)=\((?P<value>[^)]+)\) already exists.
            """
        ]
        UNIQUE_RES = [re.compile(dedent(s).strip()) for s in re_strs]
        NOT_NULL_RES = []
    else:  # sqlite
        unique_re_strs = [
            r"column (?P<column>\w+) is not unique",
            r"UNIQUE constraint failed: (?P<table>\w+)\.(?P<column>\w+)",
        ]
        not_null_re_strs = [
            r"NOT NULL constraint failed: (?P<table>\w+)\.(?P<column>\w+)",
        ]
        UNIQUE_RES = [re.compile(dedent(s).strip()) for s in unique_re_strs]
        NOT_NULL_RES = [re.compile(dedent(s).strip()) for s in not_null_re_strs]
    for regex in UNIQUE_RES:
        match = regex.search(message)
        if match:
            column = match.group("column")
            try:
                value = match.group("value")<commit_msg>handle not-null errors for postgres<commit_after># coding=utf-8
from __future__ import unicode_literals

import re
from functools import wraps
from six.moves.urllib.parse import urlsplit
from textwrap import dedent

import sqlalchemy as sa
from seamless_karma.extensions import db
from flask import request
from flask.ext.restful import abort, marshal
from .utils import update_url_query


def parse_sqlalchemy_exception(exception, model=None):
    """
    Given a SQLAlchemy exception, return a string to nicely display to the
    client that explains the error.
    """
    message = exception.orig.args[0]
    if not model:
        return message
    if db.engine.name == 'postgresql':
        unique_re_strs = [
            r"""
            duplicate key value violates unique constraint "[^"]+"
            DETAIL:  Key \((?P<column>[^)]+)\)=\((?P<value>[^)]+)\) already exists.
            """
        ]
        not_null_re_strs = [
            r"""
            null value in column "(?P<column>\w+)" violates not-null constraint
            """,
        ]
        UNIQUE_RES = [re.compile(dedent(s).strip()) for s in unique_re_strs]
        NOT_NULL_RES = [re.compile(dedent(s).strip()) for s in not_null_re_strs]
    else:  # sqlite
        unique_re_strs = [
            r"column (?P<column>\w+) is not unique",
            r"UNIQUE constraint failed: (?P<table>\w+)\.(?P<column>\w+)",
        ]
        not_null_re_strs = [
            r"(?P<table>\w+)\.(?P<column>\w+) may not be NULL",
            r"NOT NULL constraint failed: (?P<table>\w+)\.(?P<column>\w+)",
        ]
        UNIQUE_RES = [re.compile(dedent(s).strip()) for s in unique_re_strs]
        NOT_NULL_RES = [re.compile(dedent(s).strip()) for s in not_null_re_strs]
    for regex in UNIQUE_RES:
        match = regex.search(message)
        if match:
            column = match.group("column")
            try:
                value = match.group("value")
{'fraction_non_alphanumeric': 0.12605514912774338, 'fraction_numerical': 0.0011254924029262803, 'mean_word_length': 2.4615384615384617, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, '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': '393128', 'n_tokens_mistral': 1150, 'n_tokens_neox': 1118, 'n_words': 291}