Skip to content

Commit d9e22a0

Browse files
author
Ask Solem
committed
Added support for repositories (http://develop.github.com/p/repo.html)
1 parent a62e478 commit d9e22a0

1 file changed

Lines changed: 114 additions & 26 deletions

File tree

github2/client.py

Lines changed: 114 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,61 @@
11
from github2.request import GithubRequest
22

3-
class GithubData(object):
4-
def __init__(self, **kwargs):
5-
for attr_name in self.attributes:
6-
if attr_name in kwargs:
7-
setattr(self, attr_name, kwargs[attr_name])
8-
else:
9-
setattr(self, attr_name, None)
10-
11-
def to_dict(self):
12-
dict_ = {}
13-
for attr_name in self.attributes:
14-
attr_value = getattr(self, attr_name, None)
15-
if attr_value is not None:
16-
dict_[attr_name] = attr_value
17-
return dict_
18-
19-
20-
class Issue(GithubData):
3+
4+
class BaseDataType(type):
5+
6+
def __new__(cls, name, bases, attrs):
7+
super_new = super(BaseDataType, cls).__new__
8+
9+
attributes = attrs.pop("attributes", tuple())
10+
attrs.update(dict([(attr_name, None)
11+
for attr_name in attributes]))
12+
13+
def constructor(self, **kwargs):
14+
for attr_name, attr_value in kwargs.items():
15+
if attr_name not in attributes:
16+
raise TypeError("%s.__init__() doesn't support the "
17+
"%s argument." % ( cls_name, attr_name))
18+
setattr(self, attr_name, attr_value)
19+
attrs["__init__"] = constructor
20+
21+
def to_dict(self):
22+
dict_ = {}
23+
for attr_name in self.attributes:
24+
attr_value = getattr(self, attr_name, None)
25+
if attr_value is not None:
26+
dict_[attr_name] = attr_value
27+
return dict_
28+
attrs["to_dict"] = to_dict
29+
30+
return super_new(cls, name, bases, attrs)
31+
32+
33+
class BaseData(object):
34+
__metaclass__ = BaseDataType
35+
36+
37+
class Issue(BaseData):
2138
attributes = ("position", "number", "votes", "body", "title",
2239
"created_at", "updated_at", "user", "state")
2340

24-
class User(GithubData):
25-
attributes = ("id", "login", "name", "company", "location", "email",
26-
"blog", "following_count", "followers_count",
41+
42+
class User(BaseData):
43+
attributes = ("id", "login", "name", "company", "location",
44+
"email", "blog", "following_count", "followers_count",
2745
"public_gist_count", "public_repo_count",
28-
"total_private_repo_count", "collaborators", "disk_usage",
29-
"owned_private_repo_count", "private_gist_count",
30-
"plan")
46+
"total_private_repo_count", "collaborators",
47+
"disk_usage", "owned_private_repo_count",
48+
"private_gist_count", "plan")
3149

3250
def is_authenticated(self):
3351
return self.plan is not None
3452

3553

54+
class Repository(BaseData):
55+
attributes = ("description", "forks", "name", "watchers", "private",
56+
"url", "fork", "owner", "homepage")
57+
58+
3659
class GithubCommand(object):
3760

3861
def __init__(self, request):
@@ -62,17 +85,18 @@ def show(self, username):
6285
return User(**user_data)
6386

6487
def followers(self, username):
65-
return self.make_request("show", username, "followers")
88+
return self.make_request("show", username, "followers", filter="users")
6689

6790
def following(self, username):
68-
return self.make_request("show", username, "following")
91+
return self.make_request("show", username, "following", filter="users")
6992

7093
def follow(self, other_user):
7194
return self.make_request("follow", other_user)
7295

7396
def unfollow(self, other_user):
7497
return self.make_request("unfollow", other_user)
7598

99+
76100
class Issues(GithubCommand):
77101
domain = "issues"
78102

@@ -112,12 +136,76 @@ def add_label(self, project, number, label):
112136
def remove_label(self, project, number, label):
113137
return self.make_request("label/remove", project, label, str(number),
114138
filter="labels")
139+
140+
class Repositories(GithubCommand):
141+
domain = "repos"
142+
143+
def search(self, query):
144+
return self.make_request("search", query, filter="repositories")
145+
146+
def show(self, project):
147+
repo_data = self.make_request("show", project, filter="repository")
148+
return Repository(**repo_data)
149+
150+
def list(self, for_user=None):
151+
for_user = for_user or self.request.username
152+
return [Repository(**repo_data)
153+
for repo_data in self.make_request("show", for_user,
154+
filter="repositories")]
155+
156+
def watch(self, project):
157+
return self.make_request("watch", project)
158+
159+
def unwatch(self, project):
160+
return self.make_request("unwatch", project)
161+
162+
def fork(self, project):
163+
new_repo_data = self.make_request("fork", project,
164+
filter="repository")
165+
return Repository(**new_repo_data)
166+
167+
def create(self, name, description=None, homepage=None, public=True):
168+
repo_data = {"name": name, "description": description,
169+
"homepage": homepage, "public": str(int(public))}
170+
new_repo_data = self.make_request("create", post_data=repo_data,
171+
filter="repository")
172+
return Repository(**new_repo_data)
173+
174+
def set_private(self, repo_name):
175+
return self.make_request("set/private", repo_name)
176+
177+
def set_public(self, repo_name):
178+
return self.make_request("set/public", repo_name)
179+
180+
def list_collaborators(self, project):
181+
return self.make_request("show", project, "collaborators",
182+
filter="collaborators")
183+
184+
def add_collaborator(self, repo_name, username):
185+
return self.make_request("collaborators", repo_name, "add", username)
186+
187+
def remove_collaborator(self, repo_name, username):
188+
return self.make_request("collaborators", repo_name, "remove",
189+
username)
190+
191+
def network(self, project):
192+
return self.make_request("show", project, "network", filter="network")
193+
194+
def tags(self, project):
195+
return self.make_request("show", project, "tags", filter="tags")
196+
197+
def branches(self, project):
198+
return self.make_request("show", project, "branches",
199+
filter="branches")
200+
201+
115202
class Github(object):
116203

117204
def __init__(self, username, api_token):
118205
self.request = GithubRequest(username=username, api_token=api_token)
119206
self.issues = Issues(self.request)
120207
self.users = Users(self.request)
208+
self.repos = Repositories(self.request)
121209

122210
def project_for_user_repo(self, user, repo):
123211
return "/".join([user, repo])

0 commit comments

Comments
 (0)