Beispiel #1
0
def _handle_implicit_target(options, template_file, output):
    common_data_file = options[constants.LABEL_CONFIG]
    default_template_type = options[constants.LABEL_TEMPLATE_TYPE]
    for src, dest, t_type in handle_template(
            template_file, output, options[constants.LABEL_TMPL_DIRS]):
        if t_type:
            yield TemplateTarget(src, common_data_file, dest, t_type)
        else:
            yield TemplateTarget(src, common_data_file, dest,
                                 default_template_type)
Beispiel #2
0
    def test_single_command(self, fake_template_doer):
        test_args = ["moban", "-m", self.config_file]
        with patch.object(sys, "argv", test_args):
            from moban.main import main

            main()
            call_args = list(fake_template_doer.call_args[0][0])
            eq_(
                call_args,
                [
                    TemplateTarget("README.rst.jj2", "data.yaml",
                                   "README.rst"),
                    TemplateTarget("setup.py.jj2", "data.yaml", "setup.py"),
                ],
            )
Beispiel #3
0
    def test_ad_hoc_type(self):
        target = dict(template=TEMPLATE, output=OUTPUT)
        template_type = [
            {
                "base_type": "jinja2"
            },
            {
                "options": [
                    {
                        "block_end_string": "*))"
                    },
                    {
                        "block_start_string": "((*"
                    },
                ]
            },
        ]
        options = dict(
            configuration=CONFIGURATION,
            template_type=template_type,
            template_dir=TEMPLATE_DIRS,
        )

        actual = list(targets._handle_explicit_target(options, target))
        file_extension = uuid.uuid4().hex
        expected = [
            TemplateTarget(TEMPLATE, CONFIGURATION, OUTPUT, file_extension)
        ]
        eq_(actual, expected)
Beispiel #4
0
    def test_single_command(self, _):
        test_args = ["moban"]
        with patch.object(sys, "argv", test_args):
            from moban.main import main

            with patch("moban.plugins.template.TemplateEngine.render_to_files"
                       ) as fake:
                main()
                call_args = list(fake.call_args[0][0])
                eq_(
                    call_args,
                    [
                        TemplateTarget("README.rst.jj2", "custom-data.yaml",
                                       "README.rst"),
                        TemplateTarget("setup.py.jj2", "data.yml", "setup.py"),
                    ],
                )
Beispiel #5
0
def _handle_explicit_target(options, target):
    common_data_file = options[constants.LABEL_CONFIG]
    default_template_type = options[constants.LABEL_TEMPLATE_TYPE]
    template_file = target.get(constants.LABEL_TEMPLATE,
                               options.get(constants.LABEL_TEMPLATE, None))
    data_file = target.get(constants.LABEL_CONFIG, common_data_file)
    output = target[constants.LABEL_OUTPUT]
    template_type = target.get(constants.LABEL_TEMPLATE_TYPE)
    for src, dest, t_type in handle_template(
            template_file, output, options[constants.LABEL_TMPL_DIRS]):
        if template_type:
            yield TemplateTarget(src, data_file, dest, template_type)
        else:
            if t_type:
                yield TemplateTarget(src, data_file, dest, t_type)
            else:
                yield TemplateTarget(src, data_file, dest,
                                     default_template_type)
Beispiel #6
0
def _handle_group_target(options, a_list_short_hand_targets,
                         group_template_type):
    # grouping by template type feature
    common_data_file = options[constants.LABEL_CONFIG]
    for _output, _template_file in _iterate_list_of_dicts(
            a_list_short_hand_targets):
        for src, dest, t_type in handle_template(
                _template_file, _output, options[constants.LABEL_TMPL_DIRS]):
            yield TemplateTarget(src, common_data_file, dest,
                                 group_template_type)
Beispiel #7
0
    def test_single_command_with_a_few_options(self, fake_template_doer):
        test_args = ["moban", "-t", "README.rst.jj2", "-o", "xyz.output"]
        with patch.object(sys, "argv", test_args):
            from moban.main import main

            main()
            call_args = list(fake_template_doer.call_args[0][0])
            eq_(
                call_args,
                [TemplateTarget("README.rst.jj2", "data.yaml", "xyz.output")],
            )
Beispiel #8
0
def test_do_templates_2(_do_templates_with_more_shared_templates):
    jobs = [
        TemplateTarget("1.template", "data1.yml", "1.output"),
        TemplateTarget("1.template", "data2.yml", "2.output"),
        TemplateTarget("1.template", "data3.yml", "3.output"),
        TemplateTarget("1.template", "data4.yml", "4.output"),
        TemplateTarget("1.template", "data5.yml", "6.output"),
    ]
    expected = {
        "1.template": [
            ("data1.yml", "1.output"),
            ("data2.yml", "2.output"),
            ("data3.yml", "3.output"),
            ("data4.yml", "4.output"),
            ("data5.yml", "6.output"),
        ]
    }
    engine = ENGINES.get_engine("jinja2", ".", ".")
    engine.render_to_files(jobs)
    _do_templates_with_more_shared_templates.assert_called_with(expected)
