Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

executable file 57 lines (42 sloc) 1.662 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#!/usr/bin/python


from github2 import client
import routes
import routefs


class GithubFS(routefs.RouteFS):
    def __init__(self, *args, **kwargs):
        super(GithubFS, self).__init__(*args, **kwargs)

        self.user_cache = {}
        self.branch_cache = {}
        self.github = client.Github()

    def make_map(self):
        m = routes.Mapper()
        m.connect('/', controller='list_users')
        m.connect('/{user}', controller='list_repos')
        m.connect('/{user}/{repo}', controller='list_branches')
        m.connect('/{user}/{repo}/{branch}', controller='get_branch')
        return m

    def list_users(self, **kwargs):
        return [user for user, projects in self.user_cache.iteritems() if projects]

    def list_repos(self, user, **kwargs):
        if user not in self.user_cache:
            try:
                self.user_cache[user] = [r.name for r in self.github.repos.list(user)]
            except:
                self.user_cache[user] = None

        return self.user_cache[user]

    def list_branches(self, user, repo, **kwargs):
        project = '%s/%s' % (user, repo)
        if project not in self.branch_cache:
            try:
                self.branch_cache[project] = self.github.repos.branches(project)
            except:
                self.branch_cache[project] = {}

        return self.branch_cache[project].keys()

    def get_branch(self, user, repo, branch, **kwargs):
        project = '%s/%s' % (user, repo)
        sha1 = self.branch_cache.get(project, {}).get(branch, None)
        if sha1:
            sha1 += '\n'
        return sha1


if __name__ == '__main__':
    routefs.main(GithubFS)
Something went wrong with that request. Please try again.