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

from os.path import expanduser
import re

import oursql

from .user import User
from .wikiproject import WikiProject

__all__ = ["Bot"]

class Bot:
    """Represents an instance of the Reports bot on a particular wiki."""

    def __init__(self, config, project, lang):
        self._config = config
        self._project = project
        self._lang = lang

        self._site = None
        self._wikidb = None
        self._localdb = None

    def _sql_connect(self, **kwargs):
        """Return a new SQL connection using the given arguments.

        We apply some transformations: a default file is configured if not
        username or password is provided, a default charset is set, and
        autocommit is turned off.
        """
        if ("read_default_file" not in kwargs and "user" not in kwargs
                and "password" not in kwargs):
            kwargs["read_default_file"] = expanduser("~/.my.cnf")
        if "charset" not in kwargs:
            kwargs["charset"] = "utf8"
        if "autoping" not in kwargs:
            kwargs["autoping"] = True
        if "autoreconnect" not in kwargs:
            kwargs["autoreconnect"] = True

        return oursql.connect(**kwargs)<commit_msg>Workaround for oursql bug regarding utf8mb4 charset; pretend it's utf8.<commit_after># -*- coding: utf-8 -*-

import encodings
from os.path import expanduser
import re

import oursql

from .user import User
from .wikiproject import WikiProject

__all__ = ["Bot"]

class Bot:
    """Represents an instance of the Reports bot on a particular wiki."""

    def __init__(self, config, project, lang):
        self._config = config
        self._project = project
        self._lang = lang

        self._site = None
        self._wikidb = None
        self._localdb = None

    @staticmethod
    def _register_utf8mb4():
        """Install utf8mb4 as a valid alias for utf_8."""
        if "utf8mb4" not in encodings.aliases.aliases:
            encodings.aliases.aliases["utf8mb4"] = "utf_8"
        if hasattr(encodings, "_cache") and "utf8mb4" in encodings._cache:
            del encodings._cache["utf8mb4"]

    def _sql_connect(self, **kwargs):
        """Return a new SQL connection using the given arguments.

        We apply some transformations: a default file is configured if not
        username or password is provided, a default charset is set, and
        autocommit is turned off.

        The default charset really should be utf8mb4, but because of a bug in
        oursql's handling of charsets (conflating Python codecs with MySQL
        charsets, which are sometimes different), we cannot. Instead, if
        utf8mb4 is chosen, we'll install it as an alias first.
        """
        if ("read_default_file" not in kwargs and "user" not in kwargs
                and "password" not in kwargs):
            kwargs["read_default_file"] = expanduser("~/.my.cnf")
        if "charset" not in kwargs:
            kwargs["charset"] = "utf8mb4"
        if "autoping" not in kwargs:
            kwargs["autoping"] = True
        if "autoreconnect" not in kwargs:
            kwargs["autoreconnect"] = True

        if kwargs["charset"] == "utf8mb4":
            self._register_utf8mb4()

        return oursql.connect(**kwargs)
{'fraction_non_alphanumeric': 0.09021871202916161, 'fraction_numerical': 0.009113001215066828, 'mean_word_length': 2.423076923076923, 'pattern_counts': {'":': 1, '<': 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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6093184', 'n_tokens_mistral': 937, 'n_tokens_neox': 910, 'n_words': 341}