예제 #1
0
파일: test.py 프로젝트: bekbulatov/PyV8Mono
    def test_load_file(self):
        # Load file
        renderer = MonoContext()
        self.assertEqual([], renderer.load_file(JS_FILE))

        # Load absent file
        self.assertRaises(V8Error, renderer.load_file, ABSENT_JS_FILE)
예제 #2
0
    def __init__(self, js_root='', js_files=None, js_files_autoload=False, **kwargs):
        self.renderer = MonoContext(**kwargs)
        self.js_root = js_root
        self.js_files = js_files
        self.js_files_autoload = js_files_autoload

        if self.js_files_autoload:
            self.load_js_files()
예제 #3
0
파일: test.py 프로젝트: bekbulatov/PyV8Mono
    def test_execute_file_no_wt(self):
        renderer = MonoContext(watch_templates=False)

        with open(TMP_JS_FILE, 'w') as f:
            f.write(self.js1)
        out, errors = renderer.execute_file(TMP_JS_FILE, ';fn();', '')
        self.assertEqual('1', out)

        time.sleep(1)

        with open(TMP_JS_FILE, 'w') as f:
            f.write(self.js2)
        out, errors = renderer.execute_file(TMP_JS_FILE, ';fn();', '')
        self.assertEqual('1', out)
예제 #4
0
class FestRenderer(object):
    _js_loaded = False

    def __init__(self, js_root='', js_files=None, js_files_autoload=False, **kwargs):
        self.renderer = MonoContext(**kwargs)
        self.js_root = js_root
        self.js_files = js_files
        self.js_files_autoload = js_files_autoload

        if self.js_files_autoload:
            self.load_js_files()

    def load_js_files(self):
        if not self._js_loaded:
            if self.js_files is not None:
                for js_file in self.js_files:
                    filepath = os.path.abspath(os.path.join(self.js_root, js_file))
                    self.renderer.load_file(filepath)
            self._js_loaded = True

    def render(self, template_name, json_str):
        if not self.js_files_autoload:
            self.load_js_files()

        template_name = force_bytes(template_name)
        json_str = force_bytes(json_str)

        try:
            bundle = template_name.split('/')[-2]
        except ValueError:
            raise ValueError('Wrong template name')

        filepath = os.path.abspath(os.path.join(self.js_root, template_name))
        append_str = ';fest["{}.xml"]( JSON.parse(__dataFetch()) );'.format(bundle)
        response, errors = self.renderer.execute_file(filepath, append_str, json_str)
        if errors:
            logger.warning('\n'.join(errors))
        return response
예제 #5
0
파일: test.py 프로젝트: bekbulatov/PyV8Mono
    def test_execute_file(self):
        renderer = MonoContext()

        # Execute file with no append not json
        out, errors = renderer.execute_file(JS_FILE, '', '')
        self.assertEqual(0, len(out))

        # Execute file with append
        out, errors = renderer.execute_file(JS_FILE, ';fest["top.xml"]( JSON.parse(__dataFetch()) );', '{}')
        self.assertEqual(31478, len(out))

        # Execute file with append and json
        out, errors = renderer.execute_file(JS_FILE, ';fest["top.xml"]( JSON.parse(__dataFetch()) );', self.json_str)
        self.assertEqual(551294, len(out))

        # Execute file with append and json again
        out, errors = renderer.execute_file(JS_FILE, ';fest["top.xml"]( JSON.parse(__dataFetch()) );', self.json_str)
        self.assertEqual(551294, len(out))

        # Execute file with append and json again
        out, errors = renderer.execute_file(JS_FILE, ';fest["top.xml"]( JSON.parse(__dataFetch()) );', self.json_str)
        self.assertEqual(551294, len(out))

        # Check \0 embeded symbol
        out, errors = renderer.execute_file(ZERO_JS_FILE, '', '')
        self.assertEqual(11, len(out))

        # Execute file with js error
        self.assertRaises(V8Error, renderer.execute_file, ERROR_JS_FILE, '', '')

        # Execute absent file
        self.assertRaises(V8Error, renderer.execute_file, ABSENT_JS_FILE, ';fest["top.xml"]( JSON.parse(__dataFetch()) );', self.json_str)

        # Catch warn message from __errorLog
        out, errors = renderer.execute_file(JS_FILE, '__errorLog("123");', self.json_str)
        self.assertEqual(['123'], errors)
예제 #6
0
파일: test.py 프로젝트: bekbulatov/PyV8Mono
    def test_init(self):
        # Init w/o parameters
        renderer = MonoContext()
        renderer.execute_file(JS_FILE, ';fest["top.xml"]( JSON.parse(__dataFetch()) );', self.json_str)

        # Init with LowMemoryNotification
        renderer = MonoContext(run_low_memory_notification=10)
        renderer.execute_file(JS_FILE, ';fest["top.xml"]( JSON.parse(__dataFetch()) );', self.json_str)

        # Init with IdleNotification
        renderer = MonoContext(run_idle_notification_loop=10)
        renderer.execute_file(JS_FILE, ';fest["top.xml"]( JSON.parse(__dataFetch()) );', self.json_str)

        # Init with watch_templates
        renderer = MonoContext(watch_templates=True)
        renderer.execute_file(JS_FILE, ';fest["top.xml"]( JSON.parse(__dataFetch()) );', self.json_str)

        # Init cmd_args
        renderer = MonoContext(cmd_args='--max-old-space-size=128')
        renderer.execute_file(JS_FILE, ';fest["top.xml"]( JSON.parse(__dataFetch()) );', self.json_str)

        # Init with all parameters
        renderer = MonoContext(run_low_memory_notification=5, run_idle_notification_loop=10, watch_templates=True)
        renderer.execute_file(JS_FILE, ';fest["top.xml"]( JSON.parse(__dataFetch()) );', self.json_str)

        # Wrong init
        self.assertRaises(TypeError, MonoContext, run_low_memory_notification='7')

        self.assertRaises(ValueError, MonoContext, cmd_args='--max-old-space-size=128 ' * 200)