On Sun, Aug 23, 2015 at 7:24 AM, Mike Orr <[email protected]> wrote:
> Has anyone used traversal over a filesystem for non-static content?
> I'm thinking of putting my HTML content in files to get Git versioning
> and text-editor friendliness, so that each file would represent a URL
> but not be served as-is (you'd still put a site template around it and
> plug in metadata). Interspersed there would be true static files
> (e.g., images), although I'd like to have dynamic thumbnails.

I've got basic traversal over a filesystem working. This demo serves a
static directory. Does anyone have any critiques of this approach? If
not, I'll write it up for the Pyramid Cookbook.

--
Mike Orr <[email protected]>

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/pylons-discuss.
For more options, visit https://groups.google.com/d/optout.
"""Filesystem traversal demo."""

import os

import pyramid.config
import pyramid.httpexceptions
import pyramid.response
import waitress

WWW_HOME = "/home/sluggo/exp/scrapping/www"   # SET ME: The directory to serve.

# Resources

def get_root(request):
    return Directory(None, "", WWW_HOME)

class FSResource(object):
    def __init__(self, parent, name, path):
        self.__parent__ = parent
        self.__name__ = name
        self.path = path

class Directory(FSResource):
    def __getitem__(self, key):
        path = os.path.join(self.path, key)
        if os.path.isdir(path):
            return Directory(self, key, path)
        elif os.path.isfile(path):
            return File(self, key, path)
        else:
            raise KeyError(key)

class File(FSResource):
    pass

# Views

def directory(context, request):
    index = context["index.html"]
    if os.path.exists(index.path):
        return pyramid.response.FileResponse(index.path, request)
    else:
        return pyramid.httpexceptions.HTTPNotFound()

def file_(context, request):
    return pyramid.response.FileResponse(context.path, request)

# Application

def make_app():
    config = pyramid.config.Configurator()
    config.set_root_factory(get_root)
    config.add_view(directory, context=Directory)
    config.add_view(file_, context=File)
    return config.make_wsgi_app()

def main():
    app = make_app()
    waitress.serve(app, host="127.0.0.1", port=5000)

if __name__ == "__main__":  main()

Reply via email to