def base_link(ext, *sources, **options): from pylons import config from pylons.decorators.cache import beaker_cache combined = options.pop('combined', False) minified = options.pop('minified', False) beaker_options = options.pop('beaker_kwargs', False) fs_root = config.get('pylons.paths').get('static_files') if not (config.get('debug', False) or options.get('builtins', False)): if beaker_options: beaker_kwargs.update(beaker_options) if combined: sources = beaker_cache(**beaker_kwargs)(combine_sources)( list(sources), ext, fs_root) if minified: sources = beaker_cache(**beaker_kwargs)(minify_sources)( list(sources), '.min.' + ext, fs_root) if 'js' in ext: return __javascript_link(*sources, **options) if 'css' in ext: return __stylesheet_link(*sources, **options)
class CacheController(BaseController): def test_default_cache_decorator(self): g.counter += 1 return Response('Counter=%s' % g.counter) test_default_cache_decorator = beaker_cache( key=None)(test_default_cache_decorator) def test_get_cache_decorator(self): g.counter += 1 return Response('Counter=%s' % g.counter) test_get_cache_decorator = beaker_cache( key="param", query_args=True)(test_get_cache_decorator) def test_expire_cache_decorator(self): g.counter += 1 return Response('Counter=%s' % g.counter) test_expire_cache_decorator = beaker_cache( expire=8)(test_expire_cache_decorator) def test_key_cache_decorator(self, id): g.counter += 1 return Response('Counter=%s, id=%s' % (g.counter, id)) test_key_cache_decorator = beaker_cache(key="id")(test_key_cache_decorator) def test_keyslist_cache_decorator(self, id, id2="123"): g.counter += 1 return Response('Counter=%s, id=%s' % (g.counter, id)) test_keyslist_cache_decorator = beaker_cache( key=["id", "id2"])(test_keyslist_cache_decorator)
def test_response_cache_func(self, use_cache_status=True): pylons.response.status_int = 404 def func(): pylons.app_globals.counter += 1 return 'Counter=%s' % pylons.app_globals.counter if use_cache_status: func = beaker_cache(key=None)(func) else: func = beaker_cache(key=None, cache_response=False)(func) return func()
def u_cache(**kwargs): if 'expire' in kwargs: # Vary expiration times by +/- 20%. factor = 0.8 + (random.random() * 0.4) kwargs['expire'] = int(kwargs['expire'] * factor) kwargs['cache_response'] = False return beaker_cache(**kwargs)
def test_default_cache_decorator_func(self): def func(): pylons.app_globals.counter += 1 return 'Counter=%s' % pylons.app_globals.counter func = beaker_cache(key=None)(func) return func()
def pre_cache(*args, **kwargs): if config["debug"]: decorate = lambda f: f else: decorate = beaker_cache(*args, **kwargs) return decorate
def base_link(ext, *sources, **options): """Base function that glues all logic together. It parses options and calls :func:`process_sources`. :param ext: js or css helper :param sources: a list of source files. Can be a dicts with keys: file='/script.js': a path to your file minify=False|'strip'|'minify': should this file be minified, stripped or left as it is? dest='/script.min.js': minified file destination (needed if files are not combined) Alternatively you can provide strings in which case they will be treated as {file='<your string>', minify=False} In this case the files will not be minified and 'combined' must be provided :param combined: the combined file name if the files need to be combined. Otherwise they have to have 'dest' keys :param beaker_kwargs: Beaker options to pass to caching decorators :param timestamp: append timestamp to links, eg. test.js?t=123012343 :type ext: string :type sources: list of strings or dicts or any combination :type combined: string :type beaker_kwargs: dict :type timestamp: bool :returns: HTML source code .. versionadded:: 0.3.1 `beaker_kwargs` parameter .. versionadded:: 0.3.2 `combined_filename` parameter .. versionadded:: 0.3.5 `timestamp` parameter """ combined = options.pop('combined', False) timestamp = options.pop('timestamp', False) beaker_options = options.pop('beaker_kwargs', False) fs_root = config.get('pylons.paths').get('static_files') sources = list(sources) if not (config.get('debug', False) or options.get('builtins', False)): if beaker_options: beaker_kwargs.update(beaker_options) # use beaker_cache to cache the returned sources sources = beaker_cache(**beaker_kwargs)(process_sources)( sources, ext, fs_root, combined, timestamp) else: for i in range(len(sources)): if isinstance(sources[i], dict): sources[i] = sources[i]['file'] if 'js' in ext: return __javascript_link(*sources, **options) if 'css' in ext: return __stylesheet_link(*sources, **options)
def base_link(ext, *sources, **options): from pylons import config from pylons.decorators.cache import beaker_cache combined = options.pop('combined', False) minified = options.pop('minified', False) beaker_options = options.pop('beaker_kwargs', False) fs_root = config.get('pylons.paths').get('static_files') if not (config.get('debug', False) or options.get('builtins', False)): if beaker_options: beaker_kwargs.update(beaker_options) if combined: sources = beaker_cache(**beaker_kwargs)(combine_sources)(list(sources), ext, fs_root) if minified: sources = beaker_cache(**beaker_kwargs)(minify_sources)(list(sources), '.min.' + ext, fs_root) if 'js' in ext: return __javascript_link(*sources, **options) if 'css' in ext: return __stylesheet_link(*sources, **options)
class CacheController(WSGIController): def test_default_cache_decorator(self): pylons.g.counter += 1 return Response('Counter=%s' % pylons.g.counter) test_default_cache_decorator = beaker_cache( key=None)(test_default_cache_decorator) def test_get_cache_decorator(self): pylons.g.counter += 1 return Response('Counter=%s' % pylons.g.counter) test_get_cache_decorator = beaker_cache( key="param", query_args=True)(test_get_cache_decorator) def test_get_cache_default(self): pylons.g.counter += 1 return Response('Counter=%s' % pylons.g.counter) test_get_cache_default = beaker_cache( query_args=True)(test_get_cache_default) def test_expire_cache_decorator(self): pylons.g.counter += 1 return Response('Counter=%s' % pylons.g.counter) test_expire_cache_decorator = beaker_cache( expire=1)(test_expire_cache_decorator) def test_key_cache_decorator(self, id): pylons.g.counter += 1 return Response('Counter=%s, id=%s' % (pylons.g.counter, id)) test_key_cache_decorator = beaker_cache(key="id")(test_key_cache_decorator) def test_keyslist_cache_decorator(self, id, id2="123"): pylons.g.counter += 1 return Response('Counter=%s, id=%s' % (pylons.g.counter, id)) test_keyslist_cache_decorator = beaker_cache( key=["id", "id2"])(test_keyslist_cache_decorator) def test_header_cache(self): pylons.response.headers['Content-Type'] = 'text/plain' pylons.response.headers['x-powered-by'] = 'pylons' return "Hello folks, time is %s" % time.time() test_header_cache = beaker_cache()(test_header_cache)
def base_link(ext, *sources, **options): """Base function that glues all logic together. It parses options and calls :func:`minify_sources` or :func:`combine_sources` if apropriate. :param ext: js or css helper :param sources: paths to your files :param combined: if True combines sources into one file :param minified: if True minifies javascript or css files :param beaker_kwargs: Beaker options to pass to caching decorators :param combined_filename: filename that will be used when combining files :param timestamp: append `time.time` timestamp to file, eg. test.js?t=123012343 :param strip_prefix: prefix to be stripped from `sources` URL list :type ext: string :type sources: string :type combined_filename: keyword arg :type combined: keyword arg :type minified: keyword arg :type beaker_kwargs: dict :type timestamp: bool :type strip_prefix: string :returns: HTML source code .. versionadded:: 0.3.1 `beaker_kwargs` parameter .. versionadded:: 0.3.2 `combined_filename` parameter .. versionadded:: 0.3.5 `timestamp` parameter .. versionadded:: 0.3.6 `strip_prefix` parameter """ filename = options.pop('combined_filename', False) combined = options.pop('combined', False) minified = options.pop('minified', False) timestamp = options.pop('timestamp', False) strip_prefix = options.pop('strip_prefix', False) beaker_options = options.pop('beaker_kwargs', False) fs_root = config.get('pylons.paths').get('static_files') stripped_sources = [] if filename and not combined: raise ValueError("combined_filename=True specifies filename for" " combined=True parameter which is not set.") if not (config.get('debug', False) or options.get('builtins', False)): if beaker_options: beaker_kwargs.update(beaker_options) if strip_prefix: stripped_sources = [source.replace(strip_prefix, '', 1) for source in sources if source.startswith(strip_prefix)] if stripped_sources: sources = stripped_sources if combined: # use beaker_cache decorator to cache the return value sources = beaker_cache(**beaker_kwargs)\ (combine_sources)(list(sources), ext, fs_root, filename, timestamp) if minified: # use beaker_cache decorator to cache the return value sources = beaker_cache(**beaker_kwargs)\ (minify_sources)(list(sources), '.min.' + ext, fs_root, timestamp) if stripped_sources: sources = [strip_prefix + source for source in sources] sources = [url(source) for source in sources] if 'js' in ext: return __javascript_link(*sources, **options) if 'css' in ext: return __stylesheet_link(*sources, **options)