source: trunk/packages/python-routefs/routefs/dictfs.py @ 1510

Last change on this file since 1510 was 981, checked in by broder, 16 years ago

Import RouteFS for new versions of ConsoleFS and RemconfFS

File size: 1.2 KB
Line 
1"""
2DictFS allows you to easily create read-only filesystems when the
3file tree is known in advance.
4
5To create your own DictFS descendent, simply override the files
6property, which can be created either using the property
7decorator, or just a simple assignment.
8
9A dictionary represents a directory, with keys corresponding to
10file names and the values corresponding to the file contents.
11"""
12
13import routefs
14from routes import Mapper
15import os
16
17class DictFS(routefs.RouteFS):
18    @property
19    def files(self):
20        """
21        This property should be overridden in your DictFS descendant
22        """
23        return dict()
24   
25    def make_map(self):
26        m = Mapper()
27       
28        m.connect('*path', controller='handler')
29       
30        return m
31   
32    def handler(self, path, **kwargs):
33        if path != '':
34            elements = path.split(os.path.sep)
35        else:
36            elements = []
37       
38        try:
39            tree = self.files
40            for elt in elements:
41                tree = tree[elt]
42        except KeyError:
43            return
44       
45        if type(tree) is dict:
46            return tree.keys()
47        else:
48            return tree
Note: See TracBrowser for help on using the repository browser.