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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file fresco-fs-0.1.1.tar.gz
.
File metadata
- Download URL: fresco-fs-0.1.1.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4bd6f2dd356f024aee68c34a0818e533ca76803c187f431944938d960f3119ef |
|
MD5 | cc73d7269dab3b2d2c9de15457fbe418 |
|
BLAKE2b-256 | 66e6d7a46213f579fa62a38b0e27ad049f8e5b65c55f6010f4f1eae57e24ce3d |
File details
Details for the file fresco_fs-0.1.1-py2-none-any.whl
.
File metadata
- Download URL: fresco_fs-0.1.1-py2-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 2
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 432d2fa0a1d1dc9058cbeb0960a5059539e263c0434c7b6618376525877cab7b |
|
MD5 | 82d9cca195c7097df6c7a2712160cee4 |
|
BLAKE2b-256 | c721ac1f23136ba17e576d50749646db647ea43192ce5ca51b48476ebcc33f67 |