Update PyHesiodFS example to include controllers list
[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 import pwd
12 import routefs
13 from routes import Mapper
14
15 class HomeFS(routefs.RouteFS):
16     def __init__(self, *args, **kwargs):
17         super(HomeFS, self).__init__(*args, **kwargs)
18         self.cache = {}
19     
20     def make_map(self):
21         m = Mapper()
22         m.connect('', controller='getList')
23         m.connect(':action', controller='getUser')
24         return m
25     
26     def getUser(self, action, **kwargs):
27         try:
28             if action not in self.cache:
29                 self.cache[action] = pwd.getpwnam(action).pw_dir
30             return routefs.Symlink(self.cache[action])
31         except KeyError:
32             return
33     
34     def getList(self, **kwargs):
35         return self.cache.keys()
36
37 if __name__ == '__main__':
38     routefs.main(HomeFS)