Example #1
0
def handle_moban_file_v1(moban_file_configurations, command_line_options):
    merged_options = None
    target = extract_target(command_line_options)
    if constants.LABEL_CONFIG in moban_file_configurations:
        merged_options = merge(
            command_line_options,
            moban_file_configurations[constants.LABEL_CONFIG],
        )
    merged_options = merge(command_line_options, constants.DEFAULT_OPTIONS)
    plugins_dirs = merged_options.get(constants.LABEL_PLUGIN_DIRS)
    if plugins_dirs:
        handle_plugin_dirs(plugins_dirs)

    targets = moban_file_configurations.get(constants.LABEL_TARGETS)
    if targets:
        if target:
            # if command line option exists, append its template to targets
            # issue 30
            targets += target
        number_of_templated_files = handle_targets(merged_options, targets)
    else:
        number_of_templated_files = 0

    if constants.LABEL_COPY in moban_file_configurations:
        number_of_copied_files = handle_copy(
            merged_options[constants.LABEL_TMPL_DIRS],
            moban_file_configurations[constants.LABEL_COPY],
        )
    else:
        number_of_copied_files = 0
    exit_code = reporter.convert_to_shell_exit_code(number_of_templated_files +
                                                    number_of_copied_files)
    reporter.report_up_to_date()
    return exit_code
Example #2
0
 def get_data(self, file_name):
     file_extension = os.path.splitext(file_name)[1]
     if file_extension == ".json":
         data = utils.open_json(self.context_dirs, file_name)
     elif file_extension in [".yml", ".yaml"]:
         data = utils.open_yaml(self.context_dirs, file_name)
         utils.merge(data, self.__cached_environ_variables)
     else:
         raise exceptions.IncorrectDataInput
     return data
Example #3
0
 def get_data(self, file_name):
     try:
         data = load_data(self.context_dirs, file_name)
         utils.merge(data, self.__cached_environ_variables)
         return data
     except (IOError, exceptions.IncorrectDataInput) as exception:
         # If data file doesn't exist:
         # 1. Alert the user of their (potential) mistake
         # 2. Attempt to use environment vars as data
         reporter.report_warning_message(str(exception))
         reporter.report_using_env_vars()
         return self.__cached_environ_variables
Example #4
0
def handle_moban_file_v1(moban_file_configurations, command_line_options):
    merged_options = None

    targets = moban_file_configurations.get(constants.LABEL_TARGETS, [])
    if constants.LABEL_COPY in moban_file_configurations:
        legacy_copy_targets = handle_copy(
            merged_options, moban_file_configurations[constants.LABEL_COPY])
        targets += legacy_copy_targets

    cli_target = extract_target(command_line_options)
    group_target = command_line_options.get(constants.LABEL_GROUP)
    if group_target:
        # will raise exception when group target not found
        targets = extract_group_targets(group_target, targets)

    if constants.LABEL_CONFIG in moban_file_configurations:
        merged_options = merge(
            command_line_options,
            moban_file_configurations[constants.LABEL_CONFIG],
        )
    merged_options = merge(command_line_options, constants.DEFAULT_OPTIONS)
    plugins_dirs = merged_options.get(constants.LABEL_PLUGIN_DIRS)
    if plugins_dirs:
        handle_plugin_dirs(plugins_dirs)

    requires = moban_file_configurations.get(constants.LABEL_REQUIRES)
    if requires:
        handle_requires(requires)

    # call expand template directory always after handle require please
    # the penalty is: newly clone repos are not visible
    merged_options[constants.LABEL_TMPL_DIRS] = list(
        expand_template_directories(merged_options[constants.LABEL_TMPL_DIRS]))
    extensions = moban_file_configurations.get(constants.LABEL_EXTENSIONS)
    if extensions:
        plugins.ENGINES.register_extensions(extensions)

    template_types = merged_options.get(constants.LABEL_TEMPLATE_TYPES)
    if template_types:
        plugins.ENGINES.register_options(template_types)

    if cli_target:
        number_of_templated_files = handle_targets(merged_options,
                                                   [cli_target])
    elif targets:
        number_of_templated_files = handle_targets(merged_options, targets)
    else:
        number_of_templated_files = 0

    exit_code = reporter.convert_to_shell_exit_code(number_of_templated_files)
    reporter.report_up_to_date()
    return exit_code
