def test_get_compiler_by_name(monkeypatch): compiler_stub = stub() monkeypatch.setattr("static_precompiler.utils.get_compilers", lambda: { "foo": compiler_stub, }) with pytest.raises(CompilerNotFound): get_compiler_by_name("bar") assert get_compiler_by_name("foo") is compiler_stub
def test_get_compiler_by_name(self): with patch("static_precompiler.utils.get_compilers" ) as mocked_get_compilers: mocked_get_compilers.return_value = { CoffeeScript.name: CoffeeScript() } self.assertRaises(CompilerNotFound, get_compiler_by_name, 'non-existing compiler') compiler = get_compiler_by_name(CoffeeScript.name) self.assertTrue(isinstance(compiler, CoffeeScript))
def inlinecompile(nodelist, context, compiler): source = nodelist.render(context) if isinstance(compiler, six.string_types): compiler = utils.get_compiler_by_name(compiler) if settings.USE_CACHE: cache_key = utils.get_cache_key("{0}.{1}".format(compiler.__class__.__name__, utils.get_hexdigest(source))) cache = utils.get_cache() cached = cache.get(cache_key, None) if cached is not None: return cached output = compiler.compile_source(source) cache.set(cache_key, output, settings.CACHE_TIMEOUT) return output return compiler.compile_source(source)
def inlinecompile(nodelist, context, compiler): source = nodelist.render(context) if isinstance(compiler, six.string_types): compiler = utils.get_compiler_by_name(compiler) if settings.USE_CACHE: cache_key = utils.get_cache_key("{0}.{1}".format( compiler.__class__.__name__, utils.get_hexdigest(source))) cache = utils.get_cache() cached = cache.get(cache_key, None) if cached is not None: return cached output = compiler.compile_source(source) cache.set(cache_key, output, settings.CACHE_TIMEOUT) return output return compiler.compile_source(source)
def render(self, context): source = self.nodelist.render(context) if self.compiler[0] == self.compiler[-1] and self.compiler[0] in ('"', "'"): compiler = self.compiler[1:-1] else: compiler = django.template.Variable(self.compiler).resolve(context) if isinstance(compiler, six.string_types): compiler = utils.get_compiler_by_name(compiler) if settings.USE_CACHE: cache_key = utils.get_cache_key("{0}.{1}".format( compiler.__class__.__name__, utils.get_hexdigest(source))) cache = utils.get_cache() cached = cache.get(cache_key, None) if cached is not None: return cached output = compiler.compile_source(source) cache.set(cache_key, output, settings.CACHE_TIMEOUT) return output return compiler.compile_source(source)