Eliminate make_map in favor of a simpler map property.
[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     controllers = ['getList', 'getUser']
17     def __init__(self, *args, **kwargs):
18         super(HomeFS, self).__init__(*args, **kwargs)
19         self.cache = {}
20
21     @property
22     def map(self):
23         m = Mapper()
24         m.connect('', controller='getList')
25         m.connect(':action', controller='getUser')
26         return m
27     
28     def getUser(self, action, **kwargs):
29         try:
30             if action not in self.cache:
31                 self.cache[action] = pwd.getpwnam(action).pw_dir
32             return routefs.Symlink(self.cache[action])
33         except KeyError:
34             return
35     
36     def getList(self, **kwargs):
37         return self.cache.keys()
38
39 if __name__ == '__main__':
40     routefs.main(HomeFS)