<commit_before>from configparser import ConfigParser
import os
import re

from qldsmanager import app_dir
from qldsmanager.util.matheval import eval_expr


class AbstractConfig:
    def __init__(self):
        self.__config_dir = os.path.expanduser('~/.qldsmanager/') #has to end with /
        self.filename = None
        self.filepath = None
        self.required = None

        self._configure()
        self.pre_parse()

        self.parser = self.__get_parser(self.filename)

        if self.required:
            self.__check_missing(self.required)

        self.extra_check()

    def __get_parser(self, filename):
        parser = ConfigParser()

        if os.path.isfile(app_dir + filename):
            parser.read_file(open(app_dir + filename))

        parser.read(os.path.expanduser(self.__config_dir + filename))

        return parser

    def __check_missing(self, options: dict):
        missing = self._has_missing(self.parser.sections(), options.keys())
        if missing:
            print('Missing sections in configuration: %s' % ', '.join(missing))
            exit(10)

        missing_options = dict()
        for section,values in options.items():
            missing = self._has_missing(self.parser.options(section), values)
            if missing:
                missing_options[section] = missing

        if missing_options:
            print('Missing options in sections\n  %s' % '\n  '.join([
                              '%s: %s' % (key, value) for (key, value) in missing_options.items()
                              ]))
            exit(11)

    def _has_missing(self, data, required_keys):
        missing = []
        for k in required_keys:
            if k not in data:
                missing.append(k)

        return missing

    def update(self):
        config_file = self.__config_dir + self.filename

        if not os.path.isdir(self.__config_dir):
            os.mkdir(self.__config_dir)

        if os.path.isfile(config_file) and not os.access(config_file, os.W_OK):
            raise IOError('Cannot write to file %s' % config_file)

        with (open(config_file, 'w+')) as config_fp:
            self.parser.write(config_fp)

    def set(self, section: str, option: str, value):
        return self.parser.set(section, option, value)

    def get(self, section, option):
        try:
            return self.parser.get(section, option)
        except:
            return None

    def get_config_dir(self):
        return self.__config_dir

    def extra_check(self):
        return True

    def pre_parse(self):
        return True

    def _configure(self):
        return True


class Configuration(AbstractConfig):
    def _configure(self):
        self.filename = 'config'
        self.required = dict(
            dir=['ql', 'steamcmd'],
            config=['servers']
        )


class ServerConfig(AbstractConfig):
    def __init__(self):
        self.config = Configuration()
        self.extra_required = ['net_port']

        self.servers = {}
        self.parameters = {}
        self.defaults = {}
        self.extra = {}
        self.loop = {}

        self.servers_file = os.path.expanduser(self.config.get('config', 'servers'))

        super(ServerConfig, self).__init__(self)

    def pre_parse(self):
        servers_file_path = os.path.dirname(self.servers_file)
        self.filename = self.servers_file

        if not os.path.exists(servers_file_path):
            os.makedirs(servers_file_path)
<commit_msg>Remove additional argument<commit_after>from configparser import ConfigParser
import os
import re

from qldsmanager import app_dir
from qldsmanager.util.matheval import eval_expr


class AbstractConfig:
    def __init__(self):
        self.__config_dir = os.path.expanduser('~/.qldsmanager/') #has to end with /
        self.filename = None
        self.filepath = None
        self.required = None

        self._configure()
        self.pre_parse()

        self.parser = self.__get_parser(self.filename)

        if self.required:
            self.__check_missing(self.required)

        self.extra_check()

    def __get_parser(self, filename):
        parser = ConfigParser()

        if os.path.isfile(app_dir + filename):
            parser.read_file(open(app_dir + filename))

        parser.read(os.path.expanduser(self.__config_dir + filename))

        return parser

    def __check_missing(self, options: dict):
        missing = self._has_missing(self.parser.sections(), options.keys())
        if missing:
            print('Missing sections in configuration: %s' % ', '.join(missing))
            exit(10)

        missing_options = dict()
        for section,values in options.items():
            missing = self._has_missing(self.parser.options(section), values)
            if missing:
                missing_options[section] = missing

        if missing_options:
            print('Missing options in sections\n  %s' % '\n  '.join([
                              '%s: %s' % (key, value) for (key, value) in missing_options.items()
                              ]))
            exit(11)

    def _has_missing(self, data, required_keys):
        missing = []
        for k in required_keys:
            if k not in data:
                missing.append(k)

        return missing

    def update(self):
        config_file = self.__config_dir + self.filename

        if not os.path.isdir(self.__config_dir):
            os.mkdir(self.__config_dir)

        if os.path.isfile(config_file) and not os.access(config_file, os.W_OK):
            raise IOError('Cannot write to file %s' % config_file)

        with (open(config_file, 'w+')) as config_fp:
            self.parser.write(config_fp)

    def set(self, section: str, option: str, value):
        return self.parser.set(section, option, value)

    def get(self, section, option):
        try:
            return self.parser.get(section, option)
        except:
            return None

    def get_config_dir(self):
        return self.__config_dir

    def extra_check(self):
        return True

    def pre_parse(self):
        return True

    def _configure(self):
        return True


class Configuration(AbstractConfig):
    def _configure(self):
        self.filename = 'config'
        self.required = dict(
            dir=['ql', 'steamcmd'],
            config=['servers']
        )


class ServerConfig(AbstractConfig):
    def __init__(self):
        self.config = Configuration()
        self.extra_required = ['net_port']

        self.servers = {}
        self.parameters = {}
        self.defaults = {}
        self.extra = {}
        self.loop = {}

        self.servers_file = os.path.expanduser(self.config.get('config', 'servers'))

        super(ServerConfig, self).__init__()

    def pre_parse(self):
        servers_file_path = os.path.dirname(self.servers_file)
        self.filename = self.servers_file

        if not os.path.exists(servers_file_path):
            os.makedirs(servers_file_path)

{'fraction_non_alphanumeric': 0.10521801286633309, 'fraction_numerical': 0.0011436740528949249, 'mean_word_length': 2.1358135365307036, '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': '2197808', 'n_tokens_mistral': 2022, 'n_tokens_neox': 1967, 'n_words': 450}