From aec8bfa8231d9a6d4cf59468ae023b6c2b61d45b Mon Sep 17 00:00:00 2001 From: Evan Broder Date: Mon, 11 Aug 2008 23:09:08 -0700 Subject: [PATCH] Add DictFS, for when the filesystem tree is known in advance --- routefs/dictfs.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100755 routefs/dictfs.py diff --git a/routefs/dictfs.py b/routefs/dictfs.py new file mode 100755 index 0000000..a0d302c --- /dev/null +++ b/routefs/dictfs.py @@ -0,0 +1,54 @@ +#!/usr/bin/python + +""" +DictFS allows you to easily create read-only filesystems when the +file tree is known in advance. + +To create your own DictFS descendent, simply override the files +property, which can be created either using the property +decorator, or just a simple assignment. + +A dictionary represents a directory, with keys corresponding to +file names and the values corresponding to the file contents. +""" + +import routefs +from routes import Mapper +import os + +class DictFS(routefs.RouteFS): + @property + def files(self): + """ + This property should be overridden in your DictFS descendant + """ + return dict(Hello='World', + Directory=dict(a='a', b='b', c=routefs.Symlink('a'))) + + def make_map(self): + m = Mapper() + + m.connect('*path', controller='handler') + + return m + + def handler(self, path, **kwargs): + if path != '': + elements = path.split(os.path.sep) + else: + elements = [] + + try: + tree = self.files + for elt in elements: + tree = tree[elt] + except KeyError: + return + + if type(tree) is dict: + return routefs.Directory(tree.keys()) + else: + return tree + +if __name__ == '__main__': + routefs.main(DictFS) -- 1.7.9.5