public
Description: Python interface for talking to the github API
Clone URL: git://github.com/dustin/py-github.git
pep8 compliance
ashcrow (author)
Sun Aug 31 21:19:08 -0700 2008
dustin (committer)
Sun Sep 07 21:01:07 -0700 2008
commit  1be68604d61e216cdcd069780e9f967630822924
tree    4e1c22acace7402db5f2320b19f533e058cd3867
parent  f46e25fbaa8923e169ddce7afbb2dc8339cc50d6
...
4
5
6
 
7
8
9
10
11
 
12
13
14
...
16
17
18
19
20
 
...
4
5
6
7
8
9
10
11
12
13
14
15
16
...
18
19
20
 
21
22
0
@@ -4,11 +4,13 @@ import sys
0
 
0
 import github
0
 
0
+
0
 def usage():
0
     """display the usage and exit"""
0
     print "Usage: %s keyword [keyword...]" % (sys.argv[0])
0
     sys.exit(1)
0
 
0
+
0
 if __name__ == '__main__':
0
     g = github.GitHub()
0
     if len(sys.argv) < 2:
0
@@ -16,4 +18,4 @@ if __name__ == '__main__':
0
     res = g.search(' '.join(sys.argv[1:]))
0
 
0
     for repo in res:
0
- print "Found %s at %s" % (repo.name, repo.url)
0
\ No newline at end of file
0
+ print "Found %s at %s" % (repo.name, repo.url)
...
15
16
17
18
 
19
20
21
22
23
24
25
 
26
27
28
 
29
30
31
...
39
40
41
42
 
43
44
45
46
47
48
 
49
50
51
52
53
 
54
55
56
57
 
58
59
60
...
65
66
67
 
68
69
70
71
72
 
73
74
75
76
77
78
79
 
 
 
80
81
82
83
 
84
85
86
87
88
89
 
 
90
91
92
...
104
105
106
 
107
108
109
...
113
114
115
 
116
117
118
...
123
124
125
 
126
127
128
129
130
131
132
133
 
 
 
 
134
135
136
...
165
166
167
 
168
169
170
171
172
 
173
174
175
176
177
 
 
178
179
180
181
182
 
183
184
 
185
186
187
188
189
 
190
191
 
192
193
194
195
196
 
197
198
 
199
200
201
202
203
 
204
205
206
...
15
16
17
 
18
19
20
21
22
23
24
 
25
26
27
 
28
29
30
31
...
39
40
41
 
42
43
44
45
46
47
48
49
50
51
52
53
 
54
55
56
57
 
58
59
60
61
...
66
67
68
69
70
71
72
73
 
74
75
76
77
78
 
 
 
79
80
81
82
83
84
85
86
87
88
89
90
 
 
91
92
93
94
95
...
107
108
109
110
111
112
113
...
117
118
119
120
121
122
123
...
128
129
130
131
132
133
134
135
 
 
 
 
136
137
138
139
140
141
142
...
171
172
173
174
175
176
177
178
 
179
180
181
182
 
 
183
184
185
186
187
188
 
189
190
 
191
192
193
194
195
 
196
197
 
198
199
200
201
202
 
203
204
 
205
206
207
208
209
 