Example #5
0
def handle_moban_file_v1(moban_file_configurations, command_line_options):
    merged_options = None

    targets = moban_file_configurations.get(constants.LABEL_TARGETS)
    target = extract_target(command_line_options)

    if constants.LABEL_CONFIG in moban_file_configurations:
        merged_options = merge(
            command_line_options,
            moban_file_configurations[constants.LABEL_CONFIG],
        )
    merged_options = merge(command_line_options, constants.DEFAULT_OPTIONS)
    plugins_dirs = merged_options.get(constants.LABEL_PLUGIN_DIRS)
    if plugins_dirs:
        handle_plugin_dirs(plugins_dirs)

    requires = moban_file_configurations.get(constants.LABEL_REQUIRES)
    if requires:
        handle_requires(requires)

    # call expand template directory always after handle require please
    # the penalty is: newly clone repos are not visible
    merged_options[constants.LABEL_TMPL_DIRS] = list(
        expand_template_directories(merged_options[constants.LABEL_TMPL_DIRS]))
    extensions = moban_file_configurations.get(constants.LABEL_EXTENSIONS)
    if extensions:
        plugins.ENGINES.register_extensions(extensions)

    if targets:
        if target:
            targets = target
            # If template specified via CLI flag `-t:
            # 1. Only update the specified template
            # 2. Do not copy
            if constants.LABEL_COPY in moban_file_configurations:
                del moban_file_configurations[constants.LABEL_COPY]
        number_of_templated_files = handle_targets(merged_options, targets)
    else:
        number_of_templated_files = 0

    if constants.LABEL_COPY in moban_file_configurations:
        number_of_copied_files = handle_copy(
            merged_options, moban_file_configurations[constants.LABEL_COPY])
    else:
        number_of_copied_files = 0
    exit_code = reporter.convert_to_shell_exit_code(number_of_templated_files +
                                                    number_of_copied_files)
    reporter.report_up_to_date()
    return exit_code
Example #6
0
def test_three_level_conflict():
    user = {
        "L1":
        {
            "L2": {
                "L3": "World"
            }
        }
    }
    default = {
        "L1":
        {
            "L2": {
                "L3": "Hi"
            }
        }
    }
    merged = merge(user, default)
    assert merged == {
        "L1":
        {
            "L2": {
                "L3": "World"
            }
        }
    }
Example #7
0
def handle_command_line(options):
    """
    act upon command options
    """
    options = merge(options, constants.DEFAULT_OPTIONS)
    engine = plugins.ENGINES.get_engine(
        options[constants.LABEL_TEMPLATE_TYPE],
        options[constants.LABEL_TMPL_DIRS],
        options[constants.LABEL_CONFIG_DIR],
    )
    if options[constants.LABEL_TEMPLATE] is None:
        if options[constants.POSITIONAL_LABEL_TEMPLATE] is None:
            raise exceptions.NoTemplate(constants.ERROR_NO_TEMPLATE)
        else:
            engine.render_string_to_file(
                options[constants.POSITIONAL_LABEL_TEMPLATE],
                options[constants.LABEL_CONFIG],
                options[constants.LABEL_OUTPUT],
            )
    else:
        engine.render_to_file(
            options[constants.LABEL_TEMPLATE],
            options[constants.LABEL_CONFIG],
            options[constants.LABEL_OUTPUT],
        )
    engine.report()
    HASH_STORE.save_hashes()
    exit_code = reporter.convert_to_shell_exit_code(
        engine.number_of_templated_files())
    return exit_code
