|
1 | 1 | from github2.request import GithubRequest |
2 | 2 |
|
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): |
21 | 38 | attributes = ("position", "number", "votes", "body", "title", |
22 | 39 | "created_at", "updated_at", "user", "state") |
23 | 40 |
|
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", |
27 | 45 | "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") |
31 | 49 |
|
32 | 50 | def is_authenticated(self): |
33 | 51 | return self.plan is not None |
34 | 52 |
|
35 | 53 |
|
| 54 | +class Repository(BaseData): |
| 55 | + attributes = ("description", "forks", "name", "watchers", "private", |
| 56 | + "url", "fork", "owner", "homepage") |
| 57 | + |
| 58 | + |
36 | 59 | class GithubCommand(object): |
37 | 60 |
|
38 | 61 | def __init__(self, request): |
@@ -62,17 +85,18 @@ def show(self, username): |
62 | 85 | return User(**user_data) |
63 | 86 |
|
64 | 87 | def followers(self, username): |
65 | | - return self.make_request("show", username, "followers") |
| 88 | + return self.make_request("show", username, "followers", filter="users") |
66 | 89 |
|
67 | 90 | def following(self, username): |
68 | | - return self.make_request("show", username, "following") |
| 91 | + return self.make_request("show", username, "following", filter="users") |
69 | 92 |
|
70 | 93 | def follow(self, other_user): |
71 | 94 | return self.make_request("follow", other_user) |
72 | 95 |
|
73 | 96 | def unfollow(self, other_user): |
74 | 97 | return self.make_request("unfollow", other_user) |
75 | 98 |
|
| 99 | + |
76 | 100 | class Issues(GithubCommand): |
77 | 101 | domain = "issues" |
78 | 102 |
|
@@ -112,12 +136,76 @@ def add_label(self, project, number, label): |
112 | 136 | def remove_label(self, project, number, label): |
113 | 137 | return self.make_request("label/remove", project, label, str(number), |
114 | 138 | 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 | + |
115 | 202 | class Github(object): |
116 | 203 |
|
117 | 204 | def __init__(self, username, api_token): |
118 | 205 | self.request = GithubRequest(username=username, api_token=api_token) |
119 | 206 | self.issues = Issues(self.request) |
120 | 207 | self.users = Users(self.request) |
| 208 | + self.repos = Repositories(self.request) |
121 | 209 |
|
122 | 210 | def project_for_user_repo(self, user, repo): |
123 | 211 | return "/".join([user, repo]) |
|
0 commit comments