<commit_before>/*
 * Copyright (c) 2015 <NAME> <<EMAIL>>
 *
 * This file is part of Ukase.
 *
 *  Ukase is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package com.github.ukase.toolkit;

import com.github.jknack.handlebars.io.AbstractTemplateLoader;
import com.github.jknack.handlebars.io.FileTemplateLoader;
import com.github.jknack.handlebars.io.StringTemplateSource;
import com.github.jknack.handlebars.io.TemplateLoader;
import com.github.jknack.handlebars.io.TemplateSource;
import com.github.ukase.config.UkaseSettings;
import com.github.ukase.toolkit.jar.ZipTemplateSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

@Component
public class CompoundTemplateLoader extends AbstractTemplateLoader {
    private static final String IMAGE_AS_PAGE = "default - image as page";
    private static final TemplateSource IMAGE_AS_PAGE_TEMPLATE;
    static {
        IMAGE_AS_PAGE_TEMPLATE =
                new StringTemplateSource(IMAGE_AS_PAGE, StaticUtils.readStringFile(getStream("image-as-page.hbs")));
    }

    private static InputStream getStream(String fileName) {
        return CompoundTemplateLoader.class.getResourceAsStream(fileName);
    }

    private final ZipFile zip;
    private final Map<String, ZipEntry> resources = new HashMap<>();
    private final TemplateLoader externalLoader;

    @Autowired
    public CompoundTemplateLoader(UkaseSettings settings) throws IOException {
        File templates = settings.getTemplates();
        externalLoader =  templates == null ? null : new FileTemplateLoader(templates);

        if (settings.getJar() == null) {
            zip = null;
            return;
        }

        zip = new ZipFile(settings.getJar());
        zip.stream().forEach(this::registerResource);
    }

    @Override
    public TemplateSource sourceAt(String location) throws IOException {
        if (IMAGE_AS_PAGE.equals(location)) {
            //TODO extract loader for default templates (if there were more than 1)
            return IMAGE_AS_PAGE_TEMPLATE;
        }
        try {
            if (externalLoader == null) {
                return getTemplateSource(location, null);<commit_msg>Log current config on startup<commit_after>/*
 * Copyright (c) 2015 <NAME> <<EMAIL>>
 *
 * This file is part of Ukase.
 *
 *  Ukase is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package com.github.ukase.toolkit;

import com.github.jknack.handlebars.io.AbstractTemplateLoader;
import com.github.jknack.handlebars.io.FileTemplateLoader;
import com.github.jknack.handlebars.io.StringTemplateSource;
import com.github.jknack.handlebars.io.TemplateLoader;
import com.github.jknack.handlebars.io.TemplateSource;
import com.github.ukase.config.UkaseSettings;
import com.github.ukase.toolkit.jar.ZipTemplateSource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

@Slf4j
@Component
public class CompoundTemplateLoader extends AbstractTemplateLoader {
    private static final String IMAGE_AS_PAGE = "default - image as page";
    private static final TemplateSource IMAGE_AS_PAGE_TEMPLATE;
    static {
        IMAGE_AS_PAGE_TEMPLATE =
                new StringTemplateSource(IMAGE_AS_PAGE, StaticUtils.readStringFile(getStream("image-as-page.hbs")));
    }

    private static InputStream getStream(String fileName) {
        return CompoundTemplateLoader.class.getResourceAsStream(fileName);
    }

    private final ZipFile zip;
    private final Map<String, ZipEntry> resources = new HashMap<>();
    private final TemplateLoader externalLoader;

    @Autowired
    public CompoundTemplateLoader(UkaseSettings settings) throws IOException {
        log.info("Starting with settings: ", settings);
        log.info(settings.toString(), settings);
        File templates = settings.getTemplates();
        externalLoader =  templates == null ? null : new FileTemplateLoader(templates);

        if (settings.getJar() == null) {
            zip = null;
            return;
        }

        zip = new ZipFile(settings.getJar());
        zip.stream().forEach(this::registerResource);
    }

    @Override
    public TemplateSource sourceAt(String location) throws IOException {
        if (IMAGE_AS_PAGE.equals(location)) {
            //TODO extract loader for default templates (if there were more than 1)
            return IMAGE_AS_PAGE_TEMPLATE;
        }
        try {
            if (externalLoader == null) {
                return getTemplateSource(location, null);
{'fraction_non_alphanumeric': 0.08495821727019498, 'fraction_numerical': 0.002321262766945218, 'mean_word_length': 3.646297627606039, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6890689', 'n_tokens_mistral': 1796, 'n_tokens_neox': 1751, 'n_words': 526}