Example #8
0
def test_simple_union():
    user = {"hi": "world"}
    default = {"world": "hi"}
    merged = merge(user, default)
    assert merged == {
        "hi": "world",
        "world": "hi"
    }
Example #9
0
def handle_moban_file_v1(moban_file_configurations, command_line_options):
    merged_options = None
    if constants.LABEL_CONFIG in moban_file_configurations:
        merged_options = merge(
            command_line_options,
            moban_file_configurations[constants.LABEL_CONFIG])
    merged_options = merge(
        command_line_options,
        constants.DEFAULT_OPTIONS)
    list_of_templating_parameters = parse_targets(
        merged_options,
        moban_file_configurations[constants.LABEL_TARGETS])
    engine_class = EngineFactory.get_engine(
        merged_options[constants.LABEL_TEMPLATE_TYPE])
    engine = engine_class(
        merged_options[constants.LABEL_TMPL_DIRS],
        merged_options[constants.LABEL_CONFIG_DIR])
    engine.render_to_files(list_of_templating_parameters)
Example #10
0
def test_simple_overlapping():
    user = {
        "hi": "world",
        "world": "hei"
    }
    default = {"world": "hi"}
    merged = merge(user, default)
    assert merged == {
        "hi": "world",
        "world": "hei"
    }
Example #11
0
def load_data(base_dir, file_name):
    abs_file_path = utils.search_file(base_dir, file_name)
    data = LOADER.get_data(abs_file_path)
    if data is not None:
        parent_data = None
        if base_dir and constants.LABEL_OVERRIDES in data:
            parent_data = load_data(base_dir,
                                    data.pop(constants.LABEL_OVERRIDES))
        if parent_data:
            return utils.merge(data, parent_data)
        else:
            return data
    else:
        return None
Example #12
0
File: main.py Project: jayvdb/moban
def handle_command_line(options):
    """
    act upon command options
    """
    options = merge(options, constants.DEFAULT_OPTIONS)
    if options[constants.LABEL_TEMPLATE] is None:
        print(ERROR_NO_TEMPLATE)
        sys.exit(-1)
    engine_class = EngineFactory.get_engine(
        options[constants.LABEL_TEMPLATE_TYPE])
    engine = engine_class(options[constants.LABEL_TMPL_DIRS],
                          options[constants.LABEL_CONFIG_DIR])
    engine.render_to_file(
        options[constants.LABEL_TEMPLATE],
        options[constants.LABEL_CONFIG],
        options[constants.LABEL_OUTPUT]
    )
Example #13
0
def test_two_level_merge():
    user = {
        "L1":
        {
            "L2": "World"
        }
    }
    default = {
        "L1":
        {
            "L2.1": "Hi"
        }
    }
    merged = merge(user, default)
    assert merged == {
        "L1":
        {
            "L2": "World",
            "L2.1": "Hi"
        }
    }
Example #14
0
 def get_data(self, file_name):
     data = utils.open_yaml(self.context_dirs, file_name)
     utils.merge(data, self.__cached_environ_variables)
     return data
Example #15
0
def test_three_level_conflict():
    user = {"L1": {"L2": {"L3": "World"}}}
    default = {"L1": {"L2": {"L3": "Hi"}}}
    merged = merge(user, default)
    assert merged == {"L1": {"L2": {"L3": "World"}}}
Example #16
0
def test_simple_union():
    user = {"hi": "world"}
    default = {"world": "hi"}
    merged = merge(user, default)
    assert merged == {"hi": "world", "world": "hi"}
Example #17
0
def test_two_level_merge():
    user = {"L1": {"L2": "World"}}
    default = {"L1": {"L2.1": "Hi"}}
    merged = merge(user, default)
    assert merged == {"L1": {"L2": "World", "L2.1": "Hi"}}
Example #18
0
def test_simple_overlapping():
    user = {"hi": "world", "world": "hei"}
    default = {"world": "hi"}
    merged = merge(user, default)
    assert merged == {"hi": "world", "world": "hei"}