예제 #1
0
def compile_templates(argv=None):
    """Compiles templates for better performance. This is a command line
    script. From the buildout directory, run:

        bin/jinja2_compile

    It will compile templates from the directory configured for 'templates_dir'
    to the one configured for 'templates_compiled_target'.

    At this time it doesn't accept any arguments.
    """
    if argv is None:
        argv = sys.argv

    cwd = os.getcwd()
    app_path = os.path.join(cwd, 'app')
    gae_path = os.path.join(cwd, 'etc/parts/google_appengine')

    extra_paths = [
        app_path,
        gae_path,
        # These paths are required by the SDK.
        os.path.join(gae_path, 'lib', 'antlr3'),
        os.path.join(gae_path, 'lib', 'django'),
        os.path.join(gae_path, 'lib', 'ipaddr'),
        os.path.join(gae_path, 'lib', 'webob'),
        os.path.join(gae_path, 'lib', 'yaml', 'lib'),
    ]

    sys.path = extra_paths + sys.path

    from config import config

    app = make_wsgi_app(config)
    template_path = get_config('tipfy.ext.jinja2', 'templates_dir')
    compiled_path = get_config('tipfy.ext.jinja2', 'templates_compiled_target')

    if compiled_path is None:
        raise ValueError('Missing configuration key to compile templates.')

    if isinstance(template_path, basestring):
        # A single path.
        source = os.path.join(app_path, template_path)
    else:
        # A list of paths.
        source = [os.path.join(app_path, p) for p in template_path]

    target = os.path.join(app_path, compiled_path)

    # Set templates dir and deactivate compiled dir to use normal loader to
    # find the templates to be compiled.
    app.config['tipfy.ext.jinja2']['templates_dir'] = source
    app.config['tipfy.ext.jinja2']['templates_compiled_target'] = None

    if target.endswith('.zip'):
        zip_cfg = 'deflated'
    else:
        zip_cfg = None

    old_list_templates = FileSystemLoader.list_templates
    FileSystemLoader.list_templates = list_templates

    env = get_env()
    env.compile_templates(target, extensions=None, filter_func=filter_templates,
                          zip=zip_cfg, log_function=logger,
                          ignore_errors=False, py_compile=False)

    FileSystemLoader.list_templates = old_list_templates
예제 #2
0
파일: mark.py 프로젝트: cklzqw/gaeaib
def install_jinja2():
  from tipfy.ext.jinja2 import get_env
  environment = get_env()
  environment.filters['wakaba'] = jinja2
예제 #3
0
def compile_templates(argv=None):
    """Compiles templates for better performance. This is a command line
    script. From the buildout directory, run:

        bin/jinja2_compile

    It will compile templates from the directory configured for 'templates_dir'
    to the one configured for 'templates_compiled_target'.

    At this time it doesn't accept any arguments.
    """
    if argv is None:
        argv = sys.argv

    cwd = os.getcwd()
    app_path = os.path.join(cwd, 'app')
    gae_path = os.path.join(cwd, 'etc/parts/google_appengine')

    extra_paths = [
        app_path,
        gae_path,
        # These paths are required by the SDK.
        os.path.join(gae_path, 'lib', 'antlr3'),
        os.path.join(gae_path, 'lib', 'django'),
        os.path.join(gae_path, 'lib', 'ipaddr'),
        os.path.join(gae_path, 'lib', 'webob'),
        os.path.join(gae_path, 'lib', 'yaml', 'lib'),
    ]

    sys.path = extra_paths + sys.path

    from config import config

    app = make_wsgi_app(config)
    template_path = get_config('tipfy.ext.jinja2', 'templates_dir')
    compiled_path = get_config('tipfy.ext.jinja2', 'templates_compiled_target')

    if compiled_path is None:
        raise ValueError('Missing configuration key to compile templates.')

    if isinstance(template_path, basestring):
        # A single path.
        source = os.path.join(app_path, template_path)
    else:
        # A list of paths.
        source = [os.path.join(app_path, p) for p in template_path]

    target = os.path.join(app_path, compiled_path)

    # Set templates dir and deactivate compiled dir to use normal loader to
    # find the templates to be compiled.
    app.config['tipfy.ext.jinja2']['templates_dir'] = source
    app.config['tipfy.ext.jinja2']['templates_compiled_target'] = None

    if target.endswith('.zip'):
        zip_cfg = 'deflated'
    else:
        zip_cfg = None

    old_list_templates = FileSystemLoader.list_templates
    FileSystemLoader.list_templates = list_templates

    env = get_env()
    env.compile_templates(target,
                          extensions=None,
                          filter_func=filter_templates,
                          zip=zip_cfg,
                          log_function=logger,
                          ignore_errors=False,
                          py_compile=False)

    FileSystemLoader.list_templates = old_list_templates
예제 #4
0
파일: rainbow.py 프로젝트: a37912/gaeaib
def install_jinja2():
  from tipfy.ext.jinja2 import get_env
  environment = get_env()
  environment.filters['rainbow'] = rainbow
예제 #5
0
파일: warmup.py 프로젝트: a37912/gaeaib
import os
import logging

from tipfy import RequestHandler, Response, get_config
from tipfy.ext.jinja2 import get_env
from urls import get_rules
from werkzeug.utils import import_string

jinja = get_env()

def warm_jinja2():
  for tpldir in get_config('tipfy.ext.jinja2', 'templates_dir'):
    warm_jinja2_tpldir(tpldir)

def warm_jinja2_tpldir(tpldir):
  for tpl in os.listdir(tpldir):
    logging.info("warm load tpl: %r" % tpl)

    jinja.get_template(tpl)

def warm_urls():
  for rule in get_rules():
    logging.info("warm rule: %r" % rule.handler)
    import_string(rule.handler)

class Do(RequestHandler):
  def get(self):
    warm_jinja2()
    warm_urls()
      
    return Response("ni-paaa")
예제 #6
0
파일: jinja.py 프로젝트: aristidb/cppbash
 def pre_dispatch_handler(self):
     env = get_env()
     env.autoescape = True
     env.filters['linebreaks'] = linebreaks
     env.filters['urlparameter'] = urlparameter
     env.filters['atomdatetime'] = atomdatetime