Exemple #1
0
def test_json_data_overrides_environ_variables():
    test_var = "TEST_ENVIRONMENT_VARIABLE"
    test_value = "am I found"
    os.environ[test_var] = test_value
    context = Context(os.path.join("tests", "fixtures"))
    data = context.get_data("simple.json")
    eq_(data[test_var], test_value)
Exemple #2
0
def test_unknown_data_file():
    test_var = "TEST_ENVIRONMENT_VARIABLE"
    test_value = "am I found"
    os.environ[test_var] = test_value
    context = Context(os.path.join("tests", "fixtures"))
    data = context.get_data("unknown.data")
    eq_(data[test_var], test_value)
Exemple #3
0
def test_context():
    context = Context(os.path.join("tests", "fixtures"))
    data = context.get_data("simple.yaml")
    eq_(data["simple"], "yaml")
Exemple #4
0
class MobanEngine(object):
    def __init__(self, template_dirs, context_dirs, engine):
        template_dirs = list(expand_template_directories(template_dirs))
        utils.verify_the_existence_of_directories(template_dirs)
        context_dirs = expand_template_directory(context_dirs)
        self.context = Context(context_dirs)
        self.template_dirs = template_dirs
        self.engine = engine
        self.templated_count = 0
        self.file_count = 0

    def report(self):
        if self.templated_count == 0:
            reporter.report_no_action()
        elif self.templated_count == self.file_count:
            reporter.report_full_run(self.file_count)
        else:
            reporter.report_partial_run(self.templated_count, self.file_count)

    def number_of_templated_files(self):
        return self.templated_count

    def render_to_file(self, template_file, data_file, output_file):
        self.file_count = 1
        data = self.context.get_data(data_file)
        template = self.engine.get_template(template_file)
        template_abs_path = utils.get_template_path(self.template_dirs,
                                                    template_file)

        flag = self.apply_template(template_abs_path, template, data,
                                   output_file)
        if flag:
            reporter.report_templating(template_file, output_file)
            self.templated_count += 1

    def render_string_to_file(self, template_in_string, data_file,
                              output_file):
        self.file_count = 1
        template = self.engine.get_template_from_string(template_in_string)
        template_abs_path = template_in_string[:10] + "..."
        data = self.context.get_data(data_file)
        flag = self.apply_template(template_abs_path, template, data,
                                   output_file)
        if flag:
            reporter.report_templating(template_abs_path, output_file)
            self.templated_count += 1

    def apply_template(self, template_abs_path, template, data, output_file):
        rendered_content = self.engine.apply_template(template, data,
                                                      output_file)
        if PY3_ABOVE:
            if not isinstance(rendered_content, bytes):
                rendered_content = rendered_content.encode("utf-8")

        try:
            flag = HASH_STORE.is_file_changed(output_file, rendered_content,
                                              template_abs_path)
            if flag:
                utils.write_file_out(output_file, rendered_content)
                utils.file_permissions_copy(template_abs_path, output_file)
            return flag
        except exceptions.FileNotFound:
            utils.write_file_out(output_file, rendered_content)
            return True

    def render_to_files(self, array_of_template_targets):
        sta = Strategy(array_of_template_targets)
        sta.process()
        choice = sta.what_to_do()
        if choice == Strategy.DATA_FIRST:
            self._render_with_finding_data_first(sta.data_file_index)
        else:
            self._render_with_finding_template_first(sta.template_file_index)

    def _render_with_finding_template_first(self, template_file_index):
        for (template_file, data_output_pairs) in template_file_index.items():
            template = self.engine.get_template(template_file)
            template_abs_path = utils.get_template_path(
                self.template_dirs, template_file)
            for (data_file, output) in data_output_pairs:
                data = self.context.get_data(data_file)
                flag = self.apply_template(template_abs_path, template, data,
                                           output)
                if flag:
                    reporter.report_templating(template_file, output)
                    self.templated_count += 1
                self.file_count += 1

    def _render_with_finding_data_first(self, data_file_index):
        for (data_file, template_output_pairs) in data_file_index.items():
            data = self.context.get_data(data_file)
            for (template_file, output) in template_output_pairs:
                template = self.engine.get_template(template_file)
                template_abs_path = utils.get_template_path(
                    self.template_dirs, template_file)
                flag = self.apply_template(template_abs_path, template, data,
                                           output)
                if flag:
                    reporter.report_templating(template_file, output)
                    self.templated_count += 1
                self.file_count += 1