Esempio n. 1
0
    def test_render_path(self):
        """
        Test the render_path() method.

        """
        renderer = Renderer()
        path = get_data_path('say_hello.mustache')
        actual = renderer.render_path(path, to='foo')
        self.assertEqual(actual, "Hello, foo")
Esempio n. 2
0
    def test_render_path(self):
        """
        Test the render_path() method.

        """
        renderer = Renderer()
        path = get_data_path('say_hello.mustache')
        actual = renderer.render_path(path, to='foo')
        self.assertEqual(actual, "Hello, foo")
class PystacheTemplate(bottle.BaseTemplate):
    ''' Pystache is a Python Mustache implementation.
        Set `View.template_extension` to be able to use partials,
        because partial calls are not handled by Bottle and Pystache works
        with only one extension, defined as `mustache` by default.
    '''

    try:
        extensions = bottle.BaseTemplate.extensions
    except AttributeError:
        # Bottle had a misspelling in BaseTemplate.
        # It is fixed in Bottle v0.10.
        extensions = bottle.BaseTemplate.extentions
    extensions.insert(0, 'mustache')

    def __init__(self, source=None, name=None, lookup=[], encoding='utf8',
            layout=None, **settings):
        if layout:
            self.layout = layout
        super(PystacheTemplate, self).__init__(source=source, name=name,
                lookup=lookup, encoding=encoding, **settings)

    def prepare(self, **options):
        from pystache import Renderer
        self.context = threading.local()
        self.context.vars = {}
        if hasattr(self, "layout"):
            self.layout_filename = self.search(self.layout, self.lookup)
        self.tpl = Renderer(search_dirs=self.lookup,
                file_encoding=self.encoding, string_encoding=self.encoding,
                **options)

    def render(self, *args, **kwargs):
        for dictarg in args:
            kwargs.update(dictarg)
        kwargs.update(self.defaults)
        if hasattr(self, "layout"):
            partial = self.tpl.render_path(self.filename, **kwargs)
            kwargs.update({"yield": partial})
            out = self.tpl.render_path(self.layout_filename, **kwargs)
        else:
            out = self.tpl.render_path(self.filename, **kwargs)
        return out
Esempio n. 4
0
def tpl_render(dict_list, no_triage, late_triage, sol_fine, since):
    """ Render and save to tmp """
    renderer = Renderer()
    match = re.search(r'\d{4}-\d{2}-\d{2}', since)
    since = datetime.strptime(match.group(), '%Y-%m-%d').date()
    my_path = path.abspath(path.dirname(__file__))

    output = renderer.render_path(
        path.join(my_path, TEMPLATE_DIR + "index.mustache"), {
            'lista': dict_list,
            'no-triage': no_triage,
            'late-triage': late_triage,
            'sol-fine': sol_fine,
            'start-date': since,
            'end-date': time.strftime("%Y-%m-%d")
        })

    with open(path.join(my_path, TMP_DIR + "index.html"), 'w') as html_file:
        html_file.write(output)
        html_file.close()
Esempio n. 5
0
async def render(request: Request):
    body = await request.json()
    repository_url = body.get('repository_url')
    encoded_url = urllib.parse.quote_plus(repository_url)
    project_path = Config.DATA_DIR + '/' + encoded_url

    # this is either the first time and we need to clone the repo, or it
    # is not the first time, and we should pull to make sure it is up to
    # date
    if not os.path.isdir(project_path):
        with cd(Config.DATA_DIR):
            os.system('git clone %s %s' % (repository_url, encoded_url))
    else:
        with cd(project_path):
            os.system('git pull origin master')

    entry = body.get('entry')
    data = body.get('data')
    template_type = body.get('type')

    result = None
    if template_type == 'mustache':
        with cd(project_path):
            renderer = Renderer()
            result = renderer.render_path(entry, data)
    elif template_type == 'jinja':
        env = Environment(loader=FileSystemLoader(project_path))
        template = env.get_template(entry)
        result = template.render(**data)
    else:
        return Response(status=400)

    return Response(
        content=bytes(result, 'utf-8'),
        headers={
            'Content-Type': 'text/plain'
        }
    )