Eliminate the "controllers" attribute from RouteFS classes.
[invirt/packages/python-routefs.git] / routefs / examples / homefs.py
1 #!/usr/bin/python
2 """
3 RouteFS Example: HomeFS
4
5 If you work on a system where home directories are on network storage
6 (i.e. not in /home), mount HomeFS on /home. It's an automounter that
7 will automatically create symlinks from user -> their homedir whenever
8 /home/user is accessed in any way.
9 """
10
11
12 import pwd
13
14 from routes import Mapper
15
16 import routefs
17
18
19 class HomeFS(routefs.RouteFS):
20     def __init__(self, *args, **kwargs):
21         super(HomeFS, self).__init__(*args, **kwargs)
22         self.cache = {}
23
24     def make_map(self):
25         m = Mapper()
26         m.connect('/', controller='getList')
27         m.connect('/{action}', controller='getUser')
28         return m
29
30     def getUser(self, action, **kwargs):
31         try:
32             if action not in self.cache:
33                 self.cache[action] = pwd.getpwnam(action).pw_dir
34             return routefs.Symlink(self.cache[action])
35         except KeyError:
36             return
37
38     def getList(self, **kwargs):
39         return self.cache.keys()
40
41
42 if __name__ == '__main__':
43     routefs.main(HomeFS)