<commit_before>import functools
import re

from six.moves import configparser
from leapp.snactor.utils import find_project_basedir
import os


_LEAPP_CONFIG = None
_CONFIG_DEFAULTS = {
    'database': {
        'path': '/var/lib/leapp/leapp.db',
    },
    'repositories': {
        'global_repo_path': '.',
        'custom_repo_path': None,
    },
}


class BetterConfigParser(configparser.ConfigParser):
    _EINTERPOL = re.compile(r"\$\{([^}]+)\}")

    def get(self, section, *args, **kwargs):
        depth = kwargs.pop('depth', configparser.MAX_INTERPOLATION_DEPTH)
        return self._resolve(BetterConfigParser.__bases__[0].get(self, section, *args, **kwargs),
                             depth=depth,
                             section=section)

    def _resolve(self, value, depth, section):
        if depth:
            if '${' not in value:
                return value
            replacer = functools.partial(self._sub_interpol, depth=depth, section=section)
            value = BetterConfigParser._EINTERPOL.sub(replacer, value)
        return value

    def _sub_interpol(self, match, depth, section):
        s = match.group(1)
        if not s:
            return match.group()
        parts = s.split(':')
        if len(parts) == 1:
            parts.insert(0, section)
        return self.get(*parts, depth=depth - 1)


def get_config():
    global _LEAPP_CONFIG
    if not _LEAPP_CONFIG:
        project_defaults = {}
        if find_project_basedir('.'):
            project_defaults['project'] = {
                'root_dir': find_project_basedir('.'),
                'state_dir': '${root_dir}/.leapp',
            }
        _LEAPP_CONFIG = BetterConfigParser()
        for section, values in _CONFIG_DEFAULTS.items() + project_defaults.items():
            if not _LEAPP_CONFIG.has_section(section):
                _LEAPP_CONFIG.add_section(section)
            for name, value in values.items():
                _LEAPP_CONFIG.set(section, name, value)
        _LEAPP_CONFIG.read([os.getenv('LEAPP_CONFIG', '/etc/leapp/leapp.conf')])

    return _LEAPP_CONFIG<commit_msg>Fix config - dict.items in Python3 can't be joined by +<commit_after>import functools
import re

from six.moves import configparser
from leapp.snactor.utils import find_project_basedir
import os


_LEAPP_CONFIG = None
_CONFIG_DEFAULTS = {
    'database': {
        'path': '/var/lib/leapp/leapp.db',
    },
    'repositories': {
        'global_repo_path': '.',
        'custom_repo_path': None,
    },
}


class BetterConfigParser(configparser.ConfigParser):
    _EINTERPOL = re.compile(r"\$\{([^}]+)\}")

    def get(self, section, *args, **kwargs):
        depth = kwargs.pop('depth', configparser.MAX_INTERPOLATION_DEPTH)
        return self._resolve(BetterConfigParser.__bases__[0].get(self, section, *args, **kwargs),
                             depth=depth,
                             section=section)

    def _resolve(self, value, depth, section):
        if depth:
            if '${' not in value:
                return value
            replacer = functools.partial(self._sub_interpol, depth=depth, section=section)
            value = BetterConfigParser._EINTERPOL.sub(replacer, value)
        return value

    def _sub_interpol(self, match, depth, section):
        s = match.group(1)
        if not s:
            return match.group()
        parts = s.split(':')
        if len(parts) == 1:
            parts.insert(0, section)
        return self.get(*parts, depth=depth - 1)


def get_config():
    global _LEAPP_CONFIG
    if not _LEAPP_CONFIG:
        project_defaults = {}
        if find_project_basedir('.'):
            project_defaults['project'] = {
                'root_dir': find_project_basedir('.'),
                'state_dir': '${root_dir}/.leapp',
            }
        _LEAPP_CONFIG = BetterConfigParser()
        for section, values in tuple(_CONFIG_DEFAULTS.items()) + tuple(project_defaults.items()):
            if not _LEAPP_CONFIG.has_section(section):
                _LEAPP_CONFIG.add_section(section)
            for name, value in values.items():
                _LEAPP_CONFIG.set(section, name, value)
        _LEAPP_CONFIG.read([os.getenv('LEAPP_CONFIG', '/etc/leapp/leapp.conf')])

    return _LEAPP_CONFIG
{'fraction_non_alphanumeric': 0.12060065696855936, 'fraction_numerical': 0.002580947911778508, 'mean_word_length': 2.3330727130570756, 'pattern_counts': {'":': 0, '<': 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': '1404940', 'n_tokens_mistral': 1304, 'n_tokens_neox': 1280, 'n_words': 262}