<commit_before>from UM.Signal import Signal, SignalEmitter
from UM.Logger import Logger

import os
import configparser

class Preferences(SignalEmitter):
    def __init__(self):
        super().__init__()

        self._file = None
        self._parser = None
        self._preferences = {}
    
    def addPreference(self, key, default_value):
        preference = self._findPreference(key)
        if preference:
            Logger.log('w', 'Preference %s already exists', key)
            return

        group, key = self._splitKey(key)

        if group not in self._preferences:
            self._preferences[group] = {}

        self._preferences[group][key] = _Preference(key, default_value)

    def setValue(self, key, value):
        preference = self._findPreference(key)

        if preference:
            preference.setValue(value)
            self.preferenceChanged.emit(key)
    
    def getValue(self, key):
        preference = self._findPreference(key)

        if preference:
            return preference.getValue()

        return None

    def resetPreference(self, key):
        preference = self._findPreference(key)

        if preference:
            preference.setValue(preference.getDefault())
            self.preferenceChanged.emit(key)

    def readFromFile(self, file):
        self._loadFile(file)

        for group, group_entries in self._parser.items():
            if group == 'DEFAULT':
                continue

            if not group in self._preferences:
                Logger.log('w', "Unknown preference group %s", group)
                continue

            for key, value in group_entries.items():
                if not key in self._preferences[group]:
                    Logger.log('w', "Unknown preference %s", key)
                    continue

                self._preferences[group][key].setValue(value)

    def readPreferenceFromFile(self, key, file):
        preference = self._findPreference(key)
        if not preference:
            return

        self._loadFile(file)
        group, key = self._splitKey(key)

        if group in self._parser:
            if key in self._parser[group]:
                preference.setValue(self._parser[group][key])

    def writeToFile(self, file):
        parser = configparser.ConfigParser()
        for group, group_entries in self._preferences.items():
            parser[group] = {}
            for key, pref in group_entries.items():
                if pref.getValue() != pref.getDefault():
                    parser[group][key] = pref.getValue()

        parser['general']['version'] = '1'

        with open(file, 'wt') as f:
            parser.write(f)

    preferenceChanged = Signal()

    @classmethod
    def getInstance(cls):
        if not cls._instance:
            cls._instance = Preferences()
<commit_msg>Make sure preference values are stored as string<commit_after>from UM.Signal import Signal, SignalEmitter
from UM.Logger import Logger

import os
import configparser

class Preferences(SignalEmitter):
    def __init__(self):
        super().__init__()

        self._file = None
        self._parser = None
        self._preferences = {}
    
    def addPreference(self, key, default_value):
        preference = self._findPreference(key)
        if preference:
            Logger.log('w', 'Preference %s already exists', key)
            return

        group, key = self._splitKey(key)

        if group not in self._preferences:
            self._preferences[group] = {}

        self._preferences[group][key] = _Preference(key, default_value)

    def setValue(self, key, value):
        preference = self._findPreference(key)

        if preference:
            preference.setValue(value)
            self.preferenceChanged.emit(key)
    
    def getValue(self, key):
        preference = self._findPreference(key)

        if preference:
            return preference.getValue()

        return None

    def resetPreference(self, key):
        preference = self._findPreference(key)

        if preference:
            preference.setValue(preference.getDefault())
            self.preferenceChanged.emit(key)

    def readFromFile(self, file):
        self._loadFile(file)

        for group, group_entries in self._parser.items():
            if group == 'DEFAULT':
                continue

            if not group in self._preferences:
                Logger.log('w', "Unknown preference group %s", group)
                continue

            for key, value in group_entries.items():
                if not key in self._preferences[group]:
                    Logger.log('w', "Unknown preference %s", key)
                    continue

                self._preferences[group][key].setValue(value)

    def readPreferenceFromFile(self, key, file):
        preference = self._findPreference(key)
        if not preference:
            return

        self._loadFile(file)
        group, key = self._splitKey(key)

        if group in self._parser:
            if key in self._parser[group]:
                preference.setValue(self._parser[group][key])

    def writeToFile(self, file):
        parser = configparser.ConfigParser()
        for group, group_entries in self._preferences.items():
            parser[group] = {}
            for key, pref in group_entries.items():
                if pref.getValue() != pref.getDefault():
                    parser[group][key] = str(pref.getValue())

        parser['general']['version'] = '1'

        with open(file, 'wt') as f:
            parser.write(f)

    preferenceChanged = Signal()

    @classmethod
    def getInstance(cls):
        if not cls._instance:
            cls._instance = Preferences()

{'fraction_non_alphanumeric': 0.09799789251844046, 'fraction_numerical': 0.00035124692658939234, 'mean_word_length': 2.029255319148936, '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': '574049', 'n_tokens_mistral': 1514, 'n_tokens_neox': 1480, 'n_words': 375}