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

org.apache.flink.client.python.PythonDriverOptionsParserFactory Maven / Gradle / Ivy

There is a newer version: 2.0-preview1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.flink.client.python;

import org.apache.flink.runtime.entrypoint.FlinkParseException;
import org.apache.flink.runtime.entrypoint.parser.ParserResultFactory;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;

import javax.annotation.Nonnull;

import static org.apache.flink.client.cli.CliFrontendParser.PYMODULE_OPTION;
import static org.apache.flink.client.cli.CliFrontendParser.PY_OPTION;

/**
 * Parser factory which generates a {@link PythonDriverOptions} from a given list of command line
 * arguments.
 */
final class PythonDriverOptionsParserFactory implements ParserResultFactory {

    @Override
    public Options getOptions() {
        final Options options = new Options();
        options.addOption(PY_OPTION);
        options.addOption(PYMODULE_OPTION);
        return options;
    }

    @Override
    public PythonDriverOptions createResult(@Nonnull CommandLine commandLine)
            throws FlinkParseException {
        String entryPointModule = null;
        String entryPointScript = null;

        if (commandLine.hasOption(PY_OPTION.getOpt())
                && commandLine.hasOption(PYMODULE_OPTION.getOpt())) {
            throw new FlinkParseException("Cannot use options -py and -pym simultaneously.");
        } else if (commandLine.hasOption(PY_OPTION.getOpt())) {
            entryPointScript = commandLine.getOptionValue(PY_OPTION.getOpt());
            if (!entryPointScript.endsWith(".py")) {
                throw new FlinkParseException(
                        String.format(
                                "It only accepts Python file which ends with '.py' for option '-py', got '%s'.",
                                entryPointScript));
            }
        } else if (commandLine.hasOption(PYMODULE_OPTION.getOpt())) {
            entryPointModule = commandLine.getOptionValue(PYMODULE_OPTION.getOpt());
        } else {
            throw new FlinkParseException(
                    "The Python entry point has not been specified. It can be specified with options -py or -pym");
        }

        return new PythonDriverOptions(
                entryPointModule, entryPointScript, commandLine.getArgList());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy