예제 #1
0
    def parse(cls, spec, data, matrix_declarations=None):
        declarations = copy.copy(data.get(spec.DECLARATIONS, {}))
        matrix_declarations = copy.copy(matrix_declarations)
        if matrix_declarations:
            declarations = deep_update(matrix_declarations, declarations)

        parsed_data = {
            spec.VERSION: data[spec.VERSION],
            spec.KIND: data[spec.KIND],
            spec.PROJECT: data[spec.PROJECT],
        }

        if declarations:
            declarations = cls.parse_expression(spec, declarations,
                                                declarations)
            parsed_data[spec.DECLARATIONS] = declarations

        if spec.ENVIRONMENT in data:
            parsed_data[spec.ENVIRONMENT] = cls.parse_expression(
                spec, data[spec.ENVIRONMENT], declarations)

        if spec.SETTINGS in data:
            parsed_data[spec.SETTINGS] = cls.parse_expression(
                spec, data[spec.SETTINGS], declarations)

        if spec.RUN_EXEC in data:
            parsed_data[spec.RUN_EXEC] = cls.parse_expression(
                spec, data[spec.RUN_EXEC], declarations, True, False)

        for section in spec.GRAPH_SECTIONS:
            if section in data:
                parsed_data[section] = cls.parse_expression(
                    spec, data[section], declarations, True, True)

        return parsed_data
예제 #2
0
def read(config_values):
    """Reads an ordered list of configuration values and deep merge the values in reverse order."""
    if not config_values:
        raise PolyaxonConfigurationError(
            'Cannot read config_value: `{}`'.format(config_values))

    config_values = to_list(config_values)

    config = {}
    for config_value in config_values:
        if not isinstance(config_value, (Mapping, six.string_types)):
            raise PolyaxonConfigurationError(
                "Expects Mapping, string, or list of Mapping/string instances, "
                "received {} instead".format(type(config_value)))

        if isinstance(config_value, Mapping):
            config_results = config_value
        elif os.path.isfile(config_value):
            config_results = _read_from_file(config_value)
        else:
            # try reading a stream of yaml or json
            try:
                config_results = _read_from_stream(config_value)
            except ScannerError:
                raise PolyaxonConfigurationError(
                    'Received non valid yaml stream: `{}`'.format(
                        config_value))

        if config_results and isinstance(config_results, Mapping):
            config = deep_update(config, config_results)
        else:
            raise PolyaxonConfigurationError(
                'Cannot read config_value: `{}`'.format(config_value))

    return config