<commit_before># -*- coding: utf-8 -*-

import json
import re
import django

from django.db.backends.postgresql_psycopg2.version import get_version
from django.core.serializers.json import DjangoJSONEncoder
from django.db import models
from django import forms
from django.utils import six

import psycopg2
import psycopg2.extensions
import psycopg2.extras


class JsonAdapter(psycopg2.extras.Json):

    def __init__(self, *args, **kwargs):
        self.indent = kwargs.pop('indent', None)
        self.sort_keys = kwargs.pop('sort_keys', False)
        super(JsonAdapter, self).__init__(*args, **kwargs)


    def dumps(self, obj):
        return json.dumps(obj, cls=DjangoJSONEncoder)


psycopg2.extensions.register_adapter(dict, JsonAdapter)
psycopg2.extras.register_default_json(loads=json.loads)

# so that psycopg2 knows also to convert jsonb fields correctly
# http://schinckel.net/2014/05/24/python,-postgres-and-jsonb/
psycopg2.extras.register_json(loads=json.loads, oid=3802, array_oid=3807)


class JsonField(six.with_metaclass(models.SubfieldBase, models.Field)):
    empty_strings_allowed = False

    def __init__(self, *args, **kwargs):
        self.indent = kwargs.pop('indent', None)
        self.sort_keys = kwargs.pop('sort_keys', False)
        super(JsonField, self).__init__(*args, **kwargs)

    def db_type(self, connection):
        if get_version(connection) < 90200:
            raise RuntimeError("django_pgjson does not supports postgresql version < 9.2")
        return "json"

    def value_to_string(self, obj):
        value = self._get_val_from_obj(obj)
        return json.dumps(self.get_prep_value(value), cls=DjangoJSONEncoder,
                          indent=self.indent, sort_keys=self.sort_keys)

    def get_default(self):
        if self.has_default():
            if callable(self.default):
                return self.default()
            return self.default

        return None

    def to_python(self, value):
        if isinstance(value, six.string_types):
            try:
                value = json.loads(value)
            except ValueError:
                pass
        return value

    def formfield(self, **kwargs):
        defaults = {'form_class': jsonFormField(indent_=self.indent,
                                                sort_keys_=self.sort_keys)}
        defaults.update(kwargs)
        return super(JsonField, self).formfield(**defaults)

    def get_db_prep_value(self, value, connection, prepared=False):
        value = super(JsonField, self).get_db_prep_value(value, connection, prepared=prepared)
        if self.null and value is None:
            return None
        return JsonAdapter(value)

    if django.get_version() >= '1.7':
        def get_transform(self, name):
            from .lookups import KeyTransformFactory

            transform = super(JsonField, self).get_transform(name)
            if transform:
                return transform

            if not re.match("at_\w+", name):
                return None

            _, key = name.split("_", 1)
            return KeyTransformFactory(key, self)


class JsonBField(JsonField):
    def db_type(self, connection):
        if get_version(connection) < 90400:
            raise RuntimeError("django_pgjson: PostgreSQL >= 9.4 is required for jsonb support.")
        return "jsonb"

    def get_prep_lookup(self, lookup_type, value, prepared=False):
        """ Cleanup value for the jsonb lookup types

        contains requires json encoded string
        has_any and has_all require array of string_types
        has requires string, but we can easily convert int to string

        """
        if lookup_type in ['jcontains']:
            if not isinstance(value, six.string_types):
                value = json.dumps(value, cls=DjangoJSONEncoder,
                                   indent=self.indent, sort_keys=self.sort_keys)
        if lookup_type in ['jhas_any', 'jhas_all']:
            if isinstance(value, six.string_types):
                value = [value]
            # Quickly coerce the following:
            #   any iterable to array
            #   non-string values to strings
            value = ['%s' % v for v in value]
        elif lookup_type in ['jhas'] and not isinstance(value, six.string_types):
            if isinstance(value, six.integer_types):
                value = str(value)
            else:
                raise TypeError('jhas lookup requires str or int')
        return value


