class CocaineFS(routefs.RouteFS): def __init__(self, *args, **kwargs): super(CocaineFS, self).__init__(*args, **kwargs) # Initialize the Storage Service connection self.storage = cocaine.services.Service('storage') def make_map(self): result = routes.Mapper() result.connect('/', controller='namespaces') result.connect('/{namespace}', controller='keys') result.connect('/{namespace}/{key}', controller='read') return result def namespaces(self, **kwargs): return list(self.storage.find('system', ['public', 'namespace']).get()) def keys(self, namespace, **kwargs): return list(self.storage.find(namespace, ['public']).get()) def read(self, namespace, key, **kwargs): return str(self.storage.read(namespace, key).get()) if __name__ == "__main__": routefs.main(CocaineFS)
names = [] ids = self.twitter.friends.ids(screen_name=user) print "Retrieved friend ids for %s" % user for id in ids: # the api should support twitter.users.lookup(user_id=id1,id2...) to grab response in one shot, but this requires authentication. querying one-by-one in the meantime. #TODO this is NOT feasible. names.append( self.twitter.users.lookup( user_id=id)['screen_name'].encode('utf-8')) print "Retrieved friend names for %s" % user self.user_cache[user] = names except: self.user_cache[user] = None return self.user_cache[user] def list_statuses(self, user, friend, **kwargs): print "Retrieving status of %s" % friend if user not in self.status_cache: try: status = self.twitter.users.lookup( screen_name=friend)['status']['text'].encode('utf-8') print "status = %s" % status if status: status += '\n' self.status_cache[user] = status except: self.status_cache[user] = None return self.status_cache[user] if __name__ == '__main__': routefs.main(TwitFS)
from routes import Mapper import routefs class HomeFS(routefs.RouteFS): def __init__(self, *args, **kwargs): super(HomeFS, self).__init__(*args, **kwargs) self.cache = {} def make_map(self): m = Mapper() m.connect('/', controller='getList') m.connect('/{action}', controller='getUser') return m def getUser(self, action, **kwargs): try: if action not in self.cache: self.cache[action] = pwd.getpwnam(action).pw_dir return routefs.Symlink(self.cache[action]) except KeyError: return def getList(self, **kwargs): return self.cache.keys() if __name__ == '__main__': routefs.main(HomeFS)
#!/usr/bin/python import routefs class DictExFS(routefs.DictFS): files = dict(Hello='World', Directory=dict(a='a', b='b', c=routefs.Symlink('a'))) if __name__ == '__main__': routefs.main(DictExFS)
m.connect('/{action}', controller='getLocker') return m def getLocker(self, action, **kwargs): if action in self.cache: return routefs.Symlink(self.cache[action]) try: filsys = hesiod.FilsysLookup(action).filsys[0] if filsys['type'] == 'AFS': self.cache[action] = filsys['location'] return routefs.Symlink(self.cache[action]) except (TypeError, KeyError, IndexError): return def getList(self, **kwargs): return self.cache.keys() + ['README.txt'] def getReadme(self, **kwargs): return """ This is the pyHesiodFS FUSE automounter. To access a Hesiod filsys, just access /mit/name. If you're using the Finder, try pressing Cmd+Shift+G and then entering /mit/name """ if __name__ == '__main__': routefs.main(PyHesiodFS)
m = routes.Mapper() m.connect('/', controller='list_users') m.connect('/{user}', controller='list_tweets') m.connect('/{user}/{id}', controller='get_tweet') return m def list_users(self, **kwargs): """ List known usernames """ return [user for user, tweet in self.user_cache.iteritems() if tweet] def list_tweets(self, user, **kwargs): """ List tweet IDs in within the user's directory """ if user not in self.user_cache: try: self.user_cache[user] = self.twitterapi.GetUserTimeline(user) for tweet in self.user_cache[user]: self.tweet_cache[tweet.id] = tweet except: self.user_cache[user] = None return [str(tweet.id) for tweet in self.user_cache[user]] def get_tweet(self, user, id, **kwargs): """ Tweet text displayed as file content""" tweettext = safe_str(self.tweet_cache[int(id)].text)+'\n' return tweettext if __name__ == '__main__': routefs.main(TwitterRFS)
import cocaine.services, routes, routefs class CocaineFS(routefs.RouteFS): def __init__(self, *args, **kwargs): super(CocaineFS, self).__init__(*args, **kwargs) # Initialize the Storage Service connection self.storage = cocaine.services.Service('storage') def make_map(self): result = routes.Mapper() result.connect('/', controller = 'namespaces') result.connect('/{namespace}', controller = 'keys') result.connect('/{namespace}/{key}', controller = 'read') return result def namespaces(self, **kwargs): return list(self.storage.find('system', ['public', 'namespace']).get()) def keys(self, namespace, **kwargs): return list(self.storage.find(namespace, ['public']).get()) def read(self, namespace, key, **kwargs): return str(self.storage.read(namespace, key).get()) if __name__ == "__main__": routefs.main(CocaineFS)
def list_friends(self, user, **kwargs): print "Requesting user %s"%user if user not in self.user_cache: try: names = [] ids = self.twitter.friends.ids(screen_name=user) print "Retrieved friend ids for %s"%user for id in ids: # the api should support twitter.users.lookup(user_id=id1,id2...) to grab response in one shot, but this requires authentication. querying one-by-one in the meantime. #TODO this is NOT feasible. names.append(self.twitter.users.lookup(user_id=id)['screen_name'].encode('utf-8')) print "Retrieved friend names for %s"%user self.user_cache[user] = names except: self.user_cache[user] = None return self.user_cache[user] def list_statuses(self, user, friend, **kwargs): print "Retrieving status of %s"%friend if user not in self.status_cache: try: status = self.twitter.users.lookup(screen_name=friend)['status']['text'].encode('utf-8') print "status = %s"%status if status: status += '\n' self.status_cache[user] = status except: self.status_cache[user] = None return self.status_cache[user] if __name__ == '__main__': routefs.main(TwitFS)
m.connect(':action', controller='getLocker') return m def getLocker(self, action, **kwargs): if action in self.cache: return routefs.Symlink(self.cache[action]) try: filsys = hesiod.FilsysLookup(action).filsys[0] if filsys['type'] == 'AFS': self.cache[action] = filsys['location'] return routefs.Symlink(self.cache[action]) except (TypeError, KeyError, IndexError): return def getList(self, **kwargs): return self.cache.keys() + ['README.txt'] def getReadme(self, **kwargs): return """ This is the pyHesiodFS FUSE automounter. To access a Hesiod filsys, just access /mit/name. If you're using the Finder, try pressing Cmd+Shift+G and then entering /mit/name """ if __name__ == '__main__': routefs.main(PyHesiodFS)