All Downloads are FREE. Search and download functionalities are using the official Maven repository.

jdplus.cruncher.ArgsDecoder2 Maven / Gradle / Ivy

There is a newer version: 3.3.0
Show newest version
/*
 * Copyright 2018 National Bank of Belgium
 * 
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved 
 * by the European Commission - subsequent versions of the EUPL (the "Licence");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 * 
 * http://ec.europa.eu/idabc/eupl
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and 
 * limitations under the Licence.
 */
package jdplus.cruncher;

import jdplus.sa.base.api.EstimationPolicyType;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.util.Properties;
import java.util.concurrent.Callable;
import lombok.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import picocli.CommandLine;
import picocli.jansi.graalvm.AnsiConsole;

/**
 *
 * @author Philippe Charles
 */
@CommandLine.Command(
        name = "jwsacruncher",
        description = {
            "JWSACruncher is a console tool that re-estimates all the multi-processing defined in a workspace. "
            + "The workspace may have been generated by means of Demetra+ (.NET), of JDemetra+ (Java) or by any user tool.",
            "Note that calling this tool without any parameter generates a default config file in the user dir."
        },
        versionProvider = ArgsDecoder2.ManifestVersionProvider.class,
        sortOptions = false,
        descriptionHeading = "%n",
        parameterListHeading = "%nParameters:%n",
        optionListHeading = "%nOptions:%n",
        commandListHeading = "%nCommands:%n",
        headerHeading = "%n",
        mixinStandardHelpOptions = true
)
final class ArgsDecoder2 implements Callable {

    @CommandLine.Parameters(
            description = "Workspace file."
    )
    private File workspace;

    @CommandLine.Option(
            names = {"-x", "-X"},
            paramLabel = "",
            description = "Config file."
    )
    private File configFile = null;

    @CommandLine.Option(
            names = {"-d"},
            paramLabel = "",
            description = "Output folder. [workspace]/Output by default."
    )
    private String output = null;

    @CommandLine.Option(
            names = {"-m"},
            paramLabel = "",
            description = "File that contains the items of the matrix output."
    )
    private File matrixFile = null;

    @CommandLine.Option(
            names = {"-p"},
            paramLabel = "",
            description = "Refreshing policy of the processing."
    )
    private String policy = null;

    @CommandLine.Option(
            names = {"-f"},
            paramLabel = "",
            description = "Layout of the csv files: list (default), htable, vtable."
    )
    private String layout = null;

    @Override
    public Args call() throws Exception {
        WsaConfig config = configFile != null ? WsaConfig.read(configFile) : new WsaConfig();
        if (output != null) {
            config.Output = output;
        }
        if (matrixFile != null) {
            config.Matrix = readMatrixConfig(matrixFile);
        }
        if (policy != null) {
            config.policy = policy;
            if (config.getPolicy() == EstimationPolicyType.None) {
                throw new IllegalArgumentException("Invalid policy arg");
            }
        }
        if (layout != null) {
            config.layout = layout;
        }
        return Args.of(workspace, config);
    }

    private static String[] readMatrixConfig(File file) throws IOException {
        return Files.readAllLines(file.toPath()).toArray(new String[0]);
    }

    @Nullable
    public static Args decode(@NonNull String... args) {
        try (AnsiConsole ansi = AnsiConsole.windowsInstall()) {
            return CommandLine.call(new ArgsDecoder2(), args);
        }
    }

    public static final class ManifestVersionProvider implements CommandLine.IVersionProvider {

        @Override
        public String[] getVersion() {
            return new String[]{
                    "@|bold " + "jwsacruncher" + " " + loadVersion() + "|@",
                    "JVM: ${java.version} (${java.vendor} ${java.vm.name} ${java.vm.version})",
                    "OS: ${os.name} ${os.version} ${os.arch}"
            };
        }
    }

    private static String loadVersion() {
        Properties properties = new Properties();
        try (InputStream stream = ArgsDecoder2.class.getResourceAsStream("/META-INF/maven/eu.europa.ec.joinup.sat/jdplus-main-cli-bin/pom.properties")) {
            if (stream != null) {
                properties.load(stream);
            }
        } catch (IOException ex) {
            throw new UncheckedIOException(ex);
        }
        return properties.getProperty("version", "unknown");
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy