In my slowly-renovating personal site I've got traversal over a directory of HTML files working. My code so far is attached. It traverses to an HTML file and serves it as a Mako template. It supports an implicit "index.html" in directories, and appends ".html" to the filename if missing. Longer term I want to extract the resource class and default view from meta tags in the file.
<meta name="resource": class name, looked up in a dict, default to a generic HTML resource. <meta name="view": default view name if user doesn't specify. Defaults to basic HTML view. So I have two questions: 1) Right now I'm doing the 'context["index.html"]' lookup in the view because I don't know how to do it during traversal. The problem is that when I'm in a traversal level that's a directory, there are two possible outcomes. If I'm in the middle of the URL, I should allow it to traverse normally to the next subdirectory or file. But if I'm at the end of the URL (no child but maybe a view afterward), then I want to implicitly append the "index.html" file as a child. One, can I do that, and two, is it possible to tell in a traversal level whether it's the last one or not (i.e.,whether there's a child in the URL)? 2) Is it possible for a resource to offer a default view? Kotti has some feature where a resource (a database record) has a default view field that can be customized by the site admin. I don't know how that works. Is it feasable to do this without hacking up the view lookup mechanism severely? -- 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 pylons-discuss+unsubscr...@googlegroups.com. To post to this group, send email to pylons-discuss@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/CAH9f%3DuoGaGYD6UZ6gtRvD9sL6oAWBnmPZpp_Mm5GSJrvZQnZcg%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
import logging import os import pathlib log = logging.getLogger(__name__) def includeme(config): config.set_root_factory(get_root) def get_root(request): p = pathlib.Path(request.registry.settings["lair.pages_dir"]) return Directory(None, "", p) 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 = self.path / key if path.is_dir(): return Directory(self, key, path) if path.is_file(): return File(self, key, path) filename = path.name + ".html" path = path.with_name(filename) if path.is_file(): return File(self, key, path) raise KeyError(key) class File(FSResource): pass
import logging import pyramid.httpexceptions import pyramid.renderers #import pyramid.response import pyramid_mako import lair.resources log = logging.getLogger(__name__) def includeme(config): rc = lair.resources config.include(pyramid_mako) config.add_mako_renderer(".html") config.add_static_view("static", "lair:static", cache_max_age=3600) config.add_view(directory, context=rc.Directory) config.add_view(file, context=rc.File) def directory(context, request): index = context["index.html"] if index.path.exists(): return file(index, request) else: return pyramid.httpexceptions.HTTPNotFound() def file(context, request): # 'file' is not a builtin in Python 3. template_file = str(context.path) log.debug("template file = %s", template_file) data = {} return pyramid.renderers.render_to_response(template_file, data, request)