"""
This module generates the HTML report which uses the
template in the templates folder. This report can then
be viewed in the experimentation web application.
"""

import os
import tornado.template

import surround
from surround_cli.experiment.util import get_surround_config

class ReportGenerator(surround.Stage):
    def initialise(self, config):
        loader = tornado.template.Loader(os.path.join(config["project_root"], "templates"))
        self.results_template = loader.load("results.html")
        self.global_config = get_surround_config()

    def operate(self, state, config):
        results_page = self.results_template.generate(
            config=config,
            state=state,
            global_config=self.global_config,
            surround_version=surround.__version__)

        with open(os.path.join(config["output_path"], "results.html"), "wb+") as out:
            out.write(results_page)