if django.get_version() >= '1.7':
    from .lookups import ExactLookup
    from .lookups import (ArrayLengthLookup, JsonBArrayLengthLookup, JsonBContainsLookup,
                          JsonBHasLookup, JsonBHasAnyLookup, JsonBHasAllLookup)

    JsonField.register_lookup(ExactLookup)
    JsonField.register_lookup(ArrayLengthLookup)

    JsonBField.register_lookup(ExactLookup)
    JsonBField.register_lookup(JsonBArrayLengthLookup)
    JsonBField.register_lookup(JsonBContainsLookup)
    JsonBField.register_lookup(JsonBHasLookup)
    JsonBField.register_lookup(JsonBHasAnyLookup)
    JsonBField.register_lookup(JsonBHasAllLookup)


# return a class of JsonFormField

def jsonFormField(indent_=None, sort_keys_=False):
    class JsonFormField(forms.CharField):

        indent = indent_
        sort_keys = sort_keys_

        widget = forms.Textarea

        def prepare_value(self, value):
            if isinstance(value, six.string_types):
                return value
            return json.dumps(value, cls=DjangoJSONEncoder,
                              indent=self.indent, sort_keys=self.sort_keys)
    return JsonFormField


# South compatibility
try:
    from south.modelsinspector import add_introspection_rules
    add_introspection_rules([(
        (JsonField,),
        [],
        {
            'blank': ['blank', { 'default': True }],
            'null': ['null', { 'default': True }],
        },
    )], (r'^django_pgjson\.fields\.JsonField',))

    add_introspection_rules([(
        (JsonBField,),
        [],
        {
        'blank': ['blank', { 'default': True }],
        'null': ['null', { 'default': True }],
        },
    )], (r'^django_pgjson\.fields\.JsonBField',))

except ImportError:
    pass
<commit_msg>removed unneeded trailing underscores on variable names<commit_after># -*- coding: utf-8 -*-

import json
import re
import django

from django.db.backends.postgresql_psycopg2.version import get_version
from django.core.serializers.json import DjangoJSONEncoder
from django.db import models
from django import forms
from django.utils import six

import psycopg2
import psycopg2.extensions
import psycopg2.extras


class JsonAdapter(psycopg2.extras.Json):

    def __init__(self, *args, **kwargs):
        self.indent = kwargs.pop('indent', None)
        self.sort_keys = kwargs.pop('sort_keys', False)
        super(JsonAdapter, self).__init__(*args, **kwargs)


    def dumps(self, obj):
        return json.dumps(obj, cls=DjangoJSONEncoder)


psycopg2.extensions.register_adapter(dict, JsonAdapter)
psycopg2.extras.register_default_json(loads=json.loads)

# so that psycopg2 knows also to convert jsonb fields correctly
# http://schinckel.net/2014/05/24/python,-postgres-and-jsonb/
psycopg2.extras.register_json(loads=json.loads, oid=3802, array_oid=3807)


class JsonField(six.with_metaclass(models.SubfieldBase, models.Field)):
    empty_strings_allowed = False

    def __init__(self, *args, **kwargs):
        self.indent = kwargs.pop('indent', None)
        self.sort_keys = kwargs.pop('sort_keys', False)
        super(JsonField, self).__init__(*args, **kwargs)

    def db_type(self, connection):
        if get_version(connection) < 90200:
            raise RuntimeError("django_pgjson does not supports postgresql version < 9.2")
        return "json"

    def value_to_string(self, obj):
        value = self._get_val_from_obj(obj)
        return json.dumps(self.get_prep_value(value), cls=DjangoJSONEncoder,
                          indent=self.indent, sort_keys=self.sort_keys)

    def get_default(self):
        if self.has_default():
            if callable(self.default):
                return self.default()
            return self.default

        return None

    def to_python(self, value):
        if isinstance(value, six.string_types):
            try:
                value = json.loads(value)
            except ValueError:
                pass
        return value

    def formfield(self, **kwargs):
        defaults = {'form_class': jsonFormField(indent=self.indent,
                                                sort_keys=self.sort_keys)}
        defaults.update(kwargs)
        return super(JsonField, self).formfield(**defaults)

    def get_db_prep_value(self, value, connection, prepared=False):
        value = super(JsonField, self).get_db_prep_value(value, connection, prepared=prepared)
        if self.null and value is None:
            return None
        return JsonAdapter(value)

    if django.get_version() >= '1.7':
        def get_transform(self, name):
            from .lookups import KeyTransformFactory

            transform = super(JsonField, self).get_transform(name)
            if transform:
                return transform

            if not re.match("at_\w+", name):
                return None

            _, key = name.split("_", 1)
            return KeyTransformFactory(key, self)


class JsonBField(JsonField):
    def db_type(self, connection):
        if get_version(connection) < 90400:
            raise RuntimeError("django_pgjson: PostgreSQL >= 9.4 is required for jsonb support.")
        return "jsonb"

    def get_prep_lookup(self, lookup_type, value, prepared=False):
        """ Cleanup value for the jsonb lookup types

        contains requires json encoded string
        has_any and has_all require array of string_types
        has requires string, but we can easily convert int to string

        """
        if lookup_type in ['jcontains']:
            if not isinstance(value, six.string_types):
                value = json.dumps(value, cls=DjangoJSONEncoder,
                                   indent=self.indent, sort_keys=self.sort_keys)
        if lookup_type in ['jhas_any', 'jhas_all']:
            if isinstance(value, six.string_types):
                value = [value]
            # Quickly coerce the following:
            #   any iterable to array
            #   non-string values to strings
            value = ['%s' % v for v in value]
        elif lookup_type in ['jhas'] and not isinstance(value, six.string_types):
            if isinstance(value, six.integer_types):
                value = str(value)
            else:
                raise TypeError('jhas lookup requires str or int')
        return value


if django.get_version() >= '1.7':
    from .lookups import ExactLookup
    from .lookups import (ArrayLengthLookup, JsonBArrayLengthLookup, JsonBContainsLookup,
                          JsonBHasLookup, JsonBHasAnyLookup, JsonBHasAllLookup)

    JsonField.register_lookup(ExactLookup)
    JsonField.register_lookup(ArrayLengthLookup)

    JsonBField.register_lookup(ExactLookup)
    JsonBField.register_lookup(JsonBArrayLengthLookup)
    JsonBField.register_lookup(JsonBContainsLookup)
    JsonBField.register_lookup(JsonBHasLookup)
    JsonBField.register_lookup(JsonBHasAnyLookup)
    JsonBField.register_lookup(JsonBHasAllLookup)


# return a class of JsonFormField

def jsonFormField(indent=None, sort_keys=False):
    class JsonFormField(forms.CharField):

        indent = indent
        sort_keys = sort_keys

        widget = forms.Textarea

        def prepare_value(self, value):
            if isinstance(value, six.string_types):
                return value
            return json.dumps(value, cls=DjangoJSONEncoder,
                              indent=self.indent, sort_keys=self.sort_keys)
    return JsonFormField


# South compatibility
try:
    from south.modelsinspector import add_introspection_rules
    add_introspection_rules([(
        (JsonField,),
        [],
        {
            'blank': ['blank', { 'default': True }],
            'null': ['null', { 'default': True }],
        },
    )], (r'^django_pgjson\.fields\.JsonField',))

    add_introspection_rules([(
        (JsonBField,),
        [],
        {
        'blank': ['blank', { 'default': True }],
        'null': ['null', { 'default': True }],
        },
    )], (r'^django_pgjson\.fields\.JsonBField',))

except ImportError:
    pass

{'fraction_non_alphanumeric': 0.09812108559498957, 'fraction_numerical': 0.007226593865424763, 'mean_word_length': 2.5555238367113904, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4476220', 'n_tokens_mistral': 3651, 'n_tokens_neox': 3525, 'n_words': 824}