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