Exemple #1
0
def cache_file_setting(output_dir, cache_file):
    """ Convenience method for converting the cache_file setting """
    if cache_file is None:
        return os.path.join(resource_spec(output_dir), '.pike-cache')
    else:
        trimmed = cache_file.strip().lower()
        if trimmed == 'none':
            return None
        else:
            return resource_spec(cache_file)
def configure(app):
    """ Configure a flask app to use pike """
    output_dir = app.config.get('PIKE_OUTPUT_DIR', 'gen')
    watch = app.config.get('PIKE_WATCH', True)
    url_prefix = app.config.get('PIKE_URL_PREFIX', 'gen')
    serve_files = app.config.get('PIKE_SERVE_FILES', True)
    cache_file = cache_file_setting(output_dir,
                                    app.config.get('PIKE_CACHE_FILE'))
    load_file = app.config.get('PIKE_LOAD_FILE')

    if load_file is None:
        env = get_environment(watch, cache_file, url_prefix, output_dir)
    else:
        env = Environment()
        env.load(resource_spec(load_file))
    app.jinja_env.add_extension("pike.ext.JinjaExtension")
    app.jinja_env.pike = env

    if serve_files:
        @app.route('/%s/<path:filename>' % url_prefix)
        def serve_asset(filename):
            """ A view that will serve generated assets """
            fullpath = env.lookup(filename)
            if fullpath and os.path.exists(fullpath):
                if os.path.isabs(fullpath):
                    directory, fullpath = os.path.split(fullpath)
                else:
                    directory = os.getcwd()
                return send_from_directory(directory, fullpath)
            else:
                return abort(404)
    return env
def includeme(config):
    """ Set up pyramid app with pike """
    settings = config.get_settings()

    output_dir = settings.get('pike.output_dir', 'gen')
    watch = asbool(settings.get('pike.watch', True))
    path = settings.get('pike.url_prefix', 'gen')
    serve_files = asbool(settings.get('pike.serve_files', True))
    static_view = asbool(settings.get('pike.static_view', False))
    cache_file = cache_file_setting(output_dir,
                                    settings.get('pike.cache_file'))
    load_file = settings.get('pike.load_file')

    if serve_files:
        if static_view:
            abspath = os.path.abspath(resource_spec(output_dir))
            config.add_static_view(name=path, path=abspath,
                                   cache_max_age=31556926)
        else:
            config.add_route('pike_assets', '/%s/*path' % path)
            config.add_view(serve_asset, route_name='pike_assets')

    if load_file is None:
        env = get_environment(watch, cache_file, path, output_dir)
    else:
        env = Environment()
        env.load(resource_spec(load_file))

    config.registry.pike_env = env
    config.add_directive('get_pike_env', lambda c: c.registry.pike_env)

    # If pyramid_jinja2 has already been included, take care of the
    # configuration automatically
    try:
        config.add_jinja2_extension('pike.ext.JinjaExtension')
        config.get_jinja2_environment().pike = env
    except AttributeError:
        pass
Exemple #4
0
 def __init__(self, root):
     super(SourceNode, self).__init__()
     self.root = resource_spec(root)
Exemple #5
0
 def __init__(self, base_dir=os.curdir, debug=False):
     super(WriteNode, self).__init__()
     self.base_dir = resource_spec(base_dir)
     self.name = 'write(%r)' % self.base_dir
     self.debug = debug