b3daa8443828b950711d620e72710318527ce058
[invirt/packages/python-routefs.git] / routefs / examples / pyhesiodfs.py
1 #!/usr/bin/python
2
3
4 import hesiod
5 from routes import Mapper
6
7 import routefs
8
9
10 class PyHesiodFS(routefs.RouteFS):
11     controllers = ['getList', 'getReadme', 'getLocker']
12     def __init__(self, *args, **kwargs):
13         super(PyHesiodFS, self).__init__(*args, **kwargs)
14         self.fuse_args.add("allow_other", True)
15
16         self.cache = {}
17
18     def make_map(self):
19         m = Mapper()
20         m.connect('/', controller='getList')
21         m.connect('/README.txt', controller='getReadme')
22         m.connect('/{action}', controller='getLocker')
23         return m
24
25     def getLocker(self, action, **kwargs):
26         if action in self.cache:
27             return routefs.Symlink(self.cache[action])
28
29         try:
30             filsys = hesiod.FilsysLookup(action).filsys[0]
31             if filsys['type'] == 'AFS':
32                 self.cache[action] = filsys['location']
33                 return routefs.Symlink(self.cache[action])
34         except (TypeError, KeyError, IndexError):
35             return
36
37     def getList(self, **kwargs):
38         return self.cache.keys() + ['README.txt']
39
40     def getReadme(self, **kwargs):
41         return """
42 This is the pyHesiodFS FUSE automounter. To access a Hesiod filsys,
43 just access /mit/name.
44
45 If you're using the Finder, try pressing Cmd+Shift+G and then entering
46 /mit/name
47 """
48
49
50 if __name__ == '__main__':
51     routefs.main(PyHesiodFS)