Example #1
0
    def test_reload_on_path_change(self):
        '''
        Case: We have two paths. Template is in second.
        After we load this template for the first time, we move
        file from other location to the first one.
        New template overlays the already loaded one - it would
        match the same path.
        Because we move file, its mtime may be older than mtime
        of the already loaded one.
        We should detect new file an reload template.
        '''
        tmpdir = tempfile.mkdtemp(dir='.')
        try:
            paths = (
                tmpdir,
                './tests/files/',
            )
            loader = FilesystemLoader(paths=paths, input_encoding='utf-8')
            environment = PyhaaEnvironment(loader = loader)

            self.assertEqual(
                self._load_and_render(environment, 'basic.pha'),
                '<h1>ME GUSTA</h1>',
            )

            tmpfile = os.path.join(tmpdir, 'basic.pha')
            try:
                with open(tmpfile, 'w') as fp:
                    fp.write('%strong reload!')

                # Make sure file from first path is actually older
                # Otherwise it happily reloads because of mtime change
                # But it must reload because of different path/new file
                os.utime('./tests/files/basic.pha', None)
                newtime = os.path.getmtime(tmpfile) - 100
                os.utime(tmpfile, (newtime, newtime))

                self.assertEqual(
                    self._load_and_render(environment, 'basic.pha'),
                    '<strong>reload!</strong>',
                )
            finally:
                os.remove(tmpfile)

            environment.auto_reload = False
            self.assertEqual(
                self._load_and_render(environment, 'basic.pha'),
                '<strong>reload!</strong>',
            )
            environment.auto_reload = True
            self.assertEqual(
                self._load_and_render(environment, 'basic.pha'),
                '<h1>ME GUSTA</h1>',
            )
        finally:
            os.rmdir(tmpdir)
Example #2
0
    def test_reload(self):
        reloaded = False

        class MyLoader(FilesystemLoader):
            def get_bytecode(self, *args, **kwargs):
                nonlocal reloaded
                reloaded = True
                return super().get_bytecode(*args, **kwargs)

        loader = MyLoader(paths='./tests/files/', input_encoding = 'utf-8')
        environment = PyhaaEnvironment(loader = loader)

        self._load_and_render(environment, 'basic.pha')
        reloaded = False
        self._load_and_render(environment, 'basic.pha')
        self.assertFalse(reloaded)
        os.utime('./tests/files/basic.pha', None)
        self._load_and_render(environment, 'basic.pha')
        self.assertTrue(reloaded)
        reloaded = False
        environment.auto_reload = False
        os.utime('./tests/files/basic.pha', None)
        self._load_and_render(environment, 'basic.pha')
        self.assertFalse(reloaded)
Example #3
0
class TestLoaders(PyhaaTestCase):
    def setUp(self):
        loader = FilesystemLoader(paths='./tests/files/inheritance', input_encoding = 'utf-8')
        self.environment = PyhaaEnvironment(loader = loader)

    def _load_and_render(self, path):
        template = self.environment.get_template(path)
        return html_render_to_string(template)

    def test_basic_inheritance(self):
        self.assertEqual(
            self._load_and_render('basic_A.pha'),
            'A B',
        )

    def test_complicated_inheritance(self):
        #       A
        #       |
        #     +-+-+
        #     |   |
        #     B   C
        #     |   |
        #   +-+---|--+
        #   |     |  |
        #   D     |  |
        #   |     |  |
        #   +---+-+--+
        #       |
        #       E ← (E inherits, in order, from D, C and B)
        # Notice inherit syntax in E is split in two lines to test if it works...
        self.assertEqual(
            self._load_and_render('comp_E.pha'),
            'E D C B A',
        )

    def test_loop_detect_short(self):
        self.assertRaises(
            Exception,
            self._load_and_render,
            'loop_A.pha',
        )

    def test_loop_detect_long(self):
        self.assertRaises(
            Exception,
            self._load_and_render,
            'loop2_A.pha',
        )
Example #4
0
#coding:utf8

import logging
import os
import sys

logging.getLogger().setLevel(logging.ERROR)

from pyhaa import (
    PyhaaEnvironment,
    html_render_to_iterator,
)
from pyhaa.runtime.proxy import InstanceProxy

env = PyhaaEnvironment()
structure = env.parse_io(sys.stdin)
code = env.codegen_structure(structure)
bytecode = compile(code, '<string>', 'exec')
template_info = env.template_info_from_bytecode(bytecode)
template = InstanceProxy([template_info], env)
iterator = html_render_to_iterator(template)

fileno = sys.stdout.fileno()
for value in iterator:
    os.write(fileno, value)

Example #5
0
 def setUp(self):
     loader = FilesystemLoader(paths='./tests/files/inheritance', input_encoding = 'utf-8')
     self.environment = PyhaaEnvironment(loader = loader)