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