210
211
212
213
0
@@ -15,17 +15,17 @@ Copyright (c) 2007 Dustin Sallings <dustin@spy.net>
0
 """
0
 
0
 # Copyright (c) 2005 Dustin Sallings <dustin@spy.net>
0
-#
0
+#
0
 # Permission is hereby granted, free of charge, to any person obtaining a copy
0
 # of this software and associated documentation files (the "Software"), to deal
0
 # in the Software without restriction, including without limitation the rights
0
 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0
 # copies of the Software, and to permit persons to whom the Software is
0
 # furnished to do so, subject to the following conditions:
0
-#
0
+#
0
 # The above copyright notice and this permission notice shall be included in
0
 # all copies or substantial portions of the Software.
0
-#
0
+#
0
 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0
 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0
 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0
@@ -39,22 +39,23 @@ Copyright (c) 2007 Dustin Sallings <dustin@spy.net>
0
 # GAE friendly URL detection (theoretically)
0
 try:
0
     import urllib2
0
- default_fetcher=urllib2.urlopen
0
+ default_fetcher = urllib2.urlopen
0
 except LoadError:
0
     pass
0
 
0
 import xml
0
 import xml.dom.minidom
0
 
0
+
0
 class Repository(object):
0
     """A github repository."""
0
 
0
     def __init__(self, el):
0
- ch=el.firstChild
0
+ ch = el.firstChild
0
         while ch:
0
             if ch.nodeType != xml.dom.Node.TEXT_NODE and ch.firstChild:
0
                 type = 'string'
0
- if ch.attributes.has_key('type'):
0
+ if 'type' in ch.attributes.keys():
0
                     type = ch.attributes['type'].value
0
                 if type == 'integer':
0
                     self.__dict__[ch.localName] = int(ch.firstChild.data)
0
@@ -65,28 +66,30 @@ class Repository(object):
0
     def __repr__(self):
0
         return "<<Repository %s>>" % self.name
0
 
0
+
0
 class Person(object):
0
     """A person."""
0
 
0
     def __init__(self, el):
0
- ch=el.firstChild
0
+ ch = el.firstChild
0
         while ch:
0
             if ch.nodeType != xml.dom.Node.TEXT_NODE:
0
                 if ch.localName != 'repositories':
0
                     self.__dict__[ch.localName] = ch.firstChild.data
0
- ch=ch.nextSibling
0
- repos=[Repository(el) for el in el.getElementsByTagName('repository')]
0
- self.repos=dict([(r.name, r) for r in repos])
0
+ ch = ch.nextSibling
0
+ rps = [Repository(el) for el in el.getElementsByTagName('repository')]
0
+ self.repos = dict([(r.name, r) for r in rps])
0
 
0
     def __repr__(self):
0
         return "<<Person %s <%s>>>" % (self.name, self.email)
0
 
0
+
0
 class SearchResults(object):
0
     """Search results."""
0
 
0
     def __init__(self, el):
0
- ch=el.firstChild
0
- self.repos=[Repository(el)
0
+ ch = el.firstChild
0
+ self.repos = [Repository(el)
0
             for el in el.getElementsByTagName('repository')]
0
 
0
     def __iter__(self):
0
@@ -104,6 +107,7 @@ class SearchResults(object):
0
     def __repr__(self):
0
         return "<<SearchResults with %d repos>>" % len(self.repos)
0
 
0
+
0
 class User(Person):
0
     """A github user."""
0
 
0
@@ -113,6 +117,7 @@ class User(Person):
0
     def __repr__(self):
0
         return "<<User %s with %d repos>>" % (self.login, len(self.repos))
0
 
0
+
0
 class FileModification(object):
0
     """Object representing a specific file modification."""
0
 
0
@@ -123,14 +128,15 @@ class FileModification(object):
0
     def __repr__(self):
0
         return "<<FileModification: %s>>" % self.filename
0
 
0
+
0
 class Commit(object):
0
     """A single commit."""
0
 
0
     def __init__(self, el):
0
- ch=el.firstChild
0
- self.removed=[]
0
- self.added=[]
0
- self.modified=[]
0
+ ch = el.firstChild
0
+ self.removed = []
0
+ self.added = []
0
+ self.modified = []
0
         while ch:
0
             if ch.nodeType != xml.dom.Node.TEXT_NODE:
0
                 if ch.localName == 'parents':
0
@@ -165,42 +171,43 @@ class Commit(object):
0
     def __repr__(self):
0
         return "<<Commit %s>>" % self.id
0
 
0
+
0
 class GitHub(object):
0
     """Interface to github."""
0
 
0
     def __init__(self, fetcher=default_fetcher):
0
- self.fetcher=fetcher
0
+ self.fetcher = fetcher
0
 
0
     def user(self, username):
0
         """Get the info for a user."""
0
- x=self.fetcher("http://github.com/api/v1/xml/%s" % username).read()
0
- doc=xml.dom.minidom.parseString(x)
0
+ x = self.fetcher("http://github.com/api/v1/xml/%s" % username).read()
0
+ doc = xml.dom.minidom.parseString(x)
0
         return User(doc)
0
 
0
     def search(self, search_string):
0
         """Search for repositories."""
0
- x=self.fetcher("http://github.com/api/v1/xml/search/%s"
0
+ x = self.fetcher("http://github.com/api/v1/xml/search/%s"
0
             % search_string.replace(' ', '+')).read()
0
- doc=xml.dom.minidom.parseString(x)
0
+ doc = xml.dom.minidom.parseString(x)
0
         return SearchResults(doc)
0
 
0
     def commits(self, username, repo, branch='master'):
0
         """Get the recent commits for the given repo."""
0
- x=self.fetcher("http://github.com/api/v1/xml/%s/%s/commits/%s"
0
+ x = self.fetcher("http://github.com/api/v1/xml/%s/%s/commits/%s"
0
             % (username, repo, branch)).read()
0
- doc=xml.dom.minidom.parseString(x)
0
+ doc = xml.dom.minidom.parseString(x)
0
         return [Commit(el) for el in doc.getElementsByTagName('commit')]
0
 
0
     def commit(self, username, repo, commit):
0
         """Get a specific commit from the given repo."""
0
- x=self.fetcher("http://github.com/api/v1/xml/%s/%s/commit/%s"
0
+ x = self.fetcher("http://github.com/api/v1/xml/%s/%s/commit/%s"
0
             % (username, repo, commit)).read()
0
- doc=xml.dom.minidom.parseString(x)
0
+ doc = xml.dom.minidom.parseString(x)
0
         return Commit(doc.getElementsByTagName('commit')[0])
0
 
0
 if __name__ == '__main__':
0
     import sys
0
- gh=GitHub()
0
+ gh = GitHub()
0
     if len(sys.argv) == 2:
0
         u = gh.user(sys.argv[1])
0
         print "User: %s (%s)" % (u.login, u.name)
...
11
12
13
 
14
15
16
17
 
 
 
18
19
20
21
22
 
23
24
25
26
 
27
28
 
29
30
31
...
33
34
35
 
36
37
38
 
39
40
41
...
46
47
48
49
50
 
 
51
52
53
...
58
59
60
61
 
62
63
64
65
66
 
67
68
69
...
11
12
13
14
15
 
 
 
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 
31
32
33
34
...
36
37
38
39
40
41
42
43
44
45
46
...
51
52
53
 
 
54
55
56
57
58
...
63
64
65
 
66
67
68
69
70
 
71
72
73
74
0
@@ -11,21 +11,24 @@ import subprocess
0
 
0
 import github
0
 
0
+
0
 def check_for_old_format(path, url):
0
- p=subprocess.Popen(['git', '--git-dir=' + path, 'config',
0
- 'remote.origin.fetch'], stdout=subprocess.PIPE)
0
- stdout, stderr=p.communicate()
0
+ p = subprocess.Popen(['git', '--git-dir=' + path, 'config',
0
+ 'remote.origin.fetch'], stdout = subprocess.PIPE)
0
+ stdout, stderr = p.communicate()
0
     if stdout.strip() != '+refs/*:refs/*':
0
         print "Not properly configured for mirroring, repairing."
0
         subprocess.call(['git', '--git-dir=' + path, 'remote', 'rm', 'origin'])
0
         add_mirror(path, url)
0
 
0
+
0
 def add_mirror(path, url):
0
     subprocess.call(['git', '--git-dir=' + path, 'remote', 'add', '--mirror',
0
             'origin', url])
0
 
0
+
0
 def sync(path, url, repo_name):
0
- p=os.path.join(path, repo_name) + ".git"
0
+ p = os.path.join(path, repo_name) + ".git"
0
     print "Syncing %s -> %s" % (repo_name, p)
0
     if not os.path.exists(p):
0
         subprocess.call(['git', 'clone', '--bare', url, p])
0
@@ -33,9 +36,11 @@ def sync(path, url, repo_name):
0
     check_for_old_format(p, url)
0
     subprocess.call(['git', '--git-dir=' + p, 'fetch', '-f'])
0
 
0
+
0
 def sync_user_repo(path, user, repo):
0
     sync(path, "git://github.com/%s/%s" % (user.login, repo.name), repo.name)
0
 
0
+
0
 def usage():
0
     sys.stderr.write("Usage: %s username destination_url\n" % sys.argv[0])
0
     sys.stderr.write(
0
@@ -46,8 +51,8 @@ additional projects.
0
 
0
 Each line must be a simple project name (e.g. py-github), a tab character,
0
 and a git URL.
0
-"""
0
- )
0
+""")
0
+
0
 
0
 if __name__ == '__main__':
0
     try:
0
@@ -58,12 +63,12 @@ if __name__ == '__main__':
0
 
0
     privfile = os.path.join(os.getenv("HOME"), ".github-private")
0
     if os.path.exists(privfile):
0
- f=open(privfile)
0
+ f = open(privfile)
0
         for line in f:
0
             name, url = line.strip().split("\t")
0
             sync(path, url, name)
0
 
0
- gh=github.GitHub()
0
+ gh = github.GitHub()
0
     u = gh.user(user)
0
 
0
     for repo in u.repos.values():
...
8
9
10
 
11
12
13
 
14
15
16
...
30
31
32
33
 
34
35
36
37
38
39
40
 
41
42
43
...
46
47
48
49
 
50
51
52
...
60
61
62
63
 
64
65
66
67
68
 
69
70
71
72
73
 
74
75
76
77
78
 
79
80
81
...
83
84
85
86
87
 
 
88
89
90
...
92
93
94
95
 
96
97
98
99
100
101
102
 
103
104
105
106
107
108
 
109
110
 
111
112
113
...
124
125
126
127
 
128
129
130
...
133
134
135
136
137
 
 
138
139
140
...
149
150
151
152
 
153
154
155
...
169
170
171
 
172
173
174
...
8
9
10
11
12
13
14
15
16
17
18
...
32
33
34
 
35
36
37
38
39
40
41
 
42
43
44
45
...
48
49
50
 
51
52
53
54
...
62
63
64
 
65
66
67
68
69
 
70
71
72
73
74
 
75
76
77
78
79
 
80
81
82
83
...
85
86
87
 
 
88
89
90
91
92
...
94
95
96
 
97
98
99
100
101
102
103
 
104
105
106
107
108
109
 
110
111
 
112
113
114
115
...
126
127
128
 
129
130
131
132
...
135
136
137
 
 
138
139
140
141
142
...
151
152
153
 
154
155
156
157
...
171
172
173
174
175
176
177
0
@@ -8,9 +8,11 @@ import unittest
0
 
0
 import github
0
 
0
+
0
 class GitHubTest(unittest.TestCase):
0
 
0
     def __gh(self, expUrl, filename):
0
+
0
         def opener(url):
0
             self.assertEquals(expUrl, url)
0
             return open(filename)
0
@@ -30,14 +32,14 @@ class GitHubTest(unittest.TestCase):
0
             'data/commits.xml').commits('caged', 'gitnub', 'master')
0
 
0
     def __loadCommit(self, which):
0
- id='00000010000101'
0
+ id = '00000010000101'
0
         return self.__gh(
0
             'http://github.com/api/v1/xml/dustin/py-github/commit/%s' % id,
0
             'data/' + which).commit('dustin', 'py-github', id)
0
 
0
     def testUserBase(self):
0
         """Test the base properties of the user object."""
0
- u=self.__loadUser()
0
+ u = self.__loadUser()
0
         self.assertEquals("Dustin Sallings", u.name)
0
         self.assertEquals("dustin", u.login)
0
         self.assertEquals("dustin@spy.net", u.email)
0
@@ -46,7 +48,7 @@ class GitHubTest(unittest.TestCase):
0
 
0
     def testUserRepos(self):
0
         """Test the repositories within the user object."""
0
- u=self.__loadUser()
0
+ u = self.__loadUser()
0
         self.assertEquals(34, len(u.repos))
0
         self.assertEquals('buildwatch', u.repos['buildwatch'].name)
0
         self.assertEquals('http://github.com/dustin/buildwatch',
0
@@ -60,22 +62,22 @@ class GitHubTest(unittest.TestCase):
0
 
0
     def testUserWatchers(self):
0
         """Test the watchers element in the user response."""
0
- u=self.__loadUser()
0
+ u = self.__loadUser()
0
         self.assertEquals(10, u.repos['java-memcached-client'].watchers)
0
 
0
     def testUserForks(self):
0
         """Test the forks element in the user response."""
0
- u=self.__loadUser()
0
+ u = self.__loadUser()
0
         self.assertEquals(3, u.repos['java-memcached-client'].forks)
0
 
0
     def testSearchLen(self):
0
         """Test search results len"""
0
- res=self.__loadSearch()
0
+ res = self.__loadSearch()
0
         self.assertEquals(30, len(res))
0
 
0
     def testSearchSplicing(self):
0
         """Test search results splicing operator"""
0
- res=self.__loadSearch()
0
+ res = self.__loadSearch()
0
         self.assertEquals(3, len(res[:3]))
0
         self.assertEquals(3, len(res[3:6]))
0
 
0
@@ -83,8 +85,8 @@ class GitHubTest(unittest.TestCase):
0
 
0
     def testSearchIteration(self):
0
         """Test search results iteration"""
0
- res=self.__loadSearch()
0
- c=0
0
+ res = self.__loadSearch()
0
+ c = 0
0
         for r in res:
0
             c += 1
0
             self.assertTrue(isinstance(r.forks, int))
0
@@ -92,22 +94,22 @@ class GitHubTest(unittest.TestCase):
0
 
0
     def testSearchGetItem(self):
0
         """Test search results get item"""
0
- res=self.__loadSearch()
0
+ res = self.__loadSearch()
0
 
0
         self.assertEquals('merb-simple-model', res[1].name)
0
         self.assertEquals('merb_active_admin', res[-3].name)
0
 
0
     def testSearchRepr(self):
0
         """Test search results repr"""
0
- res=self.__loadSearch()
0
+ res = self.__loadSearch()
0
 
0
         self.assertEquals('<<SearchResults with 30 repos>>', `res`)
0
 
0
     def testCommitsBase(self):
0
         """Test getting commits."""
0
- commits=self.__loadCommits()
0
+ commits = self.__loadCommits()
0
         self.assertEquals(30, len(commits))
0
- c=commits[0]
0
+ c = commits[0]
0
         self.assertEquals('da603ec86b62418e2ad433bb848ae6073cef7137', c.id)
0
         self.assertEquals("Consider .xib files binary.", c.message)
0
         self.assertEquals(
0
@@ -124,7 +126,7 @@ class GitHubTest(unittest.TestCase):
0
 
0
     def testCommitWithAdd(self):
0
         """Testing a commit with some adds."""
0
- c= self.__loadCommit('commit-with-add.xml')
0
+ c = self.__loadCommit('commit-with-add.xml')
0
         self.assertEquals('33464f2c56ed5fd64319d8dcc52fdfdb5db9d8ae', c.id)
0
         self.assertEquals('b73e9af69c043f68b19aa000980e56377fddb600', c.tree)
0
         self.assertEquals('2008-04-11T21:43:32-07:00', c.committedDate)
0
@@ -133,8 +135,8 @@ class GitHubTest(unittest.TestCase):
0
             c.message)
0
         self.assertEquals(['f54d6071a0dafadd3ce50dd0b01b3ca3b69818c7'],
0
             c.parents)
0
- self.assertEquals('http://github.com/dustin/py-github/commit/%s' % c.id,
0
- c.url)
0
+ self.assertEquals('http://github.com/dustin/py-github/commit/%s' % (
0
+ c.id), c.url)
0
         self.assertEquals('dustin@spy.net', c.author.email)
0
         self.assertEquals('dustin@spy.net', c.committer.email)
0
         self.assertEquals('Dustin Sallings', c.author.name)
0
@@ -149,7 +151,7 @@ class GitHubTest(unittest.TestCase):
0
 
0
     def testCommitWithMerge(self):
0
         """Testing a commit with a merge."""
0
- c= self.__loadCommit('commit-merge.xml')
0
+ c = self.__loadCommit('commit-merge.xml')
0
         self.assertEquals('c80c0d9557bc88ec236e7de9854f738c1d6c03b9', c.id)
0
         self.assertEquals('27d3edfc5f719e1f59871b420a5a4af6616ddca0', c.tree)
0
         self.assertEquals('2008-03-12T00:05:00-07:00', c.committedDate)
0
@@ -169,6 +171,7 @@ class GitHubTest(unittest.TestCase):
0
             'English.lproj/MainMenu.xib', 'Info.plist', 'buildwatch.xml'],
0
             [m.filename for m in c.modified])
0
 
0
+
0
 if __name__ == '__main__':
0
     unittest.main()
0
     # gh=github.GitHub(hack)

Comments

    No one has commented yet.