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