def test_get_template_path_with_error(): temp_dirs = [ os.path.join("tests", "fixtures", "template-tests"), os.path.join("tests", "abc"), ] template = "I-do-not-exist.jj2" get_template_path(temp_dirs, template)
def test_get_template_path(): temp_dirs = ["tests/fixtures/template-tests", "tests/abc", "tests/abc"] template = Mock() template.filename = "a.jj2" template_path = get_template_path(temp_dirs, template) expected = os.path.join(os.getcwd(), "tests/fixtures/template-tests/a.jj2") eq_(template_path, expected)
def render_to_file(self, template_file, data_file, output_file): 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)
def test_get_template_path(): temp_dirs = [ os.path.join("tests", "fixtures", "template-tests"), os.path.join("tests", "abc"), os.path.join("tests", "abc"), ] template = "a.jj2" template_path = get_template_path(temp_dirs, template) expected = os.path.join( os.getcwd(), os.path.join("tests", "fixtures", "template-tests", "a.jj2"), ) eq_(template_path, expected)
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
def _apply_template(self, template, data, output): temp_file_path = get_template_path(self.template_dirs, template) rendered_content = template.render(**data) rendered_content = utils.strip_off_trailing_new_lines(rendered_content) rendered_content = rendered_content.encode("utf-8") flag = HASH_STORE.is_file_changed(output, rendered_content, temp_file_path) if flag: utils.write_file_out(output, rendered_content, strip=False, encode=False) utils.file_permissions_copy(temp_file_path, output) return flag
def get_template(self, template_file): actual_file = utils.get_template_path(self.template_dirs, template_file) with codecs.open(actual_file, "r", encoding="utf-8") as template_contents: source = Literal(template_contents.read()) # Template-Toolkit-Python cannot make a Template Object only # by passing a file_name or file_content. It uses a function # process() to generate the template. The process function # needs to have both the template file and the data of the # variables used in template. template = [] template_object = Template() template.append(template_object) template.append(source) return template
def get_template(self, template_file): actual_file = utils.get_template_path(self.template_dirs, template_file) with codecs.open(actual_file, "r", encoding="utf-8") as source: hbr_template = Compiler().compile(source.read()) return hbr_template