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