<commit_before>/*
 * Copyright 2017 DevJake
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.salt.config

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import me.salt.config.entities.Configuration
import me.salt.events.ConfigInteractEvent
import me.salt.events.fireEvent
import me.salt.exception.ConfigMissingValueException
import me.salt.logging.logWarn
import me.salt.objects.Interaction
import me.salt.util.GenUtil
import me.salt.objects.isEmpty
import java.io.File

object ConfigHandler {
    private val mapper = ObjectMapper(YAMLFactory()
            .enable(YAMLGenerator.Feature.MINIMIZE_QUOTES)
            .enable(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE)
            .enable(YAMLGenerator.Feature.SPLIT_LINES)
            .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER))
            .setSerializationInclusion(JsonInclude.Include.NON_ABSENT)

    private fun getFile(handler: Handler): File = File(File(GenUtil.saltResourceDir, "\\Data"), handler.expectedPath)

    fun writeConfig(handler: Handler, conf: Configuration) {
        val f = getFile(handler)
        GenUtil.createFile(f)
        if (f.exists() && f.isEmpty()) {
            mapper.writeValue(f, conf)
            fireEvent(ConfigInteractEvent(handler, conf, Interaction.WRITE))
        }
    }

    fun overwriteConfig(handler: Handler, conf: Configuration) {
        val f = getFile(handler)
        if (f.exists()) {
            mapper.writeValue(f, conf)
            fireEvent(ConfigInteractEvent(handler, conf, Interaction.WRITE))
        } else writeConfig(handler, conf)
    }

    fun <T : Configuration> readConfig(handler: Handler, type: Class<T>): T? = try {
        ObjectMapper(YAMLFactory()).registerKotlinModule()
                .enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)
                .readValue(getFile(handler), type)
    } catch (e: Exception) {
        null
    }
}<commit_msg>Updated exception catches and messages in ConfigHandler<commit_after>/*
 * Copyright 2017 DevJake
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.salt.config

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.core.JsonParseException
import com.fasterxml.jackson.databind.DeserializationFeature
import com.fasterxml.jackson.databind.JsonMappingException
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import me.salt.config.entities.Configuration
import me.salt.entities.Constants
import me.salt.events.ConfigInteractEvent
import me.salt.events.fireEvent
import me.salt.exception.ConfigMissingValueException
import me.salt.objects.Interaction
import me.salt.objects.isEmpty
import me.salt.util.GenUtil
import java.io.File
import java.io.IOException

object ConfigHandler {
    private val mapper = ObjectMapper(YAMLFactory()
            .enable(YAMLGenerator.Feature.MINIMIZE_QUOTES)
            .enable(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE)
            .enable(YAMLGenerator.Feature.SPLIT_LINES)
            .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER))
            .setSerializationInclusion(JsonInclude.Include.NON_ABSENT)

    private fun getFile(handler: Handler): File = File(File(GenUtil.saltResourceDir, Constants.CONFIG_DIR.value), handler.expectedPath)

    fun writeConfig(handler: Handler, conf: Configuration) {
        val f = getFile(handler)
        GenUtil.createFile(f)
        if (f.exists() && f.isEmpty()) {
            mapper.writeValue(f, conf)
            fireEvent(ConfigInteractEvent(handler, conf, Interaction.WRITE))
        }
    }

    fun overwriteConfig(handler: Handler, conf: Configuration) {
        val f = getFile(handler)
        if (f.exists()) {
            mapper.writeValue(f, conf)
            fireEvent(ConfigInteractEvent(handler, conf, Interaction.WRITE))
        } else writeConfig(handler, conf)
    }

    fun <T : Configuration> readConfig(handler: Handler, type: Class<T>): T? = try {
        ObjectMapper(YAMLFactory()).registerKotlinModule()
                .enable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)
                .readValue(getFile(handler), type)
    } catch (e: IOException) {
        throw ConfigMissingValueException(//TODO update exception names
                "The config could not be loaded... it likely does not exist, and must first be created!")
    } catch (e: JsonMappingException) {
        throw ConfigMissingValueException(
                "The config could not be mapped to the specified Handler instance... the Handler likely has a different structure to the parsed file!")
    } catch (e: JsonParseException) {
        throw ConfigMissingValueException(
                "The config could not be parsed... it's structure is likely malformed. Regenerate the file to fix structural issues.")
    }
}
{'fraction_non_alphanumeric': 0.08602986932171748, 'fraction_numerical': 0.002489110143123833, 'mean_word_length': 3.240765171503958, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 14}, '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': '5443527', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1703, 'n_words': 452}