Ejemplo n.º 1
0
 def __init__(self, *args, **kwargs):
     """Patch ``kwargs['cache_args']``."""
     
     if not 'cache_args' in kwargs:
         # Read the ``cache.*`` ones into ``cache_args``.
         cache_args = {}
         prefix = 'mako.cache_args.'
         for key in settings.keys():
             if key.startswith(prefix):
                 name = key.split(prefix)[1].strip()
                 value = settings[key]
                 try:
                     value = value.strip()
                 except AttributeError:
                     pass
                 cache_args[name] = value
         coerce_cache_params(cache_args)
         cache_args['timeout'] = cache_args.get('expire')
         type_arg = cache_args.get('type')
         if type_arg and type_arg.startswith('dogpile.cache'):
             cache_args = coerge_dogpile_args(cache_args)
             register_plugin("dogpile.cache",
                     "pyramid_weblayer.patch", "UnicodeKeyCapableMakoPlugin")
             kwargs['cache_impl'] = 'dogpile.cache'
         kwargs['cache_args'] = cache_args
     super(TemplateLookup, self).__init__(*args, **kwargs)
Ejemplo n.º 2
0
 def __init__(
         self, directories=None,
         module_directory='/tmp/mako_modules',
         cache=None,
         **kwargs):
     from mako.lookup import TemplateLookup
     if cache is None:
         self.template_lookup = TemplateLookup(
             directories=directories or ['content/templates'],
             module_directory=module_directory,
             **kwargs)
     else:
         from mako.cache import register_plugin
         register_plugin('wheezy', 'wheezy.web.templates', 'MakoCacheImpl')
         self.template_lookup = TemplateLookup(
             directories=directories or ['content/templates'],
             module_directory=module_directory,
             cache_impl='wheezy',
             cache_args={'cache': cache},
             **kwargs)
Ejemplo n.º 3
0
            f"cannot access local variable {var} where it is not associated",
        )
        return any((msg in rendered_output) for msg in error_msgs)


class PlainCacheImpl(CacheImpl):
    """Simple memory cache impl so that tests which
    use caching can run without beaker."""
    def __init__(self, cache):
        self.cache = cache
        self.data = {}

    def get_or_create(self, key, creation_function, **kw):
        if key in self.data:
            return self.data[key]
        else:
            self.data[key] = data = creation_function(**kw)
            return data

    def put(self, key, value, **kw):
        self.data[key] = value

    def get(self, key, **kw):
        return self.data[key]

    def invalidate(self, key, **kw):
        del self.data[key]


register_plugin("plain", __name__, "PlainCacheImpl")
Ejemplo n.º 4
0
            cache.set(key, value, timeout)

        return value

    def set(self, key, value, **kwargs):
        timeout = kw.get('timeout', None)
        cache.set(key, value, timeout)

    def get(self, key, **kwargs):
        return cache.get(key)

    def invalidate(self, key, **kwargs):
        cache.delete(key)

# optional - register the class locally
register_plugin("django_cache", __name__, "DjangoCache")
########NEW FILE########
__FILENAME__ = defaults
"""Options used only by Django"""

import os
import logging
from django.conf import settings
from nemo.parser import nemo

MAKO_TEMPLATE_DIRS=(os.path.join(settings.SITE_ROOT, 'templates'),)
MAKO_TEMPLATE_OPTS=dict(input_encoding='utf-8',
                        output_encoding='utf-8',
                        module_directory=os.path.join(settings.SITE_ROOT, 'cache'),
                        preprocessor=nemo
)
Ejemplo n.º 5
0
from mako.cache import register_plugin
from mako.template import Template
import mock
import pytest

from .. import eq_


try:
    import mako  # noqa
except ImportError:
    raise pytest.skip("this test suite requires mako templates")

register_plugin(
    "dogpile.cache", "dogpile.cache.plugins.mako_cache", "MakoPlugin"
)


class TestMakoPlugin(TestCase):
    def _mock_fixture(self):
        reg = mock.MagicMock()
        reg.get_or_create.return_value = "hello world"
        my_regions = {"myregion": reg}
        return (
            {
                "cache_impl": "dogpile.cache",
                "cache_args": {"regions": my_regions},
            },
            reg,
        )
Ejemplo n.º 6
0
            return fn(*arg, **kw)
        finally:
            exceptions._install_highlighting()
    return function_named(go, fn.__name__)

