It's not a _real_ pyhesiodfs if it's not accessible to everyone else.
[invirt/packages/python-routefs.git] / routefs / dictfs.py
1 """
2 DictFS allows you to easily create read-only filesystems when the
3 file tree is known in advance.
4
5 To create your own DictFS descendent, simply override the files
6 property, which can be created either using the property
7 decorator, or just a simple assignment.
8
9 A dictionary represents a directory, with keys corresponding to
10 file names and the values corresponding to the file contents.
11 """
12
13 import routefs
14 from routes import Mapper
15 import os
16
17 class 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