Beispiel #9
0
def test_do_templates_1(_do_templates_with_more_shared_data):
    jobs = [
        TemplateTarget("1.template", "data.yml", "1.output"),
        TemplateTarget("2.template", "data.yml", "2.output"),
        TemplateTarget("3.template", "data.yml", "3.output"),
        TemplateTarget("4.template", "data.yml", "4.output"),
        TemplateTarget("5.template", "data.yml", "6.output"),
    ]
    expected = {
        "data.yml": [
            ("1.template", "1.output"),
            ("2.template", "2.output"),
            ("3.template", "3.output"),
            ("4.template", "4.output"),
            ("5.template", "6.output"),
        ]
    }
    engine = ENGINES.get_engine("jinja2", ".", ".")
    engine.render_to_files(jobs)
    _do_templates_with_more_shared_data.assert_called_with(expected)
Beispiel #10
0
    def test_use_target_template_type(self):

        target = dict(template_type="use-me", template=TEMPLATE, output=OUTPUT)
        options = dict(
            configuration=CONFIGURATION,
            template_type=DEFAULT_TEMPLATE_TYPE,
            template_dir=TEMPLATE_DIRS,
        )

        actual = list(targets._handle_explicit_target(options, target))
        expected = [TemplateTarget(TEMPLATE, CONFIGURATION, OUTPUT, "use-me")]
        eq_(expected, actual)
Beispiel #11
0
    def test_derive_template_type_from_target_template_file(self):

        options = dict(
            configuration=CONFIGURATION,
            template_type=DEFAULT_TEMPLATE_TYPE,
            template_dir=TEMPLATE_DIRS,
        )

        actual = list(
            targets._handle_implicit_target(options, TEMPLATE, OUTPUT))
        expected = [TemplateTarget(TEMPLATE, CONFIGURATION, OUTPUT, "jj2")]
        eq_(expected, actual)
Beispiel #12
0
def _handle_explicit_target(options, target):
    common_data_file = options[constants.LABEL_CONFIG]
    default_template_type = options[constants.LABEL_TEMPLATE_TYPE]
    template_file = target.get(constants.LABEL_TEMPLATE,
                               options.get(constants.LABEL_TEMPLATE, None))
    data_file = target.get(constants.LABEL_CONFIG, common_data_file)
    output = target[constants.LABEL_OUTPUT]
    template_type = target.get(constants.LABEL_TEMPLATE_TYPE)
    if template_type and len(template_type) > 0:
        if constants.TEMPLATE_TYPES_FILE_EXTENSIONS in template_type:
            reporter.report_file_extension_not_needed()
        if constants.TEMPLATE_TYPES_BASE_TYPE in template_type:
            adhoc_type = uuid.uuid4().hex
            file_extension = uuid.uuid4().hex
            base_type = template_type[constants.TEMPLATE_TYPES_BASE_TYPE]
            template_types_options = template_type[
                constants.TEMPLATE_TYPES_OPTIONS]
            the_adhoc_type = {
                adhoc_type: {
                    constants.TEMPLATE_TYPES_FILE_EXTENSIONS: [file_extension],
                    constants.TEMPLATE_TYPES_BASE_TYPE: base_type,
                    constants.TEMPLATE_TYPES_OPTIONS: template_types_options,
                }
            }
            plugins.ENGINES.register_options(the_adhoc_type)
            template_type = file_extension
    for src, dest, t_type in handle_template(
            template_file, output, options[constants.LABEL_TMPL_DIRS]):
        if template_type:
            yield TemplateTarget(src, data_file, dest, template_type)
        else:
            if t_type:
                yield TemplateTarget(src, data_file, dest, t_type)
            else:
                yield TemplateTarget(src, data_file, dest,
                                     default_template_type)
Beispiel #13
0
def test_handling_group_target():
    group_template_type = "group-template-type"
    options = dict(
        configuration=CONFIGURATION,
        template_type=DEFAULT_TEMPLATE_TYPE,
        template_dir=TEMPLATE_DIRS,
    )
    short_hand_targets = [{OUTPUT: TEMPLATE}]

    actual = list(
        targets._handle_group_target(options, short_hand_targets,
                                     group_template_type))
    expected = [
        TemplateTarget(TEMPLATE, CONFIGURATION, OUTPUT, group_template_type)
    ]
    eq_(expected, actual)
Beispiel #14
0
    def test_use_moban_default_template_from_options(self):
        template_without_suffix = "template"
        target = dict(template=template_without_suffix, output=OUTPUT)
        options = dict(
            configuration=CONFIGURATION,
            template_type=DEFAULT_TEMPLATE_TYPE,
            template_dir=TEMPLATE_DIRS,
        )

        actual = list(targets._handle_explicit_target(options, target))
        expected = [
            TemplateTarget(
                template_without_suffix,
                CONFIGURATION,
                OUTPUT,
                DEFAULT_TEMPLATE_TYPE,
            )
        ]
        eq_(expected, actual)
Beispiel #15
0
def test_template_target_repr():
    require = TemplateTarget("template_file", "dat_file", "output")
    eq_("template_file,dat_file,output,jinja2", repr(require))
Beispiel #16
0
def test_template_target_output_suffix_updates_after_set():
    require = TemplateTarget(
        "template_file", "dat_file", "output.copy", template_type="copy"
    )
    require.set_template_type("jinja2")
    eq_("template_file,dat_file,output.copy,jinja2", repr(require))
Beispiel #17
0
def test_template_target_output_suffix_change():
    require = TemplateTarget(
        "template_file", "dat_file", "output.copy", template_type="copy"
    )
    eq_("template_file,dat_file,output,copy", repr(require))