class PlainCacheImpl(CacheImpl):
    """Simple memory cache impl so that tests which
    use caching can run without beaker.  """

    def __init__(self, cache):
        self.cache = cache
        self.data = {}

    def get_or_create(self, key, creation_function, **kw):
        if key in self.data:
            return self.data[key]
        else:
            self.data[key] = data = creation_function(**kw)
            return data

    def put(self, key, value, **kw):
        self.data[key] = value

    def get(self, key, **kw):
        return self.data[key]

    def invalidate(self, key, **kw):
        del self.data[key]

register_plugin("plain", __name__, "PlainCacheImpl")
Ejemplo n.º 7
0
        self.key = key
        self._setup_kwargs(kw)
        self.realcacheimpl.put(key, value, **kw)

    def get(self, key, **kw):
        self.key = key
        self._setup_kwargs(kw)
        return self.realcacheimpl.get(key, **kw)

    def invalidate(self, key, **kw):
        self.key = key
        self._setup_kwargs(kw)
        self.realcacheimpl.invalidate(key, **kw)


register_plugin("mock", __name__, "MockCacheImpl")


class CacheTest(TemplateTest):

    real_backend = "simple"

    def _install_mock_cache(self, template, implname=None):
        template.cache_impl = "mock"
        impl = template.cache.impl
        impl.set_backend(template.cache, implname or self.real_backend)
        return impl

    def test_def(self):
        t = Template(
            """
Ejemplo n.º 8
0
from vilya.libs.store import mc, ONE_HOUR
from mako.cache import CacheImpl, register_plugin


class McCacheImpl(CacheImpl):
    """基于mc的页面cache实现"""

    def __init__(self, cache):
        self.cache = cache

    def get_or_create(self, key, creation_function, **kw):
        r = mc.get(key)
        if r is None:
            r = creation_function()
            expiretime = kw.get("timeout", ONE_HOUR)
            mc.set(key, r, expiretime)
        return r

    def set(self, key, value, **kw):
        expiretime = kw.get("timeout", ONE_HOUR)
        mc.set(key, value, expiretime)

    def get(self, key, **kw):
        return mc.get(key)

    def invalidate(self, key, **kw):
        mc.delete(key)


register_plugin("mccache", __name__, "McCacheImpl")
Ejemplo n.º 9
0
            self.realcacheimpl.put(key, value, **kw)

    def get(self, key, **kw):
        self.key = key
        self.kwargs = kw.copy()
        if self.realcacheimpl:
            return self.realcacheimpl.get(key, **kw)

    def invalidate(self, key, **kw):
        self.key = key
        self.kwargs = kw.copy()
        if self.realcacheimpl:
            self.realcacheimpl.invalidate(key, **kw)


register_plugin("mock", __name__, "MockCacheImpl")


class BeakerCacheTest(TemplateTest):
    def _regions(self):
        return beaker.cache.CacheManager(
            cache_regions={
                'short': {
                    'expire': 1,
                    'type': 'memory'
                },
                'long': {
                    'expire': 60,
                    'type': 'memory'
                }
            })
Ejemplo n.º 10
0
from .. import eq_
from unittest import TestCase
from nose import SkipTest

try:
    import mako
except ImportError:
    raise SkipTest("this test suite requires mako templates")

from mako.template import Template
from mako.cache import register_plugin
import mock

register_plugin("dogpile.cache", "dogpile.cache.plugins.mako_cache", "MakoPlugin")


class TestMakoPlugin(TestCase):
    def _mock_fixture(self):
        reg = mock.MagicMock()
        reg.get_or_create.return_value = "hello world"
        my_regions = {"myregion": reg}
        return {"cache_impl": "dogpile.cache", "cache_args": {"regions": my_regions}}, reg

    def test_basic(self):
        kw, reg = self._mock_fixture()
        t = Template('<%page cached="True" cache_region="myregion"/>hi', **kw)
        t.render()
        eq_(reg.get_or_create.call_count, 1)

    def test_timeout(self):
        kw, reg = self._mock_fixture()
Ejemplo n.º 11
0
from vilya.libs.store import mc, ONE_HOUR
from mako.cache import CacheImpl, register_plugin


class McCacheImpl(CacheImpl):
    """基于mc的页面cache实现"""

    def __init__(self, cache):
        self.cache = cache

    def get_or_create(self, key, creation_function, **kw):
        r = mc.get(key)
        if r is None:
            r = creation_function()
            expiretime = kw.get('timeout', ONE_HOUR)
            mc.set(key, r, expiretime)
        return r

    def set(self, key, value, **kw):
        expiretime = kw.get('timeout', ONE_HOUR)
        mc.set(key, value, expiretime)

    def get(self, key, **kw):
        return mc.get(key)

    def invalidate(self, key, **kw):
        mc.delete(key)


register_plugin("mccache", __name__, "McCacheImpl")