Skip to main content

Filesystem resources for fresco

Project description

fresco-fs: filesystem resources for the fresco framework

fresco-fs provides a flexible framework to load and serve content from a filesystem directory, integrated with the fresco web framework.

This package was developed for the Skot.be kot listings site, where it is used to serve some of the site’s static content.

Example:

from fresco import FrescoApp
from fresco_fs import FSResources
import markdown


def serve_markdown(path):
    """ Take a path to a .md file and turn it into an HTML response """
    with open(path, 'r', encoding='utf-8') as f:
        return Response(markdown.markdown(f.read()))

pages = FSResources(

        search_path=['path/to/content/files/'],

        # Each of extension will be added to the request filename in
        # turn until one matches. So a request for '/mypage' would try
        # '/mypage.md' then '/mypage.txt'.
        # (Note that a request for '/mypage.txt' would be translated into
        #  '/mypage.txt.md' and '/mypage.txt.txt'.)
        search_extensions=['.md', '.txt'],

        # A function taking a mapped path and returning a fresco Response.
        responder=serve_markdown,

        # The route name to use. Routes can be generated with
        # `urlfor('pages', path='mypage')`
        route_name='pages')

app = FrescoApp()
app.include('/', pages)

FSResources allows paths to be rewritten on ingress by passing a rewriter function to the constructor. This function must take the requested path and return zero or more rewritten paths. Each rewritten path is then tested in order, and the first existing path is served.

For example to serve different content depending on language, you could write a function like this:

def try_i18n_paths(virtualpath):
    lang = get_active_language()
    return ['{}.{}'.format(virtualpath, lang), virtualpath]

The FSResources constructor would then look like this:

pages = FSResources(
        search_path=['path/to/content/files/'],
        search_extensions=['.md', '.txt'],
        responder=serve_markdown,
        rewriter=try_i18n_paths)

0.1 (released 2017-03-06)

Initial release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fresco-fs-0.1.1.tar.gz (7.3 kB view hashes)

Uploaded Source

Built Distribution

fresco_fs-0.1.1-py2-none-any.whl (5.3 kB view hashes)

Uploaded Python 2

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page