Fix patch context for new upstream
[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     def __init__(self, *args, **kwargs):
12         super(PyHesiodFS, self).__init__(*args, **kwargs)
13         self.fuse_args.add("allow_other", True)
14
15         self.cache = {}
16
17     def make_map(self):
18         m = Mapper()
19         m.connect('/', controller='getList')
20         m.connect('/README.txt', controller='getReadme')
21         m.connect('/{action}', controller='getLocker')
22         return m
23
24     def getLocker(self, action, **kwargs):
25         if action in self.cache:
26             return routefs.Symlink(self.cache[action])
27
28         try:
29             filsys = hesiod.FilsysLookup(action).filsys[0]
30             if filsys['type'] == 'AFS':
31                 self.cache[action] = filsys['location']
32                 return routefs.Symlink(self.cache[action])
33         except (TypeError, KeyError, IndexError):
34             return
35
36     def getList(self, **kwargs):
37         return self.cache.keys() + ['README.txt']
38
39     def getReadme(self, **kwargs):
40         return """
41 This is the pyHesiodFS FUSE automounter. To access a Hesiod filsys,
42 just access /mit/name.
43
44 If you're using the Finder, try pressing Cmd+Shift+G and then entering
45 /mit/name
46 """
47
48
49 if __name__ == '__main__':
50     routefs.main(PyHesiodFS)