<commit_before>from jinja2 import Template
from hokusai.lib.config_loader import ConfigLoader
from hokusai.lib.exceptions import HokusaiError

class TemplateRenderer(object):
  def __init__(self, template_path, var_path):
    self.template_path = template_path
    self.var_path = var_path

  def load_variables(self):
    configloader = ConfigLoader(self.var_path)
    config = configloader.load()
    if type(config) is not dict or "vars" not in config:
      raise HokusaiError("YAML is not valid shape")
    return config["vars"]

  def load_template(self):
    try:
      with open(self.template_path) as file_:
        template = Template(file_.read())
        return template
    except IOError: 
      raise HokusaiError("Template not found.")

  def insert_variables(self):
    vars = self.load_variables()
    template = self.load_template()
    return template.render(**vars)<commit_msg>refactor TemplateRenderer to accept generic vars dict - ConfigLoader will enforce Yaml shape<commit_after>from jinja2 import Template
from hokusai.lib.exceptions import HokusaiError

class TemplateRenderer(object):
  def __init__(self, template_path, template_vars):
    self.template_path = template_path
    self.template_vars = template_vars

  def load_variables(self):
    if type(self.template_vars) is not dict:
      raise HokusaiError("Provided variables are not key-value pairs")
    return self.template_vars

  def load_template(self):
    try:
      with open(self.template_path) as file_:
        template = Template(file_.read())
        return template
    except IOError: 
      raise HokusaiError("Template not found.")

  def render(self):
    _vars = self.load_variables()
    template = self.load_template()
    return template.render(**_vars)

{'fraction_non_alphanumeric': 0.08281338627339761, 'fraction_numerical': 0.0011344299489506524, 'mean_word_length': 3.955056179775281, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2468597', 'n_tokens_mistral': 522, 'n_tokens_neox': 505, 'n_words': 140}