<commit_before>import subprocess
import yaml
import json
import os.path


class YAMLConfig(object):
    _config_values = {}

    def __init__(self, filename, default_keys={}, strict_mode=False):
        self.filename = filename
        self.default_keys = default_keys
        self.strict_mode = strict_mode
        self._load_config()

    def _load_config(self):
        if not os.path.exists(self.filename):
            self._make_default_config()
        else:
            f = open(self.filename, 'r')
            self._config_values = yaml.load(f)
            f.close()
            self._validate_config()
        print("[Config] Config %s loaded!" % self.filename)

    def _save_config(self):
        f = open(self.filename, "w")
        yaml.dump(self._config_values, f, indent=1)
        f.close()

    def _make_default_config(self):
        try:
            os.makedirs(os.path.dirname(self.filename))
        except:
            pass
        f = open(self.filename, "w")
        yaml.dump(self.default_keys, f, indent=1)
        f.close()
        print("[Config] Default config for %s created." % self.filename)
        self._load_config()

    def _validate_config(self):
        for key, value in self.default_keys.iteritems():
            if key not in self._config_values:
                self._config_values[key] = value
                print("[Config] Added new default %s for config %s" % (key, self.filename))
        if self.strict_mode:
            for key in self._config_values.keys():
                if key not in self.default_keys:
                    del self._config_values[key]
                    print("[Config] Deleted invlid key %s for config %s" % (key, self.filename))
                else:
                    if self._config_values[key] is None:
                        self._config_values[key] = self.default_keys[key]
                        print("[Config] Resetting invalid key type for %s in config %s." % (key, self.filename))
        self._save_config()

    def get_key(self, key):
        if key not in self._config_values:
            raise KeyError
        if isinstance(self._config_values[key], unicode):
            return self._config_values[key].encode('utf-8')
        else:
            return self._config_values[key]

    def set_key(self, key, value):
        self._config_values[key] = value
        self._save_config()

    def key_exists(self, key):
        if key in self._config_values:
            return True
        else:
            return False


banList = []

globalConfig = YAMLConfig("cfg/pso2proxy.config.yml",
                          {'packetLogging': False, 'myIpAddr': "0.0.0.0", 'bindIp': "0.0.0.0", 'blockNameMode': 0,
                           'noisy': False, 'admins': [], 'enabledShips': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'commandPrefix': '!'}, True)

blockNames = {}

proxy_ver = subprocess.check_output(["git", "describe", "--always"])


def is_admin(sega_id):
    if sega_id in globalConfig.get_key('admins'):
        return True
    else:
        return False


def load_block_names():
    global blockNames
    if os.path.exists("cfg/blocknames.resources.json") and globalConfig.get_key('blockNameMode') == 1:
        f = open("cfg/blocknames.resources.json", 'r')
        blockNames = json.load(f)<commit_msg>Kill newline<commit_after>import subprocess
import yaml
import json
import os.path


class YAMLConfig(object):
    _config_values = {}

    def __init__(self, filename, default_keys={}, strict_mode=False):
        self.filename = filename
        self.default_keys = default_keys
        self.strict_mode = strict_mode
        self._load_config()

    def _load_config(self):
        if not os.path.exists(self.filename):
            self._make_default_config()
        else:
            f = open(self.filename, 'r')
            self._config_values = yaml.load(f)
            f.close()
            self._validate_config()
        print("[Config] Config %s loaded!" % self.filename)

    def _save_config(self):
        f = open(self.filename, "w")
        yaml.dump(self._config_values, f, indent=1)
        f.close()

    def _make_default_config(self):
        try:
            os.makedirs(os.path.dirname(self.filename))
        except:
            pass
        f = open(self.filename, "w")
        yaml.dump(self.default_keys, f, indent=1)
        f.close()
        print("[Config] Default config for %s created." % self.filename)
        self._load_config()

    def _validate_config(self):
        for key, value in self.default_keys.iteritems():
            if key not in self._config_values:
                self._config_values[key] = value
                print("[Config] Added new default %s for config %s" % (key, self.filename))
        if self.strict_mode:
            for key in self._config_values.keys():
                if key not in self.default_keys:
                    del self._config_values[key]
                    print("[Config] Deleted invlid key %s for config %s" % (key, self.filename))
                else:
                    if self._config_values[key] is None:
                        self._config_values[key] = self.default_keys[key]
                        print("[Config] Resetting invalid key type for %s in config %s." % (key, self.filename))
        self._save_config()

    def get_key(self, key):
        if key not in self._config_values:
            raise KeyError
        if isinstance(self._config_values[key], unicode):
            return self._config_values[key].encode('utf-8')
        else:
            return self._config_values[key]

    def set_key(self, key, value):
        self._config_values[key] = value
        self._save_config()

    def key_exists(self, key):
        if key in self._config_values:
            return True
        else:
            return False


banList = []

globalConfig = YAMLConfig("cfg/pso2proxy.config.yml",
                          {'packetLogging': False, 'myIpAddr': "0.0.0.0", 'bindIp': "0.0.0.0", 'blockNameMode': 0,
                           'noisy': False, 'admins': [], 'enabledShips': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'commandPrefix': '!'}, True)

blockNames = {}

proxy_ver = subprocess.check_output(["git", "describe", "--always"]).rstrip("\n")


def is_admin(sega_id):
    if sega_id in globalConfig.get_key('admins'):
        return True
    else:
        return False


def load_block_names():
    global blockNames
    if os.path.exists("cfg/blocknames.resources.json") and globalConfig.get_key('blockNameMode') == 1:
        f = open("cfg/blocknames.resources.json", 'r')
        blockNames = json.load(f)
{'fraction_non_alphanumeric': 0.1208358570563295, 'fraction_numerical': 0.0072683222289521504, 'mean_word_length': 2.1362773029439697, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, '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': '6151329', 'n_tokens_mistral': 2034, 'n_tokens_neox': 1953, 'n_words': 466}