<commit_before>from collections import MutableMapping, OrderedDict, ChainMap

import itertools

import re
import json

__all__ = ['ParseError',
           'InvalidSectionNameError', 'InvalidOptionNameError',
           'NoSectionError', 'NoOptionError',
           'DuplicateSectionError', 'DuplicateOptionError',
           'JSONConfigParser']

DEFAULT_SECT = 'DEFAULT'
_UNSET = object()

_JSON_ERROR_TMPL = r"""
    ^
    (?P<message> .*):                           # message
    \ line\ (?P<lineno> [0-9]+)                 # line number
    \ column\ (?P<column> [0-9]+)               # column
    (\ -\ line\ [0-9]+\ column\ [0-9]+\ -)?     # optional end of error
    \ \(char\ ([0-9]+)\)                        # index in string
    $
    """
_json_error_re = re.compile(_JSON_ERROR_TMPL, re.VERBOSE | re.MULTILINE)


class ParseError(ValueError):
    def __init__(self, message, source=_UNSET, index=_UNSET, *,
                 filename=None, section=None,
                 lineno=None, column=None,
                 line=None):
        super(ParseError, self).__init__(self)

        self.message = message
        self.filename = filename
        self.section = section

        if source is not _UNSET and index is not _UNSET:
            lineno, column, line = get_line(source, index)

        self.lineno = lineno
        self.column = column
        self.line = line

    def __str__(self):
        location = []
        if self.filename:
            location.append('File: %s' % repr(self.filename))
        if self.lineno is not None:
            location.append('Line: %i' % self.lineno)
        if self.section:
            location.append('in %s' % self.section)

        output = ', '.join(location)
        if output:
            output += '\n'

        if self.line:
            output += '  ' + self.line.strip() + '\n'

        output += '%s: %s' % (self.__class__.__name__, self.message)

        return output


class JSONError(ParseError):
    def __init__(self, error, source, index, *,
                 filename=None, section=None):
        mo = _json_error_re.match(error.args[0])
        if not mo:
            raise Exception("json exception did not match expected format")<commit_msg>make re stuff a proprty of JSONError class<commit_after>from collections import MutableMapping, OrderedDict, ChainMap

import itertools

import re
import json

__all__ = ['ParseError',
           'InvalidSectionNameError', 'InvalidOptionNameError',
           'NoSectionError', 'NoOptionError',
           'DuplicateSectionError', 'DuplicateOptionError',
           'JSONConfigParser']

DEFAULT_SECT = 'DEFAULT'
_UNSET = object()


class ParseError(ValueError):
    def __init__(self, message, source=_UNSET, index=_UNSET, *,
                 filename=None, section=None,
                 lineno=None, column=None,
                 line=None):
        super(ParseError, self).__init__(self)

        self.message = message
        self.filename = filename
        self.section = section

        if source is not _UNSET and index is not _UNSET:
            lineno, column, line = get_line(source, index)

        self.lineno = lineno
        self.column = column
        self.line = line

    def __str__(self):
        location = []
        if self.filename:
            location.append('File: %s' % repr(self.filename))
        if self.lineno is not None:
            location.append('Line: %i' % self.lineno)
        if self.section:
            location.append('in %s' % self.section)

        output = ', '.join(location)
        if output:
            output += '\n'

        if self.line:
            output += '  ' + self.line.strip() + '\n'

        output += '%s: %s' % (self.__class__.__name__, self.message)

        return output


class JSONError(ParseError):
    _JSON_ERROR_TMPL = r"""
        ^
        (?P<message> .*):                           # message
        \ line\ (?P<lineno> [0-9]+)                 # line number
        \ column\ (?P<column> [0-9]+)               # column
        (\ -\ line\ [0-9]+\ column\ [0-9]+\ -)?     # optional end of error
        \ \(char\ ([0-9]+)\)                        # index in string
        $
        """
    _json_error_re = re.compile(_JSON_ERROR_TMPL, re.VERBOSE | re.MULTILINE)

    def __init__(self, error, source, index, *,
                 filename=None, section=None):
        mo = self._json_error_re.match(error.args[0])
        if not mo:
            raise Exception("json exception did not match expected format")
{'fraction_non_alphanumeric': 0.12316910785619174, 'fraction_numerical': 0.004882379050155349, 'mean_word_length': 1.9209332469215814, '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': '765821', 'n_tokens_mistral': 1338, 'n_tokens_neox': 1306, 'n_words